"Windows program design" reading notes

xiaoxiao2021-03-06  21

Window and message

See the money

2005-3-21

In Windows, we create the application contains this object, which is displayed as a rectangular area on the screen, contains the title bar, menu, or toolbar, scroll bar, etc. You can interact directly with it through the mouse and keyboard.

The figure below describes a component of a typical window:

We can drag the title bar to move the window on the screen; you can click Maximize Button to make the entire window full screen; minimize button (Minimize Button) can make the entire window into an icon; Click Close Button to turn off the window or even the application; you can perform the previous options in the leftmost system menu (System Menu); you can change the window size of the Border (SIZING BORDER); you can choose the menu bar (Menu) Bar 's menu items perform various predefined operations; you can drag the scroll bar to browse different zones of the client area.

We call the MessageBox function created dialog that is actually a limited window. Not only, the buttons of the dialog surface and other types of buttons such as single-choice buttons, check boxes, list boxes, scroll bars, text boxes are actually windows, just usually call "Sub-window" or "control windows "Or" Sub Window Control ".

All windows are created on the basis of "Window". "Window" is an abstract concept to define the basic appearance and behavior of a type window object. Using window classes can create multiple windows based on the same window class, and use the same window process, there are basicly consistent windows appearance.

In Windows, a variable naming convention called "Hungarian Specification" is generally used, and the variable name begins with one or more lowercase letters, which represent the data type of the variable. Understand this, help us understand the parameters meaning of the Windows API function.

Call the registerclass to register a window class, which only needs a parameter, a structural pointer to the type WNDClass. Structural prototype declarations are as follows:

Typedef struct {

Uint style; // style

WndProc lpfnWndproc; // window process

INT CBCLSEXTRA; / / Extra class space

INT CBWndextra; // Extra window space

Hinstance hinstance; // instance handle

Hicon Hicon; // icon

Hcursor hcursor; // cursor

Hbrush hbrbackground; // Background painting brush

LPCTSTR LPSZMENUNAME; // Menu

LPCTSTR LPSZCLASSNAME; // class name

} WNDCLASS, * PWNDCLASS;

The two most important parameters in the WNDClass structure are the second and last one. The second parameter (lpfnwndproc) is all window procedure addresses based on this class creation window. The last parameter (LPSZCLASSNAME) is the text name of the window class.

The general feature of the window-defined window can call CREATEWINDOW to create a new window with different characteristics on the defined window class. The CREATEWINDOW function prototype declaration is as follows:

HWND CREATEWINDOW (LPCTSTR LPCLASSNAME, // class name

LPCTSTR LPWINDOWNAME, // Window Name

DWORD DWSTYLE, / / ​​Window Style

INT x, // left upper corner X coordinate

INT Y, // Left upper corner Y coordinate

INT nwidth, // width

INT nheight, // height

HWND HWNDPARENT, / / ​​Parent window handle

HMENU HMENU, // Menu Handle

Hinstance Hinstance, // Program Example Handle

Parameter values ​​in the lpvoid lpparam // WM_CREATE message structure

);

Create a general application main window, specifying the DWStyle style is WS_OVERLAPEDWINDOW. When you create a top-level window, the HWndParent parameter is set to NULL. The CREATEWINDOW call returns a window handle, and each created window corresponds to a handle.

The handle is very frequent in Windows. It is a 32-bit integer that represents an object. The program acquires the handle through the Windows function, use the handle in other Window functions to reference the object it represents. There is generally no need to pay attention to the actual value of the handle.

Call CREATEWINDOW just creates a window, you must call the showWindow and UpdateWindow functions on the screen on the screen. Both functions require the window handle returned by CREATEWINDOW, showWindow displays the window on the screen, and UpdateWindow is drawn by the window client area.

One thing is very confused, regardless of the operation of the user, the window will respond in time, how is this implementation?

After each operation, Windows sent a message to the program. This message describes a specific content. After the program receives the message, the window process associated with the target window is called, and finally returns the result. Each window created by the program is associated with a window process. The window process is actually a function, it has a fixed prototype, the declaration is as follows:

Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);

When a Windows program is executed, Windows creates a "message queue" for the program, which is used to store various messages sent to the window. After calling the CreateWindow function, start entering the message processing loop, distributes the message in the message queue to the target window associated window process.

The program takes out the message from the message queue by executing a piece called "message loop":

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

The message loop starts with the getMessage call, which removes a message from the message queue and stores in an MSG structure called MSG. The prototype of the MSG structure is as follows:

Typedef struct tagmsg

{

HWND HWND; // Target window handle

Uint message; // message ID

WPARAM WPARAM; // Message Auxiliary Parameters

LPARAM LPARAM; // Message Auxiliary Parameters

DWORD TIME; // Message Add in Message Queue Time

Point Pt; // Message Mouse coordinates when adding a message queue

} MSG, * PMSG;

As long as the Message value of the message from the message queue is not WM_QUIT (0x0012), getMessage returns a non-0 value. The WM_QUIT message returns GetMessage to cause the end of the message loop to end the application. TranslateMessage mainly performs some keyboard conversions. DispatchMessage is responsible for transmitting messages to the window process and calls the window process. After the window process call is completed, the next getMessage call is performed. The window process is typically called by the system itself. The program generally does not need to call the window process. With the SendMessage function, the program can also call the window process directly. The message ignored during the window provides default processing by DEFWINDOWPROC.

WM_PAINT messages are a very important message in Windows. When the part or all of the window customer area becomes "invalid", it must be refreshed, and the system will send this message to the program. In the following cases, the WM_PAINT message will be sent:

1. The window was originally created;

2. After the window moves or the size is changed;

3. The window is hidden after being replaced or the part covered by other windows is re-visible;

4. Call the InvalIDateERECT, INVALIDATERGN function;

5. Call the scrollwindow or scrolldc function scroll;

The following cases generally do not send a WM_PAINT message:

1. Cursor crossing the customer area;

2. Icon dragged the customer area;

3. Display dialog;

4. After the drop-down menu is released;

For the process of WM_Paint, almost always starting from a beginpaint call:

HDC = BeginPaint (HWND, & PS);

The end of an endpaint call:

Endpaint (hwnd, & ps);

In the BeginPaint call, if the client area background is not cleared, the brush specified in the HBackground parameter specified by the system using the WNDCLASS structure of the registration window class is restricted. BeginPaint calls make the customer area valid. Unable to use the device descriptap of the device returned from BeginPain to draw the table. EndPaint Release Device Description Table Handle, which is no longer valid.

Everything done by the Windows program is a message that is sent to the window process.

If the user clicks on the "Close" button, DEFWINDOWPROC has sent a WM_SYSCommand message to the window process after processing this mouse. The window process transmits the message to the DEFWINDOWPROC process, then DEFWINDOWPROC sends a WM_CLOSE message to the window procedure. The window process transmits the message to the DefWindowProc process, then DEFWINDOWPROC sends a WM_DESTROY message to the window procedure. After receiving the message, the window process calls PostQuitMessage Send a WM_QUIT to the Message Queue. The next time the getMessage returns 0. The last program ends.

The message can be divided into "enter the team message" and "do not enter the team message". The enter the team message is put into the program message queue by Windows. In the message loop of the program, re-returns and assign it to the window; if you do not enter the team, you are calling the window process directly.

General entering team messages are the results of user input. To hit the key (such as WM_KeyDown and WM_KEYUP messages), the characters generated by the keystroke (WM_CHAR), the mouse move (WM_MOUSEMOVE), the mouse button (WM_LButtondown) is given. The entry message also includes a clock message (WM_TIMER), refresh the message (WM_Paint), exits the message (WM_QUIT). Not entering the team messages are from calling a specific Windows function. When you call CREATEWINDOW, send a WM_CREATE message; send WM_SIZE and WM_SHOWWINDOW messages after calling ShowWindow; call UpdateWindWind to send WM_PAINT messages.

The message processing in the application must be performed in a manner, and cannot be implemented. When a message is handled during a window, the program will not be suddenly interrupted by other messages. Another message must be processed after a message must be processed.

The window process can be re-entry, that is, the window process can be nested.

转载请注明原文地址:https://www.9cbs.com/read-43279.html

New Post(0)