I. Overview
Windows programs have a relatively fixed structure. For writers, they do not need to write the entire process. Most of the processes are completed by the system.
In the program, you only need to enter the small part left by the system to the customer in a certain format.
The following operations are required:
Window class definition, window establishment, message function writing, message cycle.
Ii. Message Processing functions
Windows programs are event-driven. For a window, most of its routine maintenance is maintained by the system. No window has a message processing function.
In the message processing function, the incoming message is processed. The system also has its own default message processing function.
The customer writes a message processing function. Before the window is created, the message processing function is associated with the window. In this way, the message processing function is called whenever a message is generated.
Generally, a customer does not process all messages, but only messages of interest to him. Others are sent back to the default message processing function of the system.
Lresult callback wndproc (hwnd, uint message, wparam, lparam) { Switch (Message) { Case... ... Case... ...} Return defwindowproc (hwnd, message, wparam, lparam ); } |
3. Window Creation
You need to create a window by yourself. After the window is created, you will get the window handle (hwnd) returned by the system. Subsequent operations on the window are performed on the handle.
1. Registration window class
Before creating a window, you need to define the relevant properties of the window. The most important thing is to associate the defined message processing function with the window. Other properties include menus and icons.
This attribute is completed by specifying the "window class.
For self-built windows, this "window class" needs to be developed by yourself, that is, you need to fill in a wndclass structure and then register it with the system.
For some special windows, such as buttons, their behaviors are defined by the system. Therefore, you do not need to register them by yourself. Just use the corresponding "window class" name.
2. Create a window
When a window is created, the registered "window class" name is passed in as a parameter.
In this way, when there is a message for this window, the message processing function specified in the window class will be called and processed in it.
Iv. Message Loop
The system will put the messages for this program into the program's "message queue" in sequence, and the program will retrieve the messages in sequence and distribute them to the corresponding window.
Therefore, after a window is created, a loop is entered.
In a loop, messages are taken out and sent, until the acquired messages exit the message.
After the loop exits, the program ends.
# Include "stdafx. H" # Include <windows. h>// 1. Message Processing functions // Parameter: Window handle, message, Message Parameter, Message Parameter Lresult callback wndproc (hwnd, uint message, wparam, lparam) { // Process messages of interest Switch (Message) { Case wm_destroy: // When the user closes the window, the window is destroyed, the program needs to end, and an exit message is sent to exit the message loop. Postquitmessage (0 ); Return 0; } // Default processing functions provided by the system for other messages Return: defwindowproc (hwnd, message, wparam, lparam ); } // 2. Main Functions of the application // Parameters: instance handle, the handle of the previous instance, command line parameters, and window display mode Int winapi winmain (hinstance, hinstance hprevinstance, Pstr szcmdline, int icmdshow) { // 1. register the window class Static tchar szappname [] = text ("hellowin"); // window class name // Customize the "window class" Structure Wndclass; Wndclass. Style = cs_hredraw | cs_vredraw; Wndclass. lpfnwndproc = wndproc; // correlation Message Processing Function Wndclass. cbclsextra = 0; Wndclass. cbwndextra = 0; Wndclass. hinstance = hinstance; // instance handle Wndclass. hicon = loadicon (null, idi_application); // icon Wndclass. hcursor = loadcursor (null, idc_arrow); // cursor Wndclass. hbrbackground = (hbrush) getstockobject (white_brush); // painter Wndclass. lpszmenuname = NULL; Wndclass. lpszclassname = szappname; // Class Name // Register If (! Registerclass (& wndclass )) { MessageBox (null, text ("registerclass fail! "), Szappname, mb_iconerror ); Return 0; } // Create a window Hwnd; Hwnd = createwindow (szappname, // window class name Text ("the hello program"), // window title Ws_overlappedwindow, // window style Cw_usedefault, Cw_usedefault, Cw_usedefault, Cw_usedefault, Null, Null, Hinstance, // instance handle Null ); Showwindow (hwnd, icmdshow ); Updatewindow (hwnd ); // Message loop MSG; While (getmessage (& MSG, null, 0, 0) // retrieve the message from the Message Queue { Translatemessage (& MSG); // convert the message Dispatchmessage (& MSG); // send a message } Return msg. wparam; } |