winapi - 通过Windows C++让鼠标通过

标签 winapi mouse gdi pass-through

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

预先感谢您的帮助。

最佳答案

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

我尝试了 WM_NCHITTEST 方法,以及简单地将 WS_EX_TRANSPARENT 添加到我的窗口。这两种方法都不起作用——它们似乎都捕获了鼠标单击事件,这是我不想要的。

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

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

此行为似乎已记录在案 here :

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.

extended window style documentation也很有用。对于像我这样的应用程序,窗口不应该与之交互,WS_EX_NOACTIVATE 也可能很有用,因为它会阻止某些用户交互。

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

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

关于winapi - 通过Windows C++让鼠标通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13069717/

相关文章:

c - 有没有办法检查(文件)句柄是否有效?

cocoa - 捕获 subview 的 mouseDown 事件。 ( cocoa OSX)

c++ - 从资源加载 png 文件(无需 MFC、ATL)

delphi - 如何使用 GDI 按像素强度将位图转换为灰度图?

c++ - BitBlt 问题 GDI

.net - 如何在 WinAPI 中更改 ListView 的边框颜色?

c# - 添加 WHERE Name = 时出现无效查询错误

java - 模拟右键单击

Java 鼠标监听器

c++ - 如何从 Windows 防火墙中获取异常(exception)列表中的所有应用程序?