1. 程式人生 > >窗體顯示類

窗體顯示類

mes tracking 變量 內存 raw win32 void gets mds

窗體在前面已經註冊。而且已經創建出來了,但這時這個窗體並不能出如今我們的眼前,又是什麽原因呢?哦,還不顯示出來,原來是有原因的,就是窗體有多種狀態。窗體能夠隱藏、普通顯示、最大化顯示、最小化顯示等。而且創建出來時。不馬上顯示。也是能夠方便一性地創建非常多非常多窗體,最後才一次性地顯示出來。另外創建窗體之後在系統看來窗體已經是可用的。這時能夠先在窗體上畫圖。當完畢時再一次性顯示出來,也避免窗體不斷地刷新時窗體在閃動,看起來讓人眼花繚亂。因而在這裏就封裝一個窗體顯示類Window。這個類非常easy,它的代碼例如以下:

#開發者:蔡軍生(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把窗體的客戶區進行更新。

到這裏已經把界面操作功能封裝成三個類:WinClassMakerWinMakerWindow

這三個類的功能是各司其職,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()    
       


窗體顯示類