简体   繁体   English

从 Windows 中的 C++ 模拟鼠标事件

[英]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 ).我有一个使用触摸屏运行的程序,但由于鼠标指针“在”触摸屏上会出现问题,因此触摸屏的 Windows 鼠标行为已被禁用,并且正在使用自制程序来监控触摸屏并将消息发布到 window 在屏幕被触摸或未触摸的位置(即WM_LBUTTONDOWNWM_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 .这有效,但并不能完全模仿 Windows 的鼠标按下行为——无论触摸发生时触摸的 position (即,如果触摸被拖动), WM_LBUTTONUP都会发送到接收WM_LBUTTONDOWN的控件。

What I was trying to do was make it more Windows-like.我试图做的是让它更像 Windows。 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 mouse 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.我设置了一个低级鼠标挂钩,并且可以看到在此序列期间发生的唯一鼠标事件是WM_LBUTTONDOWN后跟一系列WM_MOUSEMOVE后跟WM_LBUTTONUP ,无论鼠标是在按钮上释放还是释放。

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?我已经尝试为拖动鼠标时添加替代处理,并发布WM_MOUSEMOVE消息,但是应该将这些消息发送到哪个控件? The button that the WM_LBUTTONDOWN event was originally sent to, or elsewhere? WM_LBUTTONDOWN事件最初发送到的按钮,还是其他地方? 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.我已经尝试过按钮和按钮打开的 window,但显然我做错了,因为它似乎不起作用。 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.我认为这段代码应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM