Windows SDK Notes (1): Basic Structure of Windows Programs

zhaozj2021-02-17  71

1. Overview of the Windows program has relatively fixed structures. For the writer, there is no need to write the entire process, and most of the process is completed by the system. Just fill in the small part of the customer to the customer in a certain format. The required completion is: the definition of the window class, the creation of the window, writing, message loop.

Second, the message processing function Windows program is an event-driven, for a window, most of its routine maintenance is maintained by the system. There is a message handler without a window. In the message processing function, the incoming message is processed. There is also its own default message processing function in the system.

The client writes a message processing function, and the message handler is associated with the window before the window is created. Thus, whenever there is a message generation, this message processing function will be called. Normally, customers do not handle all messages, but only the messages that you are interested in, others send back to the system's default message processing function.

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

{

Switch (Message)

{

Case ...

...

Case ...

...

}

Return DefWindowProc (Hwnd, Message, WPARAM, LPARAM);

}

Third, the establishment of the window

Customers need to create their own window. After the establishment, the window handle (HWND) returned by the system is obtained, and the subsequent operation is performed for the handle.

Register window class

Before establishing a window, you need to develop the related properties of this window, the most important thing is to associate your own message processing function and the window, and other properties include: menu, icon, etc.

This property specified step is done by specifying "window classes".

For your own window, this "window" needs you to set it yourself, that is, you can fill a WNDCLASS structure and register with the system.

For some special windows, such as buttons, such as buttons, such as buttons, their behavior is the system, so they don't need to register themselves, and use the corresponding "window class" name directly.

2. Create a window

When establishing a window, the registered "Window Class" name is incorporated into the parameter.

Thus, when there is a message for the window, the message processing function specified in the Window Class will be called in which the processing is obtained.

Fourth, message circulation

The system will be placed in the "Message Queue" of this program to the "Message Queue" of the program, and the message is taken out by the programself, and in the distributed window.

Therefore, after the window is created, it will enter a loop.

In the loop, remove the message, send a message, loop reciprocate, until the result is an exit message.

After the loop exits, the program ends.

#include "stdafx.h"

#include

// 1, message processing function

// Parameter: window handle, message, message parameter, message parameters

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

{

// Test the message of interest

Switch (Message)

{

Case WM_DESTROY:

// When the user closes the window, the window is destroyed, the program needs to end, send out the message to exit the message loop

PostquitMessage (0);

Return 0;

}

/ / Other messages are handed over to the default processing function provided by the system

Return :: DefWindowProc (Hwnd, Message, WParam, Lparam);

}

// Second, the application main function

/ / Parameter: Equity handle, handle of the previous instance, command line parameters, window display mode int WinApi WinMain (Hinstance Hinstance, Hinstance Hprevinstance,

PSTR SZCMDLINE, INT ICMDSHOW)

{

// 1. Registration window

Static tchar szappname [] = text ("hellowin"); // window class name

/ / Customize the "window class" structure

WNDCLASS WNDCLASS;

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW;

WNDCLASS.LPFNWNDPROC = WndProc; // Related message processing function

WNDCLASS.CBCLSEXTRA = 0;

Wndclass.cbWndextra = 0;

WNDCLASS.HINSTANCE = Hinstance; // Example handle

WNDCLASS.HICON = Loadicon (NULL, IDI_Application); // icon

WNDCLASS.HCURSOR = loadingcursor (null, idc_arrow); // cursor

Wndclass.hbrbackground = (hbrush) getStockObject (White_brush); // Handbrush

WNDCLASS.LPSZMENUNUNAME = NULL;

WNDCLASS.LPSZCLASSNAME = SZAPPNAME; // class name

//registered

IF (! registerclass (& wndclass))

{

MessageBox (NULL, Text ("RegisterClass Fail!"),

Szappname, MB_ICONEROR);

Return 0;

}

// Create a window

Hwnd hwnd;

HWND = CREATEWINDOW (SZAPPNAME, / / ​​window name

TEXT ("The Hello Program"), // Window Title

WS_OVERLAPPEDWINDOW, // Window style

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

Hinstance, // Example handle

NULL);

ShowWindow (hwnd, icmdshow);

UpdateWindow (HWND);

// Message loop

MSG msg;

While (GetMessage (& MSG, NULL, 0, 0)) // Take a message from the message queue

{

TranslateMessage (& MSG); // Conversion Message

DispatchMessage (& MSG); // Distribute news

}

Return msg.wparam;

}

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

New Post(0)