Knowledge point:
1. Create a window Program
2. Window Registration
3. Registration of system Window classes
4. register the global window class of the application
5. Style of window class
6. Window search process
7. Related APIs
8. Create a window
9. Create a subwindow
10. window class and additional window data
11. Windows Message Mechanism
12. What is a message?
13. Window processing functions and messages
14. Message-related functions
15. common messages for Windows
16. Message acquisition
17. Message sending
18. Message Classification
19. Message Queue
20. Messages and message queues
21. Message acquisition
22. getmessage/peekmessage order
23. Message sending
Body:
1. Create a window Program
Procedure for creating a window program:
1. Define the winmain entry function
2. Define the window processing function windowproc.
3. register the window class registerclass
4. Create a window: createwindow
5. display window
ShowWindow/UpdateWindow
6. Message Loop
GetMessage
TranslateMessage
DisptachMessage
7. Message Processing
2. Window RegistrationThe window class contains the data structure of window parameters. Each window has a window class and creates a window based on the window class. Each window class has a name, which must be registered with the system before use. Window Class Classification-System window class the window class defined by the system can be directly used by all applications. -The global window class of the application is defined by the user. All modules of the current application can be used. -The application's local window class is defined by the user. This module can be used in the current application.
3. Registration of system Window classesYou can directly use the window class without registration. The system has defined the corresponding name, for example, button-button edit box-Edit
4. register the global window class of the applicationRegisterclass/registerclassex atom registerclass (const wndclass * lpwndclass // window class data); after successful registration, a digital ID is returned. Atom registerclassex (const wndclassex * lpwcx // window class data );
typedef struct _WNDCLASSEX {
Uint cbsize; // the size of the struct.
Uint style; // the style of the window class
Wndproc lpfnwndproc; // window processing function
Int cbclsextra; // additional data of the window class
Int cbwndextra; // additional window data
Hinstance; // instance handle of the current Module
Hicon; // window icon handle
Hcursor; // mouse handle
Hbrush hbrbackground; // specifies the paint brush handle used to draw the window background.
Lpctstr lpszmenuname; // resource ID String Of The Window Menu
Lpctstr lpszclassname; // window class name
Hicon hiconsm; // small icon handle of the window
} WNDCLASSEX, *PWNDCLASSEX
To register the global window class of the application, you must add cs_globalclass in the window class style, for example, wndclassex wce = {0}; wce. style = cs_globalclass; the local window class of the application does not add the cs_globalclass style when registering the window class.
5. Style of window classCs_globalclass-global window class of the application cs_bytealignclient-horizontal position of the window client area is aligned with the horizontal position of the cs_bytealignwindow-the horizontal position of the window is aligned with the horizontal position of cs_hredraw-when the window changes horizontally, redraw cs_vredraw in the window-when the window changes vertically, redraw cs_classdc in the window-all windows of this type have the same drawing (DC) device cs_parentdc-this type of window, cs_owndc, a drawing (DC) device that uses its parent window, uses its own drawing (DC) device cs_savebits-to allow the window to save the map (Bitmap ), improve the drawing efficiency of the window, but the memory-consuming resources cs_dblclks-allow the window to receive the left mouse click and double-click cs_noclose-the window does not close the button 6. window class search process 1) the system searches for partial window classes in the application based on the incoming window class name. If 2 is found, if Execution 3 not found. 2) Compare the local window class with the hinstance variable passed in when creating the window. If it is found that the created and registered window classes are in the same module, the create window will return. If not, continue with step 3. 3) in the global window class of the application, if found, execute 4. If not found, execute 5. 4) use the information of the found window class to create a window and return. 5) search in the System window class. If you find the create window, return. Otherwise, the create window fails.
7. Related APIsRegisterclass/registerclassex register getclassinfo to obtain information unregisterclass uninstall
8. Create a window
CreateWindow/CreateWindowEx
HWND CreateWindowEx(
DWORD dwexstyle, // extended window style
Maid, // the name of the registered window class
Lptstr lpwindowname, // name of the window title bar
DWORD dwstyle, // basic window style
Int X, // horizontal coordinate position in the upper left corner of the window
Int y, // vertical coordinate position in the upper left corner of the window
Int nwidth, // The window width
Int nheight, // The window height
Hwnd hwndparent, // The parent window handle of the window
Hmenu, // Window menu handle
Hinstance, // application instance handle
Lpvoid lpparam // additional parameters when the window is created
); Return window handle after creation
9. when creating a child window, you must set the parent window handle creation style to add ws_child | ws_visible 10. when the window class and the additional data of the window are used to register the window, you can set the size of the two data memory spaces. Int cbclsextra; // additional data size of the window class int cbwndextra; // additional data size of the window class provides space for window classes and windows to store their own data. 11. Windows message mechanism program execution mechanism process driver-the Program Execution process is performed in a predetermined order. Event driver-the execution of the program is unordered. You can trigger the corresponding event randomly as needed. The Win32 window program is executed in event-driven mode, that is, the message mechanism.
12. What is a message?When the system Notification window works, messages are sent to the window. Message composition: Window handle, Message ID, two parameters, the time when the message is generated, the cursor position when the message is generated
13. Window processing functions and messagesEach window must have a window handler. Lresult callback windowproc (hwnd, // window handle uint umsg, // Message ID wparam, // Message Parameter lparam // Message Parameter); when the system notifies the window, the window handler function is called and the Message ID and parameter are passed to the window handler function. For messages not processed in window processing functions, use the default window processing functions, such as defwindowproc.
14. Message-related functions1) getmessage-get the message. Bool getmessage (lpmsg, // store the obtained message buffhwnd hwnd, // window handle uint wmsgfiltermin, // obtain the minimum iduint wmsgfiltermax of the message // obtain the maximum ID of the message ); lpmsg-when a message is obtained, the parameters of the message are stored in the MSG structure. Hwnd-gets the message of the window specified by hwnd. Wmsgfiltermin and wmsgfiltermax-only messages within the message range specified by them can be obtained. If they are both 0, there is no range. 2) translatemessage-the translation message. Translate a key message into a character message. Bool translatemessage (const MSG * lpmsg // address of the message to be translated); check whether the message is a key-pressed message. If it is not a key-pressed message, no processing is performed and the execution continues. 3) dispatchmessage-send a message. Distribute the message to the window processing function of the message. Lresult dispatchmessage (const MSG * lpmsg // message to be distributed );
15. common messages for WindowsWm_destroy-message when the window is destroyed, no message parameter. It is often used to deal with the aftermath before the window is destroyed, such as resources and memory. Wm_syscommand-system command message. This message is received when you click the maximum, minimum, and close commands in the window. When the window is closed, you are often prompted to process it. Wparam-specific commands, such as disabling SC _close, etc. lparam-loword low-horizontal position hiword high-vertical position wm_create-this message is received before the window is created successfully. It is often used to initialize window parameters and resources, including creating subwindows. Wparam-do not use lparam-is a pointer to the createstruct structure, saving 12 parameters in createconwex. Wm_size-after the window size changes, wm_size is received. It is often used to adjust the layout of each part of the window after the window size changes. Wparam-reason for window size change. Lparam-change the size of the customer area loword-the width of the change hioword-the height of the change wm_quit-is used to End message loop processing. The parameter passed by the wparam-postquitmessage function. Lparam-not used when getmessage receives the message, it returns false, ends the while processing, and exits the message loop. Wm_paint-drawing message Keyboard Message mouse message timer message
16. Message acquisitionGetmessage-gets a message from the system, removes the message from the system, and blocks the function. When the system has no message, getmessage will wait for the next message. Peekmessage-obtain a message from the system in the view mode. You can remove the message from the system without blocking the function. If the system has no message, false is returned and subsequent code is executed. Bool peekmessage (lpmsg, // message informationhwnd hwnd, // handle to windowuint wmsgfiltermin, // first messageuint wmsgfiltermax, // last messageuint wremovemsg // remove the identifier );
17. Message sendingSendmessage: sends a message and waits for the message processing result. Postmessage-post a message, which is returned immediately after being sent without waiting for the message execution result. Bool sendmessage/postmessage (hwnd, // uint MSG, // message idwparam wparam, // Message Parameter lparam // Message Parameter );
18. Message Classification1. The system message-ID range is 0-0x03ff, which is defined by the system and can be directly used in programs. 2. The custom message ID range is 0x0400-0x7fff, which is defined by the user to meet the user's needs. The user sends the message and responds to it. Custom message macro: Message used for communication between wm_user 3 Application message-ID range: 0x8000-0 xbfff. Application message macro: wm_app 4 the system registers the message-ID range 0xc000-0 xFFFF in the system and generates the corresponding message. Then, you can use the message in each program.
19. Message QueueA message queue is a queue used to store messages. messages are first in and out of the queue. All window programs have message queues. The program can obtain messages from the queue. Message Queue type system message queue-A Message Queue maintained by the system. Stores messages generated by the system, such as the mouse and keyboard. Program message queue-A Message Queue belonging to each application (thread. Maintained by applications (threads. Message Queue relationship 1 when the mouse or keyboard generates a message, the message is stored in the system message queue 2 the system will find the message queue in the corresponding Window Based on the stored message. 3. deliver the message to the Message Queue of the program.
20. Messages and message queuesBased on the relationship between messages and message queues, messages are divided into two types: queue message-message sending and retrieval, which are completed through message queues. Non-queue message-message sending and retrieval are completed by directly calling the message window. Queue message-after a message is sent, it is first placed in the queue and then obtained from the queue through message loops. Getmessage-get a message from the Message Queue postmessage-deliver the message to the Message Queue common queue messages: wm_paint, keyboard, mouse, and timer. Non-queue message-when sending a message, first look for the window processing function of the message receiving window, and directly call the processing function to complete the message. Sendmessage-send the message directly to the processing function of the window and wait for the processing result. Common messages: wm_create and wm_size.
21. Message acquisitionThe message loop getmessage/peekmessage gets the message from the program's message queue. Translatemessage checks the obtained message. If a key message is found, a character message is generated and put into the program message queue. Dispatchmessage finds the window processing function based on the message, calls the window processing function, and completes message processing.
22. getmessage/peekmessage order1. query messages in the Program (thread) Message Queue. If a message exists in the queue and check whether the message meets the specified conditions (hwnd, ID range), the message will not be retrieved if the conditions are not met, otherwise, the message is returned from the queue. 2. If the program (thread) message queue has no message, obtain the message belonging to this program from the system message queue. If the current message in the system queue belongs to this program, the system will forward the message to process 3. If there is no message in the system message queue, check the area to be re-drawn in the current window, if you find a region to be drawn, the wm_paint message is generated and the message is returned for processing. 4. If the area is not re-drawn, check that if the timer has a timer at that time, wm_timer will be generated, and the processing will be returned for execution. 5. If no timer is available, sort program resources and memory. 6. getmessage will continue waiting for the next message. Peekmessage returns false, giving control of the program. NOTE: If getmessage is obtained as wm_quit, the function returns false.
23. Message sending1. sendmessage sends a message to the specified window and waits for the other party to process the message. Then, the message execution result is used to send non-queue messages. 2 postmessage: place the message in the message queue and return immediately. It is used to send the message in the queue. You cannot determine whether the message is processed by the other party.
2. Window and message