为了方便查看,所有源码放在一起,如下:
#windows应用程序 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 #窗口类的名称 wclassName = u'ShenzhenCai' wname = u'Hello World' #定义窗口类结构 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 PyWndProc(hWnd, Msg, wParam, lParam): if Msg == WM_DESTROY: windll.user32.PostQuitMessage(0) else: return windll.user32.DefWindowProcW(hWnd, Msg, wParam, lParam) return 0 #初始化主窗口 def initwinclass(hInst, WndProc): 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 return windll.user32.RegisterClassExW(byref(wndClass)) #主函数入口 def main(): hInst = windll.kernel32.GetModuleHandleW(None) WndProc = WNDPROCTYPE(PyWndProc) if initwinclass(hInst, WndProc) <= 0: return False hWnd = windll.user32.CreateWindowExW( 0, wclassName, wname, WS_OVERLAPPEDWINDOW | WS_CAPTION, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, 0, 0, hInst, 0) if not hWnd: print('Failed to create window') exit(0) windll.user32.ShowWindow(hWnd, SW_SHOW) windll.user32.UpdateWindow(hWnd) msg = MSG() lpmsg = pointer(msg) print('Entering message loop') while windll.user32.GetMessageW(lpmsg, 0, 0, 0) != 0: windll.user32.TranslateMessage(lpmsg) windll.user32.DispatchMessageW(lpmsg) print('done.') if __name__ == "__main__": print( "Win32 Application in python" ) main()