Create a window application and a window Application

Source: Internet
Author: User

Create a window application and a window Application

Through the previous basic knowledge, we have learned how to use the basic functions of the ctypes library. In order to enhance this knowledge training and make use of the new knowledge, let's create an application that is a little more complex. This application is the foundation of all subsequent applications. Only by learning the basic principles of this program can we further learn WIN32 API program development. What's more, all the subsequent programs will be modified on this program, which is equivalent to the first step of your climb to the upper floor. In all engineering technologies, functions are gradually added from a small demo program to a powerful application. If you find that these API functions are unclear, you can refer to another book I wrote, "one-day training of Windows API", which contains a complete introduction and examples, however, these examples are written in C ++.

Next, let's start browsing the functions of the entire program. The first step is to import the ctypes library so that you can use its functions and import the ctypes library. wintypes, which has the corresponding WIN32 type, makes it easier to use, such as message structure MSG. The Code is as follows:

# Windows Applications

From ctypes import *

From ctypes. wintypes import *

Next, define the prototype of a Windows window callback function, because Windows specifies that each window requires a window message function to process all event messages in the window, in this way, the application you write has the opportunity to respond to events or operations in the window. This line of code is as follows:

WNDPROCTYPE = WINFUNCTYPE (c_int, HWND, c_uint, WPARAM, LPARAM)

Since WIN32 definition constants are all defined in the header file of the C language, it is impossible to directly use them in Python. Currently, there are only two methods: one is to use a conversion program to convert the header files provided by all sdks to the module files defined in Python; the other is to use those files by yourself, define those constants. In this example, the last method is used to define those constants when those constants are defined. In this example, the constants are defined as follows:

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

These constants mainly include the window format, style, and display mode, as well as a message definition and paint brush. Each newly created window must have a unique name in Windows, so that the corresponding window class can be found from the system memory during creation. All other Windows can use this window as the basic template, copy N identical ones. Each window has a topic name, also known as the window name. The two definition constants are as follows:

# Window class name

WclassName = u'shenzhencai'

Wname = u'hello world'

The first constant window class name, and the second constant is the title displayed in the window.

The window format and style are passed to the WIN32 API function by defining a structure. The struct of the window class is defined as follows:

# Define window class structure

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)]

The struct of this window class is the same as the struct described in C language. By defining this, you can describe the window type for the window. For the specific value of each item, see one-day Windows API practice. Next, check the window callback function, which defines the message received in the processing window. The main process is to exit the program when the window is closed (WM_DESTROY. Call the API function PostQuitMessage to send messages to the main program for loop. You need to end the application. Other messages are processed by default by calling the API function DefWindowProcW.

# Window message processing callback 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

Next we will set the properties of the window class and register the window type so that we can create this window later. Here we mainly call the RegisterClassExW function for implementation. In addition, the preceding callback function WndProc, program instance handle hInst, and window class name wclassName are combined.

# Initializing the Main Window

Def initwinclass (hInst, WndProc ):

WndClass = WNDCLASSEX ()

WndClass. cbSize = sizeof (WNDCLASSEX)

WndClass. style = CS_HREDRAW | CS_VREDRAW

WndClass. lpfnWndProc = WndProc

WndClass. cbClsExtra = 0

WndClass. cbWndExtra = 0

WndClass. hInstance = hInst

WndClass. hIcon = 0

WndClass. hCursor = 0

WndClass. hBrush = windll. gdi32.GetStockObject (WHITE_BRUSH)

WndClass. lpszMenuName = 0

WndClass. lpszClassName = wclassName

WndClass. hIconSm = 0

Return windll. user32.RegisterClassExW (byref (wndClass ))

The callback function of the window has been defined, and the window type has been registered. Then, the window is created and displayed. The Code is as follows:

# Main Function entry

Def main ():

HInst = windll. kernel32.GetModuleHandleW (None)

WndProc = WNDPROCTYPE (PyWndProc)

If initwinclass (hInst, WndProc) <= 0:

Return False

HWnd = windll. user32.createappswexw (

0, wclassName, wname,

WS_OVERLAPPEDWINDOW | WS_CAPTION,

CW_USEDEFAULT, CW_USEDEFAULT,

800,600, 0, 0, hInst, 0)

If not hWnd:

Print ('failed' to create Windows ')

Exit (0)

Windll. user32.ShowWindow (hWnd, SW_SHOW)

Windll. user32.UpdateWindow (hWnd)

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 ('did .')

In this main function, integrate the content described above. If you have a better understanding of the previous introduction, it is easy to understand it here. If you still don't understand it, check the previous content and practice it more. The GetModuleHandleW function is used to obtain the instance handle of the current process. The WNDPROCTYPE function is used to convert the callback function. The initwinclass function is used to initialize the window type and register it. The createmediawexw function is used to create a window, the ShowWindow function is used to display the window, and the UpdateWindow function is used to refresh the display client area of the window. The pointer function is used to convert the message pointer, And the GetMessageW function is used to obtain the message of the window, the TranslateMessage function is used to convert messages. The DispatchMessageW function sends converted messages to the message queue. You can create a window, display window, and update window to display the window. After the message is cyclically processed, all events in the window are processed. Finally, there is another way to trigger the main function. Its implementation is as follows:

If _ name _ = "_ main __":

Print ("Win32 Application in python ")

Main ()

A Python feature is used here. When this file is first loaded for running, the name of the current file is set to _ main, in this way, the main function is called to run. When you run this example, the most exciting time is coming. The running result is as follows:


The white window is the window we created. By using the ctypes library, you can use WIN32 API functions and use the Python code to write the code to the maximum extent. Of course, the main demonstration here is the WIN32 API call. In fact, ctypes can also be used in the testing field. For example, you have written a dynamic C ++ Connection Library, this dynamic connection library is used to compress files. If you need to test it, you can write a Python program to call this dynamic Connection Library, it is much faster than writing C ++ test code, because it does not require compilation steps, and there is no temporary file generation, and can be modified at any time, which is more flexible. To improve the efficiency of software development, it is feasible to master multiple languages in the current environment. For example, if the operating performance is high, C ++ should be used for writing, you can use Python to write programs that require high development speed and performance, such as many test cases and automated tools. Hybrid programming is now the mainstream direction, and the friends who are learning programming are working hard and cheering.



For creating a window Application

CLR Windows form application: Let's look at the Baidu entry of CLR, and you will be able to understand it.
Baike.baidu.com/view/605055.htm
MFC form application: it is an MFC-based application. The framework has been written. It is a program method of resource + code in VC.
It is very easy to get started, but if you want to get started with it, you have to look at how to write the framework. For details about the content written by Hou Jie in MFC simple introduction, you can take a closer look.
Win32 Windows application: This is a WinMain () function. The window needs to be implemented by a program, and there is no framework

For beginners, study the C ++ language carefully. If you can understand object-oriented, it's easy to start.

How does Java create a window application?

Swing

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.