Window Display class in Python

Source: Internet
Author: User
Tags constructor in python

The window is already registered in front of it, and has been created, but this window does not appear before our eyes, what is the reason? Oh, still do not show out, there is a reason, is the window has a variety of states, the window can be hidden, the general display, maximize display, minimize the display. And when it is created, it is not immediately displayed, it is also easy to create a lot of many windows, the last only to show it once; In addition, after creating a window in the system appears to be available, the window can be drawn first, when the completion of a one-time display, but also to avoid the window is constantly refreshed when the window flashing, It seems to dazzle people. So here we encapsulate a window showing Class window, this class is very simple, and its code is as follows:

#开发人员: Cai Junsheng (qq:9073204) Shenzhen  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)

In this class, we mainly provide constructor __init__ and display functions. In the constructor, the main is to create the member variable Self.hwnd that holds the window handle, which is used to save the corresponding window's relationship with this class. Show function display is mainly called the system's API function ShowWindow window display, call the system's API function UpdateWindow The window of the client area to update.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

To this point, the interface operation function has been encapsulated into three classes: Winclassmaker, Winmaker and Windows. The function of these three classes is to perform their duties, Winclassmaker is responsible for the window style registration, custom different types of Windows; Winmaker is responsible for styling the window to create an entity object out of the developer's point of view is allocating memory to an object window is responsible for displaying, hiding or moving the windows accordingly. Since these three sections are the basic function of all window classes, you will need to create new Windows later, and you can inherit the three classes for reuse code.

Putting these three classes together, rewrite the previous example code, and it becomes the following:

#windows应用程序 #使用类来描述 from ctypes import * ctypes.wintypes import * Ws_ex_appwindow = 0x40000 Ws_overlappe Dwindow = 0xcf0000 Ws_caption = 0xc00000 Sw_shownormal = 1 Sw_show = 5 Cs_hredraw = 2 Cs_vredraw = 1 Cw_usedefault = 0x800 00000 Wm_destroy = 2 White_brush = 0 Wndproctype = Winfunctype (C_int, HWND, C_uint, WPARAM, LPARAM) #定义窗口类结构 cl Ass 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)]  
 #开发人员: Cai Junsheng (qq:9073204) Shenzhen 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, class Name, hinst): Self.wndclass = Wndclassex () self.wndClass.cbSize = sizeof (wndclassex) self.wn Dclass.style = Cs_hredraw | Cs_vredraw Self.wndClass.lpfnWndProc = WndProc Self.wndClass.cbClsExtra = 0 self.wndClass.cbWn
        Dextra = 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.us Er32. 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_usedefau LT 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.exsty  
            Le, 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 s Elf.hwnd #窗口消息处理回调函数 def pywndproc (HWnd, MSG, WParam, LParam): If MSG = Wm_destroy:wi  Ndll.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.Di  
          
          
Spatchmessagew (lpmsg) print (' Done. ') if __name__ = = "__main__": Print ("Win32 application in Python") Main ()

Author: csdn Blog Caimouse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.