开发者

Simulate mouse events from C++ in Windows

I have a program running with a touch screen, but because the mouse pointer being "on" the touch screen introduces problems, the Windows-mouse behavior of the touch screen has been disabled, and a home-grown program is being used to monitor the touch screen and post messages to the window at the location the screen was touched or untouched (ie. WM_LBUTTONDOWN, WM_LBUTTONUP).

The relevant code looks something like this:

touched = false;

while (1)
{
    if (!touched) {
        // p.x and p.y calculated here based on mouse position at
        // time of touch screen event
        p.x = ...;
        p.y = ...;

        if ((window = WindowFromPoint (p)) != NULL)
            PostMessage (window, WM_LBUTTONDOWN, 0, 0);

        touched = true;
    }
    else
    {
        if ((window = WindowFromPoint (p)) != NULL)
            PostMessage (window, WM_LBUTTONUP, 0, 0);

        touched = false;
    }
}

This works, but doesn't exactly mimic Windows' mouse press behavior -- regardless of the position of the touch when the untouch occurs (ie. if a touch was dragged), the WM_LBUTTONUP is sent to the control that received the WM_LBUTTONDOWN.

What I was trying to do was make it more Windows-like. In any application, open a dialog that has a button on it. Click and hold the mouse on the button, you'll see it depress. Drag the mouse off the button and don't release the mouse, and you'll see the button raise again. Drag the mouse back onto the button and you'll see it depress. If you release the mouse while the pointer is on the button, the button is pressed. If you release the mouse while the pointer is off the button, the button is not pressed.

I set up a low-level mous开发者_如何转开发e hook, and can see that the only mouse events that occur during this sequence are WM_LBUTTONDOWN followed by a series of WM_MOUSEMOVE followed by a WM_LBUTTONUP, regardless of whether the mouse is released on or off the button.

I have tried adding alternative handling for when the mouse is dragged, and have this posting WM_MOUSEMOVE messages, but which control should these be sent to? The button that the WM_LBUTTONDOWN event was originally sent to, or elsewhere? I have tried both the button and the window the button is on, but obviously I've done something wrong as it doesn't seem to work. The best I have been able to achieve is the touch screen button not being "clicked" when I untouch off the button, but the touch screen button is still drawn depressed.

Any suggestions?

Can anyone confirm which events should be sent where during this operation?

Thanks for any advice.


This is your code:

    PostMessage (window, WM_LBUTTONDOWN, 0, 0);

Can you try this instead of the above line:

    DWORD dw = MAKEWORD(p.x, p.y);
    PostMessage (window, WM_LBUTTONDOWN, MK_LBUTTON, dw);

I think this code should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜