Form Display Class

Source: Internet
Author: User

The form has been previously registered. and has been created, but this form does not appear in our eyes today, and what is the reason? Oh, it is not shown, the original reason is that the form has a variety of states. The form can be hidden, normal display, maximized display, minimized display, and so on. And when it is created. Not immediately displayed. It is also possible to easily create very many very many forms in one place, and finally show them at once. After creating the form in addition, the form is already available in the system view. You can then draw on the form first. When finished, and then one-time display, but also to avoid the form constantly refreshed when the form is flashing, it looks dazzling. Thus, a form display class Windowis encapsulated here. This class is very easy, its code such as the following:

#开发者: Cai Junsheng (qq:9073204) Shenzhen  2014-8-24# form 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 the constructor __init__ and display function . In the constructor, the principal is to create a member variable Self.hwnd that holds the form handle, which is used to save the corresponding form's relationship with this class. Show function display is mainly called the system's API function ShowWindow to display the form, call the system's API function UpdateWindow Update the customer area of the form.

Here we have encapsulated the interface operation function into three classes:winclassmaker,winmaker , and Window.

The function of these three classes is to do their own jobs,Winclassmaker is responsible for the form style register, custom different form type. Winmaker is responsible for styling the form to create an entity object out. The developer's point of view is allocating memory to an object, andWindow is responsible for displaying, hiding, or moving the form accordingly. Because these three parts are the basic functionality of all form classes, you will need to create a new form later. are able to inherit the three classes to reuse the code.

Put these three classes together. Rewrite the previous sample code again and it will look like this:

#windows应用程序 # Use classes to describe the narrative from ctypes import *from ctypes.wintypes Import *ws_ex_appwindow = 0x40000ws_overlappedwindow = 0xcf0000ws_caption = 0xc00000sw_shownormal = 1sw_show = 5cs_hredraw = 2cs_vredraw = 1cw_usedefault = 0x80000000WM_DESTROY = 2white_brush = 0WNDPROCTYPE = Winfunctype (C_int, HWND, C_uint, WPARAM, LPARAM) #定义窗体类结构class wndclassex (Structure): _f                Ields_ = [("cbsize", C_uint), ("style", C_uint), ("Lpfnwndproc", Wndproctype), ("Cbclsextra", C_int), ("Cbwndextra", C_int), ("HInstance", HANDLE), ("Hico n ", HANDLE), (" Hcursor ", HANDLE), (" Hbrush ", HANDLE), (" Lpszmenuname ", Lpcwst R), ("lpszClassName", LPCWSTR), ("Hiconsm", HANDLE)] #开发者: Cai Junsheng (qq:9073204) Shenzhen 2014-8-24# Form class cl Window:def __init__ (Self, hwnd): Self.hwnd = HWnd def Display (self, cmdshow): Windll.user32.ShowW    Indow (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_hred RAW | 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.wnd Class.hbrush = Windll.gdi32.GetStockObject (white_brush) self.wndClass.lpszMenuName = 0 Self.wndClass.lpszCla Ssname = ClassName self.wndClass.hIconSm = 0 def Register (self): return Windll.user32.RegisterClassExW (by        Ref (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.h            Eight, self.hwndparent, Self.hmenu, Self.hinst, Self.wndcreatdata) If not self.hWnd:print (' Failed to create window ') exit (0) return Self.hwnd #窗体消息处理回        Tune function 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 (ma Ker. Create (Wname)) win. Display (sw_show) msg = MSG () lpmsg = pointer (msg) print (' Entering message loop ') while Windll.user32.GetMe    Ssagew (lpmsg, 0, 0, 0)! = 0:windll.user32.translatemessage (lpmsg) Windll.user32.DispatchMessageW (lpmsg)        Print (' Done. ')            if __name__ = = "__main__": Print ("Win32 application in Python") Main ()


Form Display Class

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.