Chapter 1 windows program framework
Key points: main functions and message processing functions of the program
A Windows program has a relatively stable structure. That is to say, a Windows program has a certain framework. What programmers need to do is to fill the framework with specific content.
Main function of a Windows program: this function is similar to the main () function of the program when we learn C language. It represents the entry of the program. However, this function seems more complicated than main (). If it is the first time you see this function, it will certainly feel confused, but it doesn't matter. Let's take a look at the meaning of each parameter of this function, (In fact, at the beginning, we can ignore their meaning.) hinstance is the handle of this instance. The handle can be understood as the alias for operating system management to use our applications, it is similar to a pointer, but it is different from a pointer. Its meaning is only known to the operating system, that is, the operating system can find our current program through hinstance. Hprevinstance is the handle of the previous instance. Szcmdline: it is a command line parameter, and icmdshow is a window display mode. Now we do not need to clear the specific meaning of each parameter. In the next study, we will use the hinstance parameter most, but not much. When we use VC ++, this function is automatically generated regardless of the program generation method.
Int winapi winmain (hinstance, hinstance hprevinstance, pstr sz1_line, int icmdshow)
The main function is the main part of the Windows program framework. Because Windows programs are message-based, message processing functions are also counted as part of the program framework. Speaking of messages, I think the most difficult thing to switch from basic C programming to Windows programming is to understand the Windows message mechanism. Although it is a mechanism, you don't have to think about it. It is actually very simple, for example, there is a button in an application. When the button is pressed, the program we are used to will generate an action, in such a simple process, most of the tasks are completed by the operating system for us. We can detect the button action and display it as follows: the button is pressed and up; when the button is pressed, it will send a message "I was pressed" to the program where it is located. The program has a message processing function that captures the message and then processes it: that is, call related functions.
Then let's take a look at the message processing function. The message processing function is actually a message loop and a callback function. What is a callback function, functions are defined by you rather than called by the operating system. The prototype and structure of the message processing function are as follows:
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Switch (Message)
{
Case...
Case... // These branch statements are used to process different messages separately.
}
Return defwindowproc (hwnd, message, wparam, lparam );
/* This function is the default message processing function of the system. That is to say, the message processing function of our window program processes only the messages we are interested in, that is, the above case statement, for other messages, we still use the default message processing function. Our message processing function is to be registered in the main program of our window. After registering as our custom message processing function, the system will process the message. Therefore, use the default function at the end of the function. Otherwise, the program cannot process other messages. */
}
After learning about the message processing function, let's continue to look at the main function of the program, which is the container of the program. The main form of the program should be created in it, and the message processing function of the program should also be set in it. In the main function, we need to do two things: one is to register the window class, and the other is to create a window. The so-called registration window class is to fill in a wndclass structure, even if our window is basically formed, and then we need to register with the system (the registration process system also provides the corresponding function, of course, you can also write the registration process yourself after the technology is almost done ). After the window is registered, the next task is to create the window. The name of the previously registered window class is passed in as a parameter. Of course, if the window has a custom message processing function, this function must also be passed in.
In this way, we have learned about the windows program framework. Now I will give another example of a typical Windows SDK program to look at our framework.
// Example. cpp
# Include // Be sure to include this header file
Lresult callback wndproc (hwnd, uint, wparam, lparam); // message processing function declaration, which is defined after the main function
Int winapi winmain (hinstance, hinstance hprevinstance, pstr sz1_line, int icmdshow)
{
Static tchar szappname [] = text ("hello"); // name of the registered window class, which is "hello"
Wndclass; // defines a window class
Wndclass. Style = cs_hredraw | cs_vredraw;
Wndclass. lpfnwndproc = wndproc; // The defined message processing function is associated here.
Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
Wndclass. hinstance = hinstance; // instance handle
Wndclass. hicon = NULL; // window icon, which is not set here
Wndclass. hcursor = loadcursor (null, idc_arror); // set the cursor
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush); // painter
Wndclass. lpszmenuname = NULL;
Wndclass. lpszclassname = szappname; // Class Name
If (! Registerclass (& wndclass) // registration window
{
MessageBox (null, text ("window registration failed"), szappname, mb_iconerror );
Return 0;
}
// Start to create a window below
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 );
// The next step is the message loop. The program continuously retrieves messages from the message queue and enables the message processing function to process the messages.
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 only process simple messages.
{
Switch (Message)
{
Case wm_destroy:
Postquitmessage (0 );
Return 0;
}
Return: defwindowproc (hwnd, message, wparam, lparam );
}
To be continued...
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 30234