Board index » delphi » WM_LBUTTONDOWN/UP messages

WM_LBUTTONDOWN/UP messages

I have written the attached procedure to simulate sending mouse left-click
events to applications (by directly sending WM_LBUTTONDOWN & WM_LBUTTONUP
messages to the correct window).  The code works great with every
application I've tried except for those which have used the Woll2Woll 1st
class shape
and image buttons.  The 1st class shape and image buttons will generate the
OnMouseDown() and OnMouseUp() events but will not generate the OnClick()
event.  Any know why the 1st class controls behave this way?  What
additional
Windows messages do they neeed to work correctly?

If you would like to see if you can reproduce this problem simply add the
following procedure to a form and send another application a left button
press by passing the procedure the X and Y screen coordinates of the place
you want to click...(that is if you have an application with the 1st class
shape and/or image buttons on them--so you can see the problem.)

Any help would be very appreciated...

procedure SendLeftButtonClick(X, Y: Integer);
var
  ClientX, ClientY: Integer;
  Window, Parent: HWnd;
  WindowRect: TRECT;

begin
  // Set the cursor position to the specified X, Y coordinates.
  //
  SetCursorPos(X, Y);

  // Get the window at that X, Y coordinates.
  // (does not get handle to hidden or disabled windows)
  //
  Window := WindowFromPoint(Point(X, Y));
  // Was there a window at that coordinate?
  //
  if (Window <> 0) then begin
    // Get the TRect (top, left, bottom, right coordinates).
    //
    GetWindowRect(Window, WindowRect);

    // And translate the X, Y screen coordinates into the client's
    // X, Y coordinates.
    //
    ClientX := X - WindowRect.Left;
    ClientY := Y - WindowRect.Top;

    // Get the parent root window.
    //
    Parent := GetAncestor(Window, GA_ROOT);
    if (Parent = 0) then
      Parent := Window;

    // Check to see if the parent window is already the
    // foreground window.  If no make it the foreground window.
    //
    if (Parent <> GetForegroundWindow()) then begin
      SetForegroundWindow(Parent);
    end;

    // Do a left-button click.
    //
    SendMessage(Window, WM_LBUTTONDOWN, MK_LBUTTON, ClientX or (ClientY shl
16));
    SendMessage(Window, WM_LBUTTONUP, 0, ClientX or (ClientY shl 16));
  end;
end;

 

Re:WM_LBUTTONDOWN/UP messages


Never mind, I found the problem.  The 1st class chape and image buttons
require that the VK_LBUTTON keyboard state to be set to generate a click()
event.  I set the keyboard state before I generate the WM_LBUTTONDOWN/UP and
now these buttons work just fine too...

"Wendell W. Pinegar" <Wend...@thePinegars.com> wrote in message
news:8gcmtv$9797@bornews.borland.com...

Quote
> I have written the attached procedure to simulate sending mouse left-click
> events to applications (by directly sending WM_LBUTTONDOWN & WM_LBUTTONUP
> messages to the correct window).  The code works great with every
> application I've tried except for those which have used the Woll2Woll 1st
> class shape
> and image buttons.  The 1st class shape and image buttons will generate
the
> OnMouseDown() and OnMouseUp() events but will not generate the OnClick()
> event.  Any know why the 1st class controls behave this way?  What
> additional
> Windows messages do they neeed to work correctly?

> If you would like to see if you can reproduce this problem simply add the
> following procedure to a form and send another application a left button
> press by passing the procedure the X and Y screen coordinates of the place
> you want to click...(that is if you have an application with the 1st class
> shape and/or image buttons on them--so you can see the problem.)

> Any help would be very appreciated...

> procedure SendLeftButtonClick(X, Y: Integer);
> var
>   ClientX, ClientY: Integer;
>   Window, Parent: HWnd;
>   WindowRect: TRECT;

> begin
>   // Set the cursor position to the specified X, Y coordinates.
>   //
>   SetCursorPos(X, Y);

>   // Get the window at that X, Y coordinates.
>   // (does not get handle to hidden or disabled windows)
>   //
>   Window := WindowFromPoint(Point(X, Y));

>   // Was there a window at that coordinate?
>   //
>   if (Window <> 0) then begin
>     // Get the TRect (top, left, bottom, right coordinates).
>     //
>     GetWindowRect(Window, WindowRect);

>     // And translate the X, Y screen coordinates into the client's
>     // X, Y coordinates.
>     //
>     ClientX := X - WindowRect.Left;
>     ClientY := Y - WindowRect.Top;

>     // Get the parent root window.
>     //
>     Parent := GetAncestor(Window, GA_ROOT);
>     if (Parent = 0) then
>       Parent := Window;

>     // Check to see if the parent window is already the
>     // foreground window.  If no make it the foreground window.
>     //
>     if (Parent <> GetForegroundWindow()) then begin
>       SetForegroundWindow(Parent);
>     end;

>     // Do a left-button click.
>     //
>     SendMessage(Window, WM_LBUTTONDOWN, MK_LBUTTON, ClientX or (ClientY
shl
> 16));
>     SendMessage(Window, WM_LBUTTONUP, 0, ClientX or (ClientY shl 16));
>   end;
> end;

Re:WM_LBUTTONDOWN/UP messages


On Mon, 22 May 2000 20:39:46 -0500, "Wendell W. Pinegar"

Quote
<Wend...@thePinegars.com> wrote:
>I have written the attached procedure to simulate sending mouse left-click
>events to applications (by directly sending WM_LBUTTONDOWN & WM_LBUTTONUP
>messages to the correct window).

Just a note: you might want to check out the mouse_event Win32 API function.  It
was designed to allow synthesizing mouse events like this.
Greg Chapman

Re:WM_LBUTTONDOWN/UP messages


Greg,

Yes, I had seen a few sample application that use the mouse_event API.  The
underlying windows messages are still the same WM_LBUTTON* messages, and the
sample applications that I ran that used mouse_event also exhibited the same
problem with the 1st class buttons...it was a bug in the Woll2Woll 1st class
components...

Quote
"Greg Chapman" <g...@well.com> wrote in message

news:280ois8gmuji9257aqr5pd27q2fi7e5lqh@4ax.com...
Quote
> On Mon, 22 May 2000 20:39:46 -0500, "Wendell W. Pinegar"
> <Wend...@thePinegars.com> wrote:

> >I have written the attached procedure to simulate sending mouse
left-click
> >events to applications (by directly sending WM_LBUTTONDOWN & WM_LBUTTONUP
> >messages to the correct window).

> Just a note: you might want to check out the mouse_event Win32 API
function.  It
> was designed to allow synthesizing mouse events like this.

> Greg Chapman

Other Threads