You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WS_EX_TOPMOST标记设置后似乎无效果的问题求助

WS_EX_TOPMOST标记设置后似乎无效果的问题求助

我写了一个pyglet的Window子类,在它的__init__方法里加了一段调用Windows API的代码,用来调整窗口的扩展样式。目前WS_EX_LAYERED和WS_EX_TOOLWINDOW这些标记都正常生效了,可唯独WS_EX_TOPMOST完全没效果,窗口没法固定在所有窗口的最上层。

先贴这段核心的API调用代码:

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)

备注:内容来源于stack exchange,提问作者Pavel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:58:01