Preliminary exploration of Windows SDK programming

zhaozj2021-02-16  53

Chapter 1 Framework for Windows Programs

Important: Program's main function, message processing function

The Windows program has a relatively stable structure, that is, the Windows program has a certain framework. The programmer to do is to populate this frame with a specific content.

The main function of the Windows program: This function is like the function main () function of the program when we learn C language, and he represents the entrance of the program. But this function seems to be more comparable than main (). If you see this function, you will definitely feel a mist, but don't take a look at the meaning of each parameter of this function, you can't do it at the beginning.理 理 他们) 是 是 是 为 为 为 为 为 类似 类似 类似 类似 类似 类似 类似 类似 类似 类似 类似 不同 但 但 但 但 不同 但 但 不同 不同 不同 不同 不同 不同 不同 不同 于 于 不同 不同 于 不同 不同 不同 于 于 于 于 不同 于 于 于 不同 不同You can find our current programs through Hinstance. HPREVINSTANCE is the handle of the previous instance. Szcmdline: is the command line parameter, ICMDSHOW is a window display mode. Now we don't have to clear the specific meaning of each parameter. In the next learning, we use the most of our use is the parameter of Hinstance, but there are not many, and we use VC , no matter which program generation, this function is generally Automatic generated.

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, PSTR Szcmdline, ICMDSHOW)

The main function is the main part of the Windows program framework. Since the Windows program is based on messaging, the message processing function is also a part of the program framework. Speaking of the news, I think that the most difficult point from the basic C programming to Windows programming may understand the Windows message mechanism. Although it is mechanism but does not have to feel high-minded, it is actually very simple, give an example: in one There is a button in the application. When the button is pressed, the procedure we have habit will generate an action. Most things in such a simple process are the operating system to complete the operation, the detection button action, and will this The action is expressed: the button is pressed, and it will send a message "I am pressed" when it is pressed, "I was pressed", and there is a message processing function in the program, capturing this message, then turn Treat it: that is, call the related function.

Then let's take a look at the message processing function, the message processing function is actually a message loop. It is a callback function. No matter what the callback function is, only manage the solution, the function is defined by you and does not return you, is the operating system Call. Let's take a look at the prototype and structure of the message processing function:

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

{

Switch (Message)

{

Case ...

Case ... // These branch statements are used to handle different messages separately

}

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

/ * This function is the default message processing function of the system, that is, our window program's message processing function is only interested in the message, that is, the above case statement, for other messages, we use the system default message Processing function. Our message processing function is to register in our window main program, after registration as our custom message handler, the system's message has it handled. So, in the end of this function, the default function is to be used, otherwise the other news, this program cannot be processed. * /}

Understand the message processing function, we continue to see the main function of the program, it is the container of the program, the main form of the program is established, the program's message processing function is also set therein. There are two things we have to do in the main function function, one is the registration window class, and the other is the establishment window. The so-called registration window class is to fill in a WNDCLASS structure, our window is basically formed, then to register to the system (the registration process system also provides the corresponding function, "of course the technology is almost the same, you can write registration procedures.) After the window is registered, the next task is true to establish this window. When it is created, the name of the window class in front of the window is incorporated as a parameter, and of course the window has a custom message processing function, then this function is also necessary. Incoming.

In this way, we understand the framework of the Windows program, now I will give an example of a classic Windows SDK program to see our framework.

//example.cpp

#include

/ / Must include the header file

LRESULT CALLBACK WNDPROC (HWND, UINT, WPARAM, LPARAM)

Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, PSTR Szcmdline, ICMDSHOW)

{

Static tchar szappname [] = text ("hello"); // The name of the registered window class is "Hello"

WNDCLASS WNDCLASS; / / Define a window class

WNDCLASS.Style = CS_HREDRAW | CS_VREDRAW;

WNDCLASS.LPFNWNDPROC = WNDPROC; // This is associated with the defined message processing function

WNDCLASS.CBCLSEXTRA = 0;

Wndclass.cbWndextra = 0;

WNDCLASS.HINSTANCE = Hinstance; // Example handle

Wndclass.hicon = null; // window icon, we don't set it here

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

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

WNDCLASS.LPSZMENUNUNAME = NULL;

WNDCLASS.LPSZCLASSNAME = SZAPPNAME; // class name

IF (! RegisterClass (& Wndclass) // Register window

{

MessageBox (NULL, TEXT ("Window Registration Failed"), SZAPPName, MB_ICONERROR

Return 0;

}

// Start establishing a window below

Hwnd hwnd;

HWnd = CREATEWINDOW (Szappname, Text ("The Hello Program"),

CW_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT, CW_USEDEFAULT,

NULL,

Hinstance,

NULL);

ShowWindow (hwnd, icmdshow);

UpdateWindow (HWND);

// Next is the message loop, the program constantly tested the message from the message queue, let the message processing function processing

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

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

Return msg.wparam;

}

// Message Processing Function Definition

LRESULT CALLBACK WNDPROC (HWND, UINT, WPARAM, LPARAM) // In this example, we just handle simple messages

{

Switch (Message)

{

Case WM_DESTROY:

PostquitMessage (0);

Return 0;

}

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

}

To Be Continue ...

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

New Post(0)