Skip to content

Instantly share code, notes, and snippets.

@rebane2001
Created May 11, 2024 10:08
Show Gist options
  • Save rebane2001/a796616fee241ce84c57f8312d2bced7 to your computer and use it in GitHub Desktop.
Save rebane2001/a796616fee241ce84c57f8312d2bced7 to your computer and use it in GitHub Desktop.
Toggle the border of a specific window using winuser.h calls in Python
import ctypes
target_window_class = "NotITG LowLevelWindow_Win32"
target_window_title = None
SWP_NOZORDER = 0x0004
SWP_FRAMECHANGED = 0x0020
SWP_NOMOVE = 0x0002
SWP_NOSIZE = 0x0001
SWP_NOOWNERZORDER = 0x0200
GWL_STYLE = -16
GWL_EXSTYLE = -20
WS_CAPTION = 0x00C00000
WS_THICKFRAME = 0x00040000
WS_MINIMIZEBOX = 0x00020000
WS_MAXIMIZEBOX = 0x00010000
WS_SYSMENU = 0x00080000
WS_EX_DLGMODALFRAME = 0x00000001
WS_EX_CLIENTEDGE = 0x00000200
WS_EX_STATICEDGE = 0x00020000
L_BORDER = WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU
L_EX_BORDER = WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE
h_wnd = ctypes.windll.user32.FindWindowW(target_window_class, target_window_title)
l_style = ctypes.windll.user32.GetWindowLongPtrA(h_wnd, GWL_STYLE)
l_ex_style = ctypes.windll.user32.GetWindowLongPtrA(h_wnd, GWL_EXSTYLE)
if l_style & WS_CAPTION:
l_style &= ~L_BORDER
l_ex_style &= ~L_EX_BORDER
else:
l_style |= L_BORDER
l_ex_style |= L_EX_BORDER
ctypes.windll.user32.SetWindowLongPtrA(h_wnd, GWL_STYLE, l_style)
ctypes.windll.user32.SetWindowLongPtrA(h_wnd, GWL_EXSTYLE, l_ex_style)
ctypes.windll.user32.SetWindowPos(h_wnd, None, 0, 0, 0, 0, SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOOWNERZORDER)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment