Skip to content

Instantly share code, notes, and snippets.

@mouseroot
Created August 1, 2013 05:40
Show Gist options
  • Save mouseroot/6128651 to your computer and use it in GitHub Desktop.
Save mouseroot/6128651 to your computer and use it in GitHub Desktop.
Win32 CreateWindow example in python
from ctypes import *
from ctypes.wintypes import *
WNDPROCTYPE = WINFUNCTYPE(c_int, HWND, c_uint, WPARAM, LPARAM)
WS_EX_APPWINDOW = 0x40000
WS_OVERLAPPEDWINDOW = 0xcf0000
WS_CAPTION = 0xc00000
SW_SHOWNORMAL = 1
SW_SHOW = 5
CS_HREDRAW = 2
CS_VREDRAW = 1
CW_USEDEFAULT = 0x80000000
WM_DESTROY = 2
WHITE_BRUSH = 0
class WNDCLASSEX(Structure):
_fields_ = [("cbSize", c_uint),
("style", c_uint),
("lpfnWndProc", WNDPROCTYPE),
("cbClsExtra", c_int),
("cbWndExtra", c_int),
("hInstance", HANDLE),
("hIcon", HANDLE),
("hCursor", HANDLE),
("hBrush", HANDLE),
("lpszMenuName", LPCWSTR),
("lpszClassName", LPCWSTR),
("hIconSm", HANDLE)]
def PyWndProcedure(hWnd, Msg, wParam, lParam):
if Msg == WM_DESTROY:
windll.user32.PostQuitMessage(0)
else:
return windll.user32.DefWindowProcW(hWnd, Msg, wParam, lParam)
return 0
def main():
WndProc = WNDPROCTYPE(PyWndProcedure)
hInst = windll.kernel32.GetModuleHandleW(0)
wclassName = 'My Python Win32 Class'
wname = 'My test window'
wndClass = WNDCLASSEX()
wndClass.cbSize = sizeof(WNDCLASSEX)
wndClass.style = CS_HREDRAW | CS_VREDRAW
wndClass.lpfnWndProc = WndProc
wndClass.cbClsExtra = 0
wndClass.cbWndExtra = 0
wndClass.hInstance = hInst
wndClass.hIcon = 0
wndClass.hCursor = 0
wndClass.hBrush = windll.gdi32.GetStockObject(WHITE_BRUSH)
wndClass.lpszMenuName = 0
wndClass.lpszClassName = wclassName
wndClass.hIconSm = 0
regRes = windll.user32.RegisterClassExW(byref(wndClass))
hWnd = windll.user32.CreateWindowExA(
0,wclassName,wname,
WS_OVERLAPPEDWINDOW | WS_CAPTION,
CW_USEDEFAULT, CW_USEDEFAULT,
300,300,0,0,hInst,0)
if not hWnd:
print('Failed to create window')
exit(0)
print('ShowWindow', windll.user32.ShowWindow(hWnd, SW_SHOW))
print('UpdateWindow', windll.user32.UpdateWindow(hWnd))
msg = MSG()
lpmsg = pointer(msg)
print('Entering message loop')
while windll.user32.GetMessageA(lpmsg, 0, 0, 0) != 0:
windll.user32.TranslateMessage(lpmsg)
windll.user32.DispatchMessageA(lpmsg)
print('done.')
if __name__ == "__main__":
print "Win32 Application in python"
main()
@derekbrokeit
Copy link

This works great on 32-bit compiled Python, but if I run this on the "amd64" Python that runs on x64, I always Failed to create window. Any thoughts on how to get this to work on Python amd64?

@murkymark
Copy link

For me it works on Python 2.7 but not on Python 3.4, all 32 bit. "CreateWindowExA" returns 0 on Python 3.4.
Edit:
It's related to the Python string format.
Using "RegisterClassExA" and "CreateWindowExA" works on Python 3.4 with broken window title but not on 2.7.
Using "RegisterClassExW" and "CreateWindowExW" works on Python 3.4 with correct window title but not on 2.7., but with "wclassName" and "wname" strings prefixed with 'u' (u'My Python Win32 Class') it works for both properly.

@peterkong752
Copy link

peterkong752 commented Apr 30, 2020

A great code! It can be worked on my side, and i want to know how I can create a window without a title bar? Any help will be appreciated.

@PasXanter
Copy link

To make the code work, just add this line to the top of the file:
windll.user32.DefWindowProcW.argtypes = HWND, UINT, WPARAM, LPARAM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment