让鼠标通过Windows C ++ [英] Letting the mouse pass through Windows C++

查看:95
本文介绍了让鼠标通过Windows C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Win32 C ++应用程序上工作,在这里我想忽略鼠标事件,并传递到窗口下方的窗口中.基本上,我下面的窗口将处理鼠标事件.我宁愿不要使用SendMessage将鼠标消息发送到我下面的窗口中,也不要使用SetCapture.有没有一种基本方法可以忽略鼠标事件,并使其通过Windows API或样式传递?请注意,我的窗口不是透明的.

I am working on an Win32 C++ application where I want to ignore the mouse events and let is pass through to the window beneath my window. Basically the window below mine will handle the mouse event. I would prefer not to send the mouse message using SendMessage to the window beneath mine or use SetCapture. Is there a way basically to ignore the mouse event and let it pass through with Windows APIs or with styles? Note that my window is not transparent.

预先感谢您的帮助.

推荐答案

因此,在尝试创建一个音乐播放器时,我发现了这个问题以及其他问题,该音乐播放器可以在屏幕上覆盖图形显示,而不会影响任何其他交互,例如拖动窗口.

So I found this question, and others, while attempting to create a music player that overlays a graphical display over the screen without impacting any other interaction, including e.g. dragging windows.

我已经尝试了WM_NCHITTEST方法,以及只是将WS_EX_TRANSPARENT添加到我的窗口中.这两种方法都不起作用-它们似乎都捕获了鼠标单击事件,这是我不希望的.

I've tried both the WM_NCHITTEST approach, as well as simply adding WS_EX_TRANSPARENT to my window. Neither of these approaches work -- they both seem to capture mouse click events, which is something I don't want.

但是,碰巧的是,我确实设法找到了可以传递给SetWindowLong(..., GWL_EXSTYLE, ...)的标志的组合,这些标志似乎可以解决问题,并导致以下代码:

However, by pure coincidence, I did manage to find a combination of flags I can pass to SetWindowLong(..., GWL_EXSTYLE, ...) that seem to do the trick, leading to the following code:

LONG cur_style = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, cur_style | WS_EX_TRANSPARENT | WS_EX_LAYERED);

似乎已在此处:

分层窗口的命中测试基于窗口的形状和透明度.这意味着使用色键输入或Alpha值为零的窗口区域将使鼠标消息通过.但是,如果分层窗口具有WS_EX_TRANSPARENT扩展窗口样式,则分层窗口的形状将被忽略,并且鼠标事件将传递到分层窗口下方的其他窗口.

Hit testing of a layered window is based on the shape and transparency of the window. This means that the areas of the window that are color-keyed or whose alpha value is zero will let the mouse messages through. However, if the layered window has the WS_EX_TRANSPARENT extended window style, the shape of the layered window will be ignored and the mouse events will be passed to other windows underneath the layered window.

The extended window style documentation is also very useful. For applications such as mine, where a window is not meant to be interacted with, WS_EX_NOACTIVATE may also be useful, as it prevents some user interactions.

为了后代的缘故,我将注意以下用于确保窗口始终位于顶部的代码:

For posterity's sake, I will note that the code I am using to ensure my window is always on top is the following:

SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

这篇关于让鼠标通过Windows C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆