Chapter 1 - Window and Messages
First, the Hellowin program is as follows: / * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------Hellowin.c - Displays "Hello, Windows 98!" In Client Area (c) Charles Petzold, 1998 --------- -------------------------------------------------- - * / #include
), // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, / / window menu handle hInstance, // program instance handle NULL); // creation parameters ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); while (GetMessage (& msg, NULL, 0, 0)) {TranslateMessage (& msg); DispatchMessage (& msg);} return msg.wParam;} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) {case WM_CREATE: PlaySound (TEXT ( " Hellion.wav "), NULL, SND_FILENAME | SND_ASYNC); RET URN 0; Case WM_Paint: HDC = Beginpaint (HWND, & PS); GetClientRect (hwnd, & rece); DrawText (HDC, Text ("Hello, Windows 98!"), -1, & Rect, Dt_SingLine | DT_Center | DT_VCENTER; Endpaint (hwnd, & ps); retrn 0; case wm_destroy: postquitMessage (0); return 0;} Return DEFWINDOWPROC (HWND, Message, WPARAM, LPARAM);} II, Windows and Message Mechanism
1, enter the team and not enter the team
Windows is an object-oriented architecture, Windows environment and applications are interacting through messages. After the Windows application starts execution, Windows creates a message queue (Message Queue "for the program to store the message sent to the program possible to create a message. The structure (MSG) of the message in the message queue is:
Typedef struct tagmsg {hwnd hwnd;
Uint Message;
WPARAM WPARAM;
LParam Lparam;
DWORD TIME;
Point Pt;
} MSG; where the first member variable is used to identify the window handle of the received message; the second parameter is the message identification number, such as WM_Paint; the specific meaning of the third and fourth parameters is related to the Message value, all Message parameters. The first four parameters are very important and often used, as for the two parameters, respectively indicate the time and cursor position of the mailing message, respectively (screen coordinates).
There are two ways to transfer messages to applications: one is "message queue" that is mailed (post) "to the application. This is "Entering Message", Win32 API has a corresponding function: postmessage (), this function is not waiting for the message to return; and the other is sent by the system to "send message" when the system is directly called "For the function of the specific window of the application, it belongs to" Do not enter the team message ". The corresponding function is SendMessage (), which must wait for the message to return.
The message of the team is basically the result of the user input, such as hit button (WM_KeyDown and WM_KEYUP message), the hike is generated (WM_CHAR), the mouse movement (WM_Mousemove) and the mouse button (WM_LBUTTONDOWN) are given. The entry message also includes a clock message (WM_TIMER), refresh message (WM_Paint), and exit message (WM_QUIT), and more.
Messages that do not enter the team are often coming from calling a specific Windows function. For example, when WinMain calls the CREATEWINDOWS function, Windows will create a form and send a WM_CREATE message to the window process in the process. When WinMain calls ShowWindow, Windows will send a WM_SIZE and WM_SHOWWINDOW message to the window process. Windows will send WM_PAINT to the window when WinMain calls UpdateWindow.
Windows calls window procedures, meaning that Windows itself has a message loop, which calls GetMessage to remove messages from the message queue, and send messages to the window with DispatchMessage. In addition to the message loop with the operating system itself, for each of the Windows applications that are being executed, the system creates a "message queue", which is the application queue to store the messages that the program may create. The application contains a code called "message loop" to retrieve these messages from the message queue and distribute them into the corresponding window function. Message loop code is a block similar to the principal function WinMain () in the application:
While (GetMessage (&& Msg, Null, Null, Null)
{file: // get a message from the message queue
TranslateMessage (&& Msg);
File: // Retrieve and generate character messages WM_CHAR
DispatchMessage (&& Msg);
FILE: / / Send the message to the corresponding window function
} This shows that the so-called "message loop" is actually a program loop.
2, the retrnight of the window process
Although the Windows program can perform multi-threaded, each thread is only for the window procedure to handle the message in the thread. That is, the message loop and window process are not executed concurrent. When the message loop accepted a message from the message queue, then call the DispatchMessage message to send to the window process until the window process returns the control to Windows, DispatchMessage can return. The window process can be called to the Windows window process to send another message function, and the window process must complete the processing of the second message before the function call returns. If the window process calls UpdateWindow Generates a WM_PAINT message, the window process must first process the WM_PAINT message, the UpdateWindow call can restore the control to the window process. Third, detailed description: 1. Windows.h this header file contains other Windows header files: WINDEF.H - Basic Type Definition Winnt.h - Support Unicode Type Definition WinBase.H - Core Function Winuser.h- - User Interface Function Wingdi.h - Graphic Debit Function 2, WinMain Function Description: Winmain Function is the entry point of the Windows program, as follows: int WinApi WinMain (Hinstance Hinstance, Hinstance HpreInstance, LPSTR LPCMDLINE, INT NSHOWCMD); WinApi - Many Windows function calls declaration to WinAPI actual situation is as follows: #define WinAPI _stdcall and the other is the four-parameter declaration of the Callback type: the first parameter: an instance handle, the handle is a 32-bit number, representing one Object. Second Parameters: 16-bit Windows Program can determine if its own other instances are running by checking the HPreInstance parameter. But in 32-bit Windows, it is discarded, all of which is the third parameter: the command line of the program. Fourth parameter: Point out how the program is originally displayed: maximize minimization, etc.
3. About the Hungarian name handle declaration as follows: Type Description Handle Universal Handle Type HWnd Identify a Window Object HDC Identify a device Object HCU I identify a menu Object HCURSOR I identify a cursor object HPEN I identify a brush object HPEN identify a brush object HFONT Identify a Font Object Hinstance Identify an Instance of an Application Module Hlocal Identifying a Local Memory Object Hglobal Identifying a Global Memory Object The following is some alphanumeric prefixes often used in Windows, and the data types represented by: Type Description B Bool, Boole ByTE Type C Char Type DW DWORD Type FN Function Type I Integer L Long Pointer NP Near (Short) P Pointer S String SZ The string of the end of / 0 ', Word Type X Short, used to represent the x coordinate, Y Short, PSTR Char * uint unsigned int 32 bits below when the Y coordinate is used: Prefix category CS type CW creation Window options DT Drawing text option IDI Chart ID Number ID Cocatric ID Number MB Message Box Options SND Sound Options WM Window Message WS Window Style 4, About Several Important Structure: MSG - Message Structure WNDCLASS - Window Class Structure Paintstruct - Draw Structure Rect - Rectangular Structure This chapter describes the MSG and WNDCLASS structure as follows: The MSG structure contains a complete information of a Windows message, defined in Winuser.h as follows: typedef struct tagmsg {hwnd hwnd; // Accept message window handle Uint Message; // Main message value, specific message content wPARAM WPARAM; // sub-message value, its specific meaning depends on the main message value lparam lparam; // sub-message value, its specific meaning depends on the main message value DWORD TIME; // message placement message The time POINT PT; // message is placed in the position of the mouse in the message queue} MSG; By the way, the Point is declared with the TypeDef struct tagpoint {int x; / * x coordinate * / int y; / * y coordinate * /} Point ;
WNDCLASS is defined in Winuser.h as follows: typedef struct tagwndclass {dword style; / * Window style * / wndproc * lpfnwndproc; / * window function * / int cbclsextra; / * Class variable Occupable storage * / int CbWndextra; / * Example Variable Occupation Storage Space * / Hinstance Hinstance; / * Defines the handle of the application instance of the class Hicon Hicon; / * Icon Object handle * / hcursor hcursor; / * Code of the cursor object Handle * / HBrush HbRBackground; / * The handle of the brush object of the user area * / LPCSTR LPSZMENUNAME; / * Identifies the string * / lpcstr lpszclassname of the menu object * / LPCSTR LPSZCLASSNAME; / * Identifies the character string of the name of the window class * /} WNDCLASS; 5, the registerclass function This function is only A parameter, returning 0 indicates an error by calling. 6. Create several functions of the window during the window. CREATEWINDOW (LPCSTR LPCLASSNAME, class name, specify the form class belonging to the window. LPCSTR LPWINDOWNAME, the name of the window, that is, the text displayed in the title bar. DWORD DWSTYLE, this window The style is described in detail later. INT X, the upper left corner of the window relative to the initial X coordinate of the upper left corner of the screen. INT Y, the upper left corner of the window relative to the initial Y coordinate in the upper left corner of the screen. Int nwidth, the width of the window. Int nHeiGHT, The height of the window. HWND HWNDPART, the handle of the parent window of a sub-window, or the handle of the owner window of the window, if there is no ownership or parent window, null. HMenu Hmenu, select a single handle, if null, use the class The menu defined in it. If a sub-window is created, this parameter is a sub-window identifier that uses this identifier to distinguish between multiple windows. Hinstance Hinstance, create an instance handle of the application of the window object. Void Far * LPPARAM creation The additional parameters specified during the window are generally set to null);
Window style Type Description: WS_Border Creates a Border Window WS_CAPTION Create a window WS_CHILDWINDOW (or WS_CHILD) with a title bar (not with WS_POPUP) WS_CLIPCHILDREN When drawing in the parent window, take the child window The area is cut out, ie the sub-window that is not related to each other in this area, not in the area covered by other sub-windows, only using WS_DISABLED to create an initial disabled window WS_DLGFrame with WS_CHILD WS_DLGFRAME Create a border box But the window WS_HSCROLL creates a window WS_VScroll with a horizontal roller bar WS_VScroll Creates a window of a vertical roller, creating a window that is initially icon, only using WS_Maximize to create a maximum size of the smallest size with WS_Maximize with WS_MAXIMIZE. Window (ie icon) WS_Maximizebox Create a window with high frame WS_MINIMIZEBOX Create a window WS_OVERLAPPED with a very small box WS_OVERLAPPED Create a overlapping window with header and border WS_POPUP Create a pop-up window, can't WS_CHILD is using the system option box with the system option box with the window WS_THICKFRAME with the title bar, and the user can directly zoom the window WS_Visible created an initial visible window ShowWindow function declaration: showWindows (hwnd, icmdshow) ICMDSHOW is WinMain accepts and passes, which contains a real way of forms such as: sw_shownormal or sw_showmaximized or sw_showminoactive (displayed on taskbar) When the second parameter is sw_shownormal, the UpdateWindow (HWMD) is called to redraw the customer area. This is done by issuing a WM_Paint message to the form process. 7. About the message loop function BOOL getMessage (LPMSG LPMSG, pointing to the far pointer to the MSG type variable, which contains data for a message retrieved from the application message queue. HWND hWnd, specify which window retrieval message, if HWND is NULL, retrieves all messages of the application that calls the function (no message belonging to other applications). UINT WMIN, UINT WMAX The following two basic parameters specify the message in WMIN and WMAX. If this Both parameters are zero, which retrieves all available messages.); Return values return zero values when retrieving WM_QUIT messages, returning non-zero values in other cases. An example of getMessage (& MSG, NULL, 0, 0) Windows is to remove the message from the message queue to populate the individual domains in the MSG.