Skip to content

Instantly share code, notes, and snippets.

@d1manson
Last active October 5, 2015 17:17
Show Gist options
  • Save d1manson/60d9d651d6f81c513745 to your computer and use it in GitHub Desktop.
Save d1manson/60d9d651d6f81c513745 to your computer and use it in GitHub Desktop.
failed attempts to put a button in the title bar of a qt application
"""
This is a mess of stuff that never worked.
I put it here main for my own reference, but it might be helpful to someone.
"""
try:
import ctypes
WM_NCCALCSIZE = 0x0083
WM_ACTIVATE = 0x0006
SWP_FRAMECHANGED = 0x0020
WM_CREATE = 0x0001
WM_NCPAINT = 0x0085
DCX_WINDOW = 0x00000001L
DCX_INTERSECTRGN = 0x00000080L
DT_LEFT = 0
DT_TOP = 0
DT_NOCLIP = 0xff
DT_SINGLELINE = 0x00000020
class tagPAINTSTRUCT(ctypes.Structure):
_fields_ = [('hdc', ctypes.wintypes.HDC),
('fErase', ctypes.wintypes.BOOL),
('rcPaint', ctypes.wintypes.RECT),
('fRestore', ctypes.wintypes.BOOL),
('fIncUpdate', ctypes.wintypes.BOOL),
('rgbReserved', ctypes.wintypes.BYTE * 32)]
class WinDLLMargins(ctypes.Structure):
_fields_ = [("cxLeftWidth", ctypes.wintypes.c_int),
("cxRightWidth", ctypes.wintypes.c_int),
("cyTopHeight", ctypes.wintypes.c_int),
("cyBottomHeight", ctypes.wintypes.c_int)]
class tagWINDOWPOS(ctypes.Structure):
_fields_ = [("hwnd", ctypes.wintypes.HWND),
("hwndInsertAfter", ctypes.wintypes.HWND),
("x", ctypes.wintypes.c_int),
("y", ctypes.wintypes.c_int),
("cx", ctypes.wintypes.c_int),
("cy", ctypes.wintypes.c_int),
("flags", ctypes.wintypes.UINT)]
class tagNCCALCSIZE_PARAMS(ctypes.Structure):
_fields_ = [("rgrc", ctypes.wintypes.RECT * 3),
("lppos", ctypes.POINTER(tagWINDOWPOS))]
_win_apis_available = True
except ImportError:
_win_apis_available = False
class MainWindow(QMainWindow):
#...
def winEvent(self, event):
ret = QMainWindow.winEvent(self, event)
if _win_apis_available:
if event.message == WM_NCPAINT:
hwnd = int(self.effectiveWinId()) # TODO: check this is the correct way to cast and should maybe reimplement def event(e): if e.type() == QEvent.winIdChange
hdc = ctypes.windll.user32.GetDCEx(hwnd, event.wParam,
DCX_WINDOW|DCX_INTERSECTRGN)
ps = tagPAINTSTRUCT()
ctypes.windll.user32.BeginPaint(hwnd, ctypes.pointer(ps))
rect = ctypes.wintypes.RECT()
rect.left = 300
rect.right = 900
rect.top = 0
rect.bottom = 30
ctypes.windll.user32.DrawTextW(hdc, ctypes.create_string_buffer(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), -1,
ctypes.pointer(rect),
DT_LEFT | DT_TOP | DT_NOCLIP | DT_SINGLELINE)
ctypes.windll.user32.EndPaint(hwnd, ctypes.pointer(ps))
# Paint into this DC
ctypes.windll.user32.ReleaseDC(hwnd, hdc)
with open("custom_log.txt", "a") as f:
f.write("WM_NCPAINT\n")
return ret
# lRet = ctypes.c_int()
# fCallDWP = not ctypes.windll.dwmapi.DwmDefWindowProc(hwnd,
# event.message, event.wParam, event.lParam, ctypes.byref(lRet))
# lRet = lRet.value
# if event.message == WM_ACTIVATE:
# margins = WinDLLMargins()
# margins.cxLeftWidth = 8
# margins.cxRightWidth = 8
# margins.cyBottomHeight = 20
# margins.cyTopHeight = 27
# ctypes.windll.dwmapi.DwmExtendFrameIntoClientArea(
# ctypes.c_int(self.winId()), ctypes.byref(margins))
# with open("custom_log.txt", "a") as f:
# f.write("WM_ACTIVATE\n")
# lRet = 0
# fCallDWP = True
# if event.message == WM_NCCALCSIZE and event.wParam:
# if self._need_to_setwindowpos:
# self._need_to_setwindowpos = False
# rect = ctypes.wintypes.RECT()
# ctypes.windll.user32.GetWindowRect(hwnd, ctypes.byref(rect))
# with open("custom_log.txt", "w") as f:
# f.write("SetWindowPos {}, ({},{},{},{})\n".format(hwnd,
# rect.left, rect.top, rect.right, rect.bottom))
# ctypes.windll.user32.SetWindowPos(hwnd, None,
# rect.left, rect.top,
# rect.right - rect.left,
# rect.bottom - rect.top,
# SWP_FRAMECHANGED)
# pncsp = ctypes.cast(event.lParam,
# ctypes.POINTER(tagNCCALCSIZE_PARAMS))
# rect = pncsp.contents.rgrc[0]
# with open("custom_log.txt", "a") as f:
# f.write("WM_NCCALCSIZE ({},{},{},{})\n".format(rect.left,
# rect.top, rect.right, rect.bottom))
# lRet = 0
# fCallDWP = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment