标签:des style os 使用 io ar 2014 代码 sp
窗口在前面已经注册,并且已经创建出来了,但这时这个窗口并不能出现在我们的眼前,又是什么原因呢?哦,还不显示出来,原来是有原因的,就是窗口有多种状态,窗口可以隐藏、普通显示、最大化显示、最小化显示等。并且创建出来时,不立即显示,也是可以方便一性地创建很多很多窗口,最后才一次性地显示出来;另外创建窗口之后在系统看来窗口已经是可用的,这时可以先在窗口上绘图,当完成时再一次性显示出来,也避免窗口不断地刷新时窗口在闪动,看起来让人眼花缭乱。因而在这里就封装一个窗口显示类Window,这个类非常简单,它的代码如下:
#开发人员:蔡军生(QQ:9073204) 深圳 2014-8-24 #窗口类 class Window: def __init__(self, hWnd): self.hWnd = hWnd def Display(self, cmdShow): windll.user32.ShowWindow(self.hWnd, cmdShow) windll.user32.UpdateWindow(self.hWnd)
在这个类里主要提供构造函数__init__和显示函数Display。在构造函数里主要是创建保存窗口句柄的成员变量self.hWnd,用来保存对应的窗口与本类的关系。显示函数Display主要是调用系统的API函数ShowWindow把窗口显示出来,调用系统的API函数UpdateWindow把窗口的客户区进行更新。
到这里已经把界面操作功能封装成三个类:WinClassMaker、WinMaker和Window。这三个类的功能是各司其职,WinClassMaker是负责把窗口样式注册,定制不同的窗口类型;WinMaker是负责样式定下来的窗口创建一个实体对象出来,站在开发人员的角度就是分配内存给一个对象;Window是负责把窗口显示、隐藏或者相应的移动等操作。由于这三部分是所有窗口类的基本功能,以后再需要创建新的窗口,都可以继承这三个类来进行复用代码。
把这三个类放在一起,就把前面的例子代码再重写一遍,就变成了下面这个样子:
#windows应用程序 #使用类来描述 from ctypes import * from ctypes.wintypes import * 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 WNDPROCTYPE = WINFUNCTYPE(c_int, HWND, c_uint, WPARAM, LPARAM) #定义窗口类结构 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)] #开发人员:蔡军生(QQ:9073204) 深圳 2014-8-24 #窗口类 class Window: def __init__(self, hWnd): self.hWnd = hWnd def Display(self, cmdShow): windll.user32.ShowWindow(self.hWnd, cmdShow) windll.user32.UpdateWindow(self.hWnd) #窗口类型注册类 class WinClassMaker: def __init__(self, wndProc, className, hInst): self.wndClass = WNDCLASSEX() self.wndClass.cbSize = sizeof(WNDCLASSEX) self.wndClass.style = CS_HREDRAW | CS_VREDRAW self.wndClass.lpfnWndProc = wndProc self.wndClass.cbClsExtra = 0 self.wndClass.cbWndExtra = 0 self.wndClass.hInstance = hInst self.wndClass.hIcon = 0 self.wndClass.hCursor = 0 self.wndClass.hBrush = windll.gdi32.GetStockObject(WHITE_BRUSH) self.wndClass.lpszMenuName = 0 self.wndClass.lpszClassName = className self.wndClass.hIconSm = 0 def Register(self): return windll.user32.RegisterClassExW(byref(self.wndClass)) #创建窗口 class WinMaker: def __init__(self, className, hInst): self.className = className self.hInst = hInst self.style = WS_OVERLAPPEDWINDOW | WS_CAPTION self.exStyle = 0 self.x = CW_USEDEFAULT self.y = 0 self.width = CW_USEDEFAULT self.height = 0 self.hWndParent = HWND(0) self.hMenu = HWND(0) self.wndCreatData = c_void_p(0) def Create(self, title): self.hWnd = windll.user32.CreateWindowExW( self.exStyle, self.className, title, self.style, self.x, self.y, self.width, self.height, self.hWndParent, self.hMenu, self.hInst, self.wndCreatData) if not self.hWnd: print(‘Failed to create window‘) exit(0) return self.hWnd #窗口消息处理回调函数 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 main(): hInst = windll.kernel32.GetModuleHandleW(None) WndProc = WNDPROCTYPE(PyWndProc) className = u‘ShenzhenCai‘ wname = u‘Hello World‘ winClass = WinClassMaker(WndProc, className, hInst) winClass.Register() maker = WinMaker(className, hInst) win = Window(maker.Create(wname)) win.Display(SW_SHOW) 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()
标签:des style os 使用 io ar 2014 代码 sp
原文地址:http://blog.csdn.net/caimouse/article/details/38819101