0

I am using Qt to build a screenshot program.

The question is:

How to obtain the window handle below the mouse cursor? Firstly, I created a full-screen Qt widget to display the fullscreen screenshot. When I move the mouse cursor, how to obtain the handle of window under the cursor? Of course, the full-screen Qt widget should be ignored.

I tried to use win32 API, such as WindowFromPointEx() by filtering the windows with WS_EX_LAYERED attribute. However, I cannot set Qt widget as WS_EX_LAYERED attribute.

What should I do to obtain the handle of the window?

fyl
  • 11
  • 5
  • 1
    The answer is in the [documentation](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-windowfrompoint): *"The `WindowFromPoint` function does not retrieve a handle to a **hidden or disabled** window"*. – IInspectable Jun 14 '20 at 06:43
  • Hi, In my screenshot app, it captures the screen and creates a full-screen window for mouse selection. How to disable this full-screen window in Qt? By the way, I need the full-screen window to respond to my mouse event and repaint. Is it possible? My idea is using WIN32 API to enumerate all windows and check if the mouse cursor is inside these window. – fyl Jun 14 '20 at 17:27
  • If you cannot hide or disable your fullscreen overlay window, [make it transparent](https://stackoverflow.com/a/18317002/1889329). – IInspectable Jun 14 '20 at 17:41
  • I have set the widget and its subwidget transparent. It can still respond to the mouse event. I cannot get the handle of the window below my widget. – fyl Jun 14 '20 at 18:26

1 Answers1

0

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.

Then you can use WindowFromPoint to obtain the handle of window under the cursor.

I have tested it on the win32 desktop program and it works for me. Because I am not familiar with the use of QT, but some similar examples show that this method is feasible.

Refer @JProgrammer's answer,

To do this in Qt use the following code:

Include the 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,

setWindowFlags(windowFlags() | Qt::WindowTransparentForInput);
Strive Sun
  • 5,988
  • 1
  • 9
  • 26