我有一个
pyglet
Window 子类,它在他的 __init__
函数中得到了这个。每个标志都有效,但WS_EX_TOPMOST
。
hwnd = self._hwnd # type: ignore
GWL_EXSTYLE = -20
WS_EX_LAYERED = 0x80000
WS_EX_TOOLWINDOW = 0x00000080
WS_EX_TOPMOST = 0x00000008
LWA_COLORCODE = 0x1
ex_style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, ex_style | WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_TOPMOST)
windll.user32.SetLayeredWindowAttributes(hwnd, 0, 255, LWA_COLORCODE)
windll.user32.SetWindowPos(hwnd, -1, 0, 0, 0, 0, 0x0001 | 0x0002)
完整的
__init__
代码一团糟,但我也会提供它以防止任何误解。
def __init__(self, *args, **kwargs):
# Overriding something for beauty's sake
kwargs['style'] = 'borderless'
kwargs['width'] = bg_img.width
kwargs['height'] = screen.height
super().__init__(*args, **kwargs)
glClearColor(0, 0, 0, 0)
# Doing some unusual low-level gibberish to explain Windows how our pyglet app should behave and look like
hwnd = self._hwnd # type: ignore
GWL_EXSTYLE = -20
WS_EX_LAYERED = 0x80000
WS_EX_TOOLWINDOW = 0x00000080
WS_EX_TOPMOST = 0x00000008
LWA_COLORCODE = 0x1
ex_style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, ex_style | WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_TOPMOST)
windll.user32.SetLayeredWindowAttributes(hwnd, 0, 255, LWA_COLORCODE)
windll.user32.SetWindowPos(hwnd, -1, 0, 0, 0, 0, 0x0001 | 0x0002)
self.set_location(screen.width - self.width, 0)
self.shown = True
self.cursors = {
'default': self.get_system_mouse_cursor(Window.CURSOR_DEFAULT),
'hand': self.get_system_mouse_cursor(Window.CURSOR_HAND)
}
# omelette w/ bacon
self.batch = Batch()
self.bg = Sprite(bg_img, batch = self.batch)
WS_EX_TOPMOST
扩展样式用于使窗口始终出现在所有其他窗口的top上。
但是,单独设置 WS_EX_TOPMOST
不会自动将窗口带到 Z 顺序的顶部。您还需要使用 SetWindowPos
指定窗口位置,因此您需要通过更新这部分代码来确保窗口位于最顶层:
.....
# new styles
windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, ex_style | WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_TOPMOST)
windll.user32.SetLayeredWindowAttributes(hwnd, 0, 255, LWA_COLORCODE)
# topmost window
HWND_TOPMOST = -1
SWP_NOSIZE = 0x0001
SWP_NOMOVE = 0x0002
SWP_SHOWWINDOW = 0x0040
windll.user32.SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW)
.....