The
has already learned how to encapsulate the functions of the Registration window in the form of a class, followed by the ability to create a window, and how this part of the functionality is most appropriate, so that all subsequent development work can reuse the code. Let's take a closer look at what the main thing on the window is mutable and what remains unchanged, in principle, is to change the variable into something that is changed in a functional way, and the unchanging thing is placed inside the class, thus reducing the problem that the class user is considering in detail. At present, a window is the easiest to change the two things: the window type, indicating that the window belongs to that kind of, such as edit, list, and so on; the other is the title of the window, because the user of the software can only distinguish the same type of window from the title of the window. In fact, these two points is from different angles to distinguish the needs of the window, window type is from the developer perspective to see the types of different windows; The title of the window is to look at the functional distinctions of different windows from the user's point of view. With these two points of thought, you can write the code for this class, the complete code is as follows:
#开发人员: Cai Junsheng (qq:9073204) Shenzhen 2014-8-20 #创建窗口 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
Class is the keyword of the Python class, which is used primarily to define the class name. Winmaker is the name of the class that I named creating the window, and you can use this class later to create many many windows without having to copy and paste n more code. Then you see the __init__ constructor, the main thing in this constructor is to create all the required parameters for the window. In the argument list for this constructor, ClassName is the name of the window type; Hinst is the handle that the window belongs to the application instance. When created, other window parameters use the default parameters. Of course, if you want to control more window properties, you can add it to the argument list. At last, we define the creation function to create the operation of the window, in which the title of the window is passed through the parameter, so that the window can display different names. In this code, the main use of Python's class three features, one is the Python class constructor __init__; one is the Python class member function create; one is a member variable of the Python class, such as Self.classname = ClassName.
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/