模拟来自 C++ 的鼠标事件在Windows中

发布于 2024-11-23 18:54:01 字数 1426 浏览 1 评论 0原文

我有一个在触摸屏上运行的程序,但是由于鼠标指针位于触摸屏上会带来问题,因此触摸屏的 Windows 鼠标行为已被禁用,并且正在使用一个自行开发的程序来监视触摸屏幕并将消息发送到屏幕被触摸或未触摸的位置的窗口(即 WM_LBUTTONDOWNWM_LBUTTONUP)。

相关代码如下所示:

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;
    }
}

这可以工作,但并不完全模仿 Windows 的鼠标按下行为 - 无论发生未触摸时触摸的位置(即,如果触摸被拖动),WM_LBUTTONUP 被发送到接收 WM_LBUTTONDOWN 的控件。

我想做的就是让它更像 Windows。在任何应用程序中,打开一个带有按钮的对话框。单击并按住鼠标在按钮上,您会看到它被按下。将鼠标拖离按钮并且不要释放鼠标,您将看到按钮再次升起。将鼠标拖回到按钮上,您会看到它被按下。如果在指针位于按钮上时释放鼠标,则会按下该按钮。如果在指针离开按钮时释放鼠标,则不会按下该按钮。

我设置了一个低级鼠标挂钩,可以看到在此序列期间发生的唯一鼠标事件是 WM_LBUTTONDOWN ,后跟一系列 WM_MOUSEMOVE ,然后是 >WM_LBUTTONUP,无论鼠标是否在按钮上释放。

我尝试添加拖动鼠标时的替代处理,并发布此 WM_MOUSEMOVE 消息,但这些消息应该发送到哪个控件? WM_LBUTTONDOWN 事件最初发送到的按钮,还是其他地方?我已经尝试了按钮和按钮所在的窗口,但显然我做错了一些事情,因为它似乎不起作用。我能够实现的最好的结果是,当我取消触摸按钮时,触摸屏按钮不会被“单击”,但触摸屏按钮仍然被按下。

有什么建议吗?

任何人都可以确认在此操作期间哪些事件应该发送到哪里?

感谢您的任何建议。

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 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2024-11-30 18:54:01

这是你的代码:

    PostMessage (window, WM_LBUTTONDOWN, 0, 0);

你可以尝试这个而不是上面的行:

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

我认为这个代码应该可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文