Skip to content

Win32 HighGUI backend can't turn top-most state off #25833

@dan-masek

Description

@dan-masek

Verified on Win10 21H2 with OpenCV 4.8.0 (standard build). However, the code remains the same since this was introduced by #14872 (Looks like in milestone 3.4.8, so that corresponds with 4.1.2?) it's going to affect quite a few releases.

The issue can be reproduced using the following Python script:

import cv2
import numpy as np

frame = np.zeros((240, 320, 3), np.uint8)
window = "Test Window"

def show_window(window, frame):
    print(cv2.getWindowProperty(window, cv2.WND_PROP_TOPMOST))
    while(True):
        cv2.imshow(window, frame)
        if cv2.waitKey() > 0:
            break

cv2.namedWindow(window)

cv2.setWindowProperty(window, cv2.WND_PROP_TOPMOST, 1)
show_window(window, frame)

cv2.setWindowProperty(window, cv2.WND_PROP_TOPMOST, 0)
show_window(window, frame)

It prints

1.0
1.0

and the window remains top-most.

I've gone into details in this StackOverflow answer, and concluded that the problem is in the following line:

HWND flag = topmost ? HWND_TOPMOST : HWND_TOP;

Based on how I interpret the documentation of SetWindowPos, HWND_TOP is not the right way, it should be HWND_NOTOPMOST.

This can be verified by the following hack:

import cv2
import numpy as np
import win32gui
import win32con

frame = np.zeros((240, 320, 3), np.uint8)
window = "Test Window"

def show_window(window, frame):
    print(cv2.getWindowProperty(window, cv2.WND_PROP_TOPMOST))
    while(True):
        cv2.imshow(window, frame)
        if cv2.waitKey() > 0:
            break

cv2.namedWindow(window)

cv2.setWindowProperty(window, cv2.WND_PROP_TOPMOST, 1)
show_window(window, frame)

wnd_handle = win32gui.FindWindow(None, window)  
win32gui.SetWindowPos(wnd_handle, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE + win32con.SWP_NOSIZE);

show_window(window, frame)

Window no longer top-most

The actual fix is trivial, add the missing "NO" and "MOST".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions