简体   繁体   English

如何使用 Qt 自动捕获 window

[英]How to capture a window automatically using Qt

I am using Qt to build a screenshot program.我正在使用 Qt 来构建截图程序。

The question is:问题是:

How to obtain the window handle below the mouse cursor?如何获取鼠标cursor下方的window手柄? Firstly, I created a full-screen Qt widget to display the fullscreen screenshot.首先,我创建了一个全屏 Qt 小部件来显示全屏截图。 When I move the mouse cursor, how to obtain the handle of window under the cursor?当我移动鼠标cursor时,如何获取cursor下的window的句柄? Of course, the full-screen Qt widget should be ignored.当然,全屏 Qt 小部件应该被忽略。

I tried to use win32 API, such as WindowFromPointEx() by filtering the windows with WS_EX_LAYERED attribute.我尝试通过使用WS_EX_LAYERED属性过滤 windows 来使用 win32 API,例如WindowFromPointEx() However, I cannot set Qt widget as WS_EX_LAYERED attribute.但是,我无法将 Qt 小部件设置为WS_EX_LAYERED属性。

What should I do to obtain the handle of the window? window的手柄应该怎么做?

On windows you can use to add WS_EX_TRANSPARENT attribute for QT full screen program, and this will make Qt widget ignore keyboard or mouse events.在 windows 上,您可以使用为 QT 全屏程序添加WS_EX_TRANSPARENT属性,这将使 Qt 小部件忽略键盘或鼠标事件。

Then you can use WindowFromPoint to obtain the handle of window under the cursor.然后可以使用WindowFromPoint获取cursor下的window的句柄。

I have tested it on the win32 desktop program and it works for me.我已经在 win32 桌面程序上对其进行了测试,它对我有用。 Because I am not familiar with the use of QT, but some similar examples show that this method is feasible.因为我对QT的使用不熟悉,但是一些类似的例子表明这种方法是可行的。

Refer @JProgrammer's answer ,参考@JProgrammer 的回答

To do this in Qt use the following code:要在 Qt 中执行此操作,请使用以下代码:

Include the header,包括 header,

#if _WIN32
    #include <windows.h>
#endif

and put the following code into the constructor.并将以下代码放入构造函数中。

#if _WIN32
    HWND hwnd = (HWND) winId();
    LONG styles = GetWindowLong(hwnd, GWL_EXSTYLE);
    SetWindowLong(hwnd, GWL_EXSTYLE, styles | WS_EX_TRANSPARENT);
#endif

Or refer @mxttie's answer ,或参考@mxttie 的回答

setWindowFlags(windowFlags() | Qt::WindowTransparentForInput);

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

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