"Windows program design" learning notes (three)

zhaozj2021-02-16  63

Chapter III Window and Message

The Windows programming is actually a process of creating a "window" object and processing window message, "Window" object not only refers to the application window, but also a number of dialogs, buttons, and radical buttons. In Windows, "Window" is ineffective. The window receives the user's input as "message", and "message" is the operating system sent to the application. For example, when we press the mouse to press a button, the Windows operating system tells the button according to this "message" of the mouse: "Hey! Button, I am now pressing you!", The button received this "message", think about it. "Then I will bed down." At this time, we saw the button was pressed by the mouse. It's that simple!

Each window is created based on a specific "window class", which identifies the "Window Process" in the window class, and the window is received and processed through the window process. The window process is actually a function, the parameter of the function contains several information, which we can use this information to perform the corresponding processing. One window can receive many messages that cannot be processed at the same time by the window, which is ascertained in order, one by a window, which is a "message series"; but there are exceptions Situation, like a person who is urgent to get a problem, some messages can send a window process directly without placing in the message queue (with the SendMessage function).

Now I know: Create a window First, you need to register a window class, you need a window process to handle the window message. The examples in the book describes what the windows and messages are in the end. This example occurs almost every Windows program, that is, each Windows program is expanded based on this program. Every detail that is familiar with it is very important. (Note in the following procedure almost explains the meaning of each statement)

#include

// State the window process function

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

/ / WIDOWS program entry function

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance,

PSTR SZCMDLINE, INT ICMDSHOW)

{

Static tchar szappname [] = text ("hellowin");

HWND HWND; / / Define a variable of a Windows handle

MSG msg; / / Define the structural variable of a message

/ / / 1 Step: Define the structure of a window class, 10 domains in this structure describe the feature of the window class, the following sequentially assigns these 10 domains

WNDCLASS WNDCLASS;

WNDCLASS.Style = CS_HREDRAW | CS_VREDREDRAW; // Based on the window style created by this window class

WNDCLASS.LPFNWNDPROC = WndProc; / / Pointer to the window process function

WNDCLASS.CBCLSEXTRA = 0; // Reserved Space of Window Class Structure

Wndclass.cbWndextra = 0; //

Wndclass.hinstance = Hinstance; // Windows program instant handle, this is provided by the compiler

/ / Set an icon for all windows established based on this window class

Wndclass.hicon = loading;

/ / Set a mouse for all windows established based on this window class

Wndclass.hcursor = loadingcursor (null, idc_arrow); // Specify the window background color based on this window class, use the handle of a white brush to achieve

WNDCLASS.HBRBACKGROUND = (Hbrush) getStockObject (White_brush);

/ / Specify the menu of the window class, assign a value in this program with NULL.

WNDCLASS.LPSZMENUNUNAME = NULL;

/ / Specify the name of the window class

Wndclass.lpszclassName = szappname;

/ / / Step 2: Register a window class to Windows, if you run the RegisterClass function under Win98, will return 0 indicate an error occurred

IF (! registerclass (& wndclass))

{

MessageBox (Null, Text ("This Program Requires Windows NT!"),

Szappname, MB_ICONEROR);

Return 0;

}

// Step 3: Create a window based on the above registered window class, allocate information on a memory saving window

HWND = CREATEWINDOW (the name of the szappname, // window class, based on this window class creation window

TEXT ("The Hello Program"), // Name of the window title bar

WS_OVERLAPPEDWINDOW, // Window style

CW_USEDEFAULT, / / ​​The X coordinate of the window position

CW_USEDEFAULT, // Y coordinate of the window position

CW_USEDEFAULT, // Window size wide

CW_USEDEFAULT, // window high

Null, // Handle of the Father Window

Handle of Null, // Window menu

Example handle of Hinstance, // Windows application

NULL); // Create a pointer to the parameter, you can use the data in it to access the program

// Step 4: // Step 4: Display the window, display the window on the display, and brush the window client area (UpdateWindow)

ShowWindow (hwnd, icmdshow);

UpdateWindow (HWND);

// This time the window has appeared on the display waiting for the user's keyboard and the mouse to perform the corresponding processing.

/ / / Step 5: Use the following loop to constraint messages in the message queue, and then Windows will process the message to the window process function.

While (GetMessage (& MSG, NULL, 0, 0)) // gets messages from the message queue

{

TranslateMessage (& MSG); // transmits the MSG structure to Windows, perform some keyboard conversion

DispatchMessage (& MSG); // Windows sends a message to the window process function (WNDPRO)

}

/ / The termination condition of the above loop is to obtain a WM_Quit message.

Return msg.wparam;

}

// Define the window process function, in the development, we mainly add the code that handles various messages in this function.

Lresult Callback WndProc (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {

HDC HDC;

Paintstruct PS;

RECT;

Switch (Message)

{

Case WM_CREATE: // When you create a window, Windows sends WM_CREATE to WNDPROC

Return 0;

Case WM_Paint: // When drawing a window customer area

HDC = BeginPaint (HWND, & PS);

GetClientRect (hwnd, & review);

DrawText (HDC, Text ("Hello, Windows 98!"), -1, & review,

DT_SINGLINE | DT_CENTER | DT_VCENTER);

Endpaint (hwnd, & ps);

Return 0;

Case WM_DESTROY: / / Clear window

PostquitMessage (0);

Return 0;

}

Return DefwindowProc (HWND, Message, WPARAM, LPARAM); // Message default processing

}

In fact, Windows encapsulates the message's processing mechanism, so that we can use the window class to identify a window Process Function (WndPro), you can handle the various messages sent to the window in this process function.

Finally, you need to pay attention to a window class to create multiple different windows.

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

New Post(0)