Detailed analysis of basic Windows code

Source: Internet
Author: User
/* The following annotation functions are described in the Platform SDK documentation and declared in different header files, most of which are declared in winuser. h. */
# Include <windows. h>

Lresult callback wndproc (hwnd, uint, wparam, lparam); // Window Process;

Int winapi winmain (hinstance, hinstance hprevinstance,
Pstr szcmdline, int icmdshow) // program entry point;
Static tchar szappname [] = text ("hellowin ");
Hwnd; // window handle class hwnd;
MSG; // p54 message;
Wndclass; // p47 registers the window class to generate an object. explanation: the window class defines the common features of the window, so you can use the same window class to create many different windows.
Wndclass. Style = cs_hredraw | cs_vredraw;
// 1... See the horizontal direction of the cs_hredraw window and the vertical direction of the cs_vredraw window. These two identifiers ensure that the text string is still located in the center of the window after the window size is changed.
Wndclass. lpfnwndproc = wndproc;
// 2... See: The second function of this program. It processes all information of all windows created based on this window class. When using a function name like this in a statement in C language, the actual reference provides a pointer to the function. // Lpfn "Long pointer to function"
Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
// 3 and 4... The above two fields are used to reserve some extra space in the class structure and window structure saved in windows; the program can use the reserved space as needed, which is not used here, so the value is 0; CB "Count of bytes (number of bytes )"
Wndclass. hinstance = hinstance;
// 5... The above domain is the instance handle of the Program (it is also one of the winmain parameters)

Wndclass. hicon = loadicon (null, idi_application );
// 6... See loadicon for program use.
// If the first parameter is set to null, it is used to obtain the pre-defined (default) icon handle;
// If the custom icon is loaded (the icon should be saved in the disk. exe program file), this parameter should be set to the instance handle hinstance of the program;
// The second parameter icon. It indicates a pre-defined icon. this parameter is an identifier starting with IDI, which is defined in winuser. h;
// The loadicon function returns the handle of the icon.
Wndclass. hcursor = loadcursor (null, idc_arrow );
// 7... See loadcursor to load the mouse pointer for the program to use. (Refer to loadicon)
// The loadcursor function loads a pre-defined mouse cursor (named idc_arrow) and returns the cursor handle. The handle is assigned to the hcursor field of the wndclass structure.
// When the mouse cursor appears in the customer area of the window created based on this class, it becomes a small arrow.
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush );
// 8... Getstockobject is used to obtain a graphic object. In this example, it is used to obtain the brush for drawing the window background;
// Call this operation to return the handle of a paint brush ('white' at this time.
Wndclass. lpszmenuname = NULL;
// 9... Specify the window menu for the previous domain. Therefore, the program does not have an application menu, so this field is set to null.
Wndclass. lpszclassname = szappname;
// 10... The above sentence is a class name. The Applet Class name can be the same as the program name, that is, the string "hellowin" stored in the szappname variable ";
// Whether the preceding string is composed of ASCII characters or Unicode characters depends on whether the Unicode identifier is defined;

If (! Registerclass (& wndclass) // registerclass is a window class registered for the program window. This function has only one parameter, that is, a pointer to the wndclass structure.
MessageBox (null, text ("this program requires Windows NT! "),
Szappname, mb_iconerror); // MessageBox is a message box;
Return 0;

// As follows: createwindow creates a window based on the window class, and returns the handle of the created window, which is stored in the variable hwnd.
Hwnd = createwindow (szappname, // window class name is the name of the window class registered by the program; this is how to associate the window we are creating with a window class.
Text ("the hello program"), // window caption (title, description)
Ws_overlappedwindow, // window style (go to the definition to see): title bar, menu box, zoom in and close the icon, and the border around the window size. // "| Ws_vscroll | ws_hscroll". With this addition, you can use the vertical scroll (ws_vscroll) and horizontal scroll (ws_hscroll). // ws 'window style' and vertical 'vertical '; horizontal 'horizontal ';
Cw_usedefault, // The Four cw_usedefault values under the initial X position indicate that the Windows Default size is used;
Cw_usedefault, // initial y position
Cw_usedefault, // initial X size
Cw_usedefault, // initial y size
Null, // parent window handle when you create a "Highest Level" window, such as an application window, the parameter of "parent window handle" is set to null.
Null, // Window menu handle, because the window does not have a menu, the "Window menu handle" is set to NULL;
Hinstance, // program instance handle "program instance handle" is set as an instance handle, which is passed to this program as a winmain parameter;
Null); // The creation parameters "create parameter" pointer is set to null. You can use this pointer to access the data in the program to be referenced later;
// Showwindow displays the window on the screen. There are two parameters: 1. Window handle; 2. It is the icmdshow parameter passed to winmain to determine how to display the window on the screen at first.
// The second parameter: 1. If the window is displayed as normal, sw_shownormal is the one received from winmain and passed to showwindow;
// 2. Maximize, It is sw_showmaximized; 3. If it is only displayed on the taskbar, It is sw_showminnoactive.
Showwindow (hwnd, icmdshow );
// If the second parameter is sw_shownormal, the client area of the window is overwritten by the background brush defined in the window class. The function calls updatewindow, causing the customer region to be drawn.
Updatewindow (hwnd); // indicates that the window refreshes itself. It sends a wm_paint message to the following Window Process (namely, the wndproc function.

// After updatewindow is called, the window appears on the display. The program must read the data entered by the user's keyboard and mouse. Windows maintains a "message queue" for each currently running Windows program"
// After an input event occurs, Windows converts the event to a message and puts the message in the program message queue.
// The program extracts messages from the message queue by executing a code called "message loop. As follows:
While (getmessage (& MSG, null, 0, 0) // getmessage obtains a message from the message queue. MSG is the 'message' type defined earlier, for details about its struct, see the definition of MSG;
{// Null, 0, 0 indicates that the program receives all messages from all the windows it creates. For details, see p55.
Translatemessage (& MSG); // translatemessage transmits the MSG structure to Windows and converts some keyboard information. (Chapter 6 will discuss it in depth)
Dispatchmessage (& MSG); // dispatchmessage sends the message to the window;
Return msg. wparam;

// The above are all preparations, and the actual actions occur in the window process. The window process determines what is displayed in the customer area of the window and how the window responds to user input.
// The window procedure can be named at Will (this program is wndproc). A Windows program can contain multiple window procedures, and a window procedure is always associated with a specific window class called registerclass registration.
// Generally, the program does not directly call the window process. Windows calls the window process directly. By calling the sendmessage function, the program can directly call its own window process.
Lresult callback wndproc (hwnd, uint message, wparam, lparam) // The four parameters are the same as the first four fields of the message. For more information, see MSG definition and P56.
Paintstruct pS;

Switch (Message)
Case wm_create: // acts as the second parameter of the wndproc Function
Playsound (text ("hellowin.wav"), null, snd_filename | snd_async );
// As shown above: playsound plays a sound file. The second parameter is used only when the sound file is a resource.
Return 0; // The window must return 0 when processing the message.

Case wm_paint:
// Invalidaterect (hwnd, null, true );
// If you plot outside the rectangle when processing the wm_paint message, you can call it before calling beginpaint; it makes the entire customer area invalid and clears the background;
// However, if it is set to false, the background will not be wiped out, and the original items will be kept in the original place; For details, see the entire page of p71;

HDC = beginpaint (hwnd, & PS );
// The processing of wm_paint always starts from the call of beginpaint (the function for drawing the start window). The second parameter is the structure pointer pointing to the paintstruct type.
Getclientrect (hwnd, & rect );
// Getclientrect obtains the size of the client area of the window. The second parameter is a pointer pointing to the rect rectangle structure.
// The rectangle structure has four long fields: left, top, right, and buttom (bottom ).
// Getclientrect sets these four fields to the size of the window customer area. The left and top fields are usually set to 0, and the right and buttom fields are set to the width and height of the customer area (in pixels );

Drawtext (HDC, text ("Hello, Windows 7! "),-1, & rect,
Dt_singleline | dt_center | dt_vcenter );
// Display the text string (that is, the output is text );
// Five parameters: 1. the device description table handle returned from beginpaint; 2. The output text; 3. The-1 parameter indicates that the text string ends with byte 0;
/5. It is a series of BITs, all of which are in winuser. h. It indicates that the text must be displayed on one line. Both the horizontal and vertical directions are in the rectangle area specified by the 4th parameters. Therefore, the output text is displayed in the center of the customer area;
Endpaint (hwnd, & PS); // endpaint End Window painting; the second parameter is a structure pointer pointing to the paintstruct type (same as above: beginpaint function ).
Return 0;

Case wm_destroy:
Postquitmessage (0); // postquitmessage inserts an "quit" message in the message queue;
Return 0;
Return defwindowproc (hwnd, message, wparam, lparam); // defwindowproc executes the Default Message Processing (that is, messages that are not processed in the window process are transmitted to it );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.