Windows Programming (1): My explanation of the hellowin Program

Source: Internet
Author: User

In the past, some C ++ programs were written on black screens. Recently, I want to learn VC and write some programs with GUI. The first classic program to learn VC is hellowin. After reading the materials over the past few days, I finally had some understanding of this program. Share with you:

First of all, the difference between windows program design and the traditional C language or C ++ program design is that Windows message mechanism. What is the message mechanism? To put it simplyDon't call me. I will call you. Unlike traditional C language programs, it calls other functions in the main function. The practice is: when you send a message to an application (for example, click it with a mouse), you actually did not send a message to the application, but sent the message to the operating system. The system will first determine who your message is sent by calculating the mouse position, and then send the message to a specific application. After your application receives the message, it converts the message and returns it to the system. The system calls the callback function written in advance by the programmer to respond to your mouse clicks.

With this basic idea, let's look at the basic process of the program:

After declaring a callback function, winmain starts. The winmain function has roughly done a few tasks: defining and initializing a window, registering the window (registerclass) with the system, and creating a window (createwindow) in the memory ), display and update window. Then, enter the message loop and wait for the system to send a message to the program. If the message is received, send the message to the system through dispatchmessage. Then, the system calls the callback function wndproc to determine the message and respond.

Pay special attention to the following issues:

First, wndproc is not called in winmain. In the initialization window, the callback function is bound to winmain through lpfnwndproc. At the time of registration, the system also knows the relationship between the two.

Second, why is it processed in the callback function instead of directly processing messages in the message loop?

Under normal circumstances, each program has a message queue. When a message arrives, it reads the message from it and then processes it. However, this is not the case for some special messages, such as creat messages. After createwindow is executed, the system sends a creat message, and the message loop is not created yet! From this simple example, we can see that it is inappropriate to put all the message processing flows in the message loop. Therefore, Windows uses the callback function to process messages in a unified manner.

Many functions are commented out in the program:

First, set custom messages in the program. A custom message is also a number. Messages after wm_user are used to indicate custom messages. Messages previously defined by the system should not be changed at will. The message is sent by wm_lbuttondown, and the message generates a sound.

Second, the invalid partition in wm_lbuttondown will cause the window to be re-painted. If there is no playsound function in wm_lbuttondown and there is a function in wm_paint, clicking the left button will also cause a sound.

Third, the WINDOS system does not have a so-called double-click. Only when the left-click button is pressed, the right-click button is also pressed, or the right-click button is also pressed.

In short, this program is very playable, And you can fully understand when the message is sent through experiments.

Finally, I made a detailed comment on this program, and I am not very sure about some problems. If someone points out the problems, I am very grateful.

# Include <windows. h> # include <mmsystem. h> // It is equivalent to adding winmm in settings. lib # pragma comment (Lib, "winmm. lib ") // custom message # define wm_mymsg (wm_user + 100) // callback function declaration lresult callback wndproc (hwnd, uint, wparam, lparam ); int winapi winmain (hinstance, // the current instance handle hinstance hprevinstance, // The previous instance handle, which is usually null lpstr lpcmdline, // command line int ncmdshow) // display the status: maximize and minimize {// window class name static tchar szappname [] = text ("mywindowsprogr Am "); // handle hwnd; // message: the message struct contains: handle, message identifier, additional message, additional message, message delivery time, cursor position during message delivery // The message is entered by the system. MSG; // window class wndclass; // window style, which can be a combination of any class styles wndclass. style = cs_hredraw | cs_vredraw; // return to the window when the motion or size is adjusted // point to the window process. You must use the callback function to call the wndclass in the window process. lpfnwndproc = wndproc; // specify the wndclass in the window for allocating the number of extra bits. cbclsextra = 0; // specify the number of bits for the wndclass instance in the allocation window. cbwndextra = 0; // instance handle wndclass. hinstance = hinstance; // icon: Load icon function (application instance, icon type). The default icon wndclass is used here. hicon = loadicon (null, idi_application); // cursor: cursor Loading Function (application instance, cursor type). The standard arrow is used here. Wndclass. hcursor = loadcursor (null, idc_arrow); // Background: Brush Type White wndclass. hbrbackground = (hbrush) getstockobject (white_brush); // menu: wndclass is not found here. lpszmenuname = NULL; // string or button ending with 0: Here is a string indicating the window class name wndclass. lpszclassname = szappname; // The registration window if (! Registerclass (& wndclass) {return 0 ;}// create a window hwnd = createwindow (szappname, // window class name text ("My windows program "), // window tag ws_overlappedwindow, // window style: cw_usedefault, // X coordinate cw_usedefault, // y coordinate cw_usedefault, // width cw_usedefault, // height null, // The parent window handle is null, // The Window menu handle is hinstance, // The program instance handle is null); // create parameters // display window, the parameter is: Window handle, display status showwindow (hwnd, sw_showna); // update window updatewindow (hwnd); While (getmessage (& MSG, null,) // obtain the Message Parameter: pointing to the MSG structure, window handle (null Indicates any window of the calling thread). The first message, the last message {// The virtual key message is converted into a character message. The message is sent to the Message Queue of the calling thread, when the current thread calls the getmessage or peekmessage function, it is read to translatemessage (& MSG); // This function distributes a message to the window program. Generally, messages are obtained from the getmessage function. The message is distributed to the callback function. The function is to pass the message to the operating system, and then the operating system calls our callback function. That is to say, we process the message in the form process function. Dispatchmessage (& MSG);} // MSG. wparam is the exit message return MSG. wparam;} // callback function: defined by the programmer. The system calls the function. The first two are message parameters, and the last two are the product lresult callback wndproc (hwnd, uint message, wparam, lparam) {// HDC of the device content handle; // The paintstruct structure contains information for drawing the customer region. The first parameter is the paintstruct PS of the HDC class; rect; Switch (Message) {// create window message: After the createwindow function is called, the message will be sent and will not enter the message queue. Case wm_create: // The playback sound. (This parameter is generally null in the middle unless the snd_resource playback tag is used: if you click it twice in a row, the synchronization will play one by one; the asynchronous mode will stop the first sound at the second and directly play the second sound) // only WAV format can be played // playsound (text ("D:/videos/msg.wav"), null, snd_filename | snd_sync); Return 0; // draw window message case wm_paint: // The beginpaint function prepares the specified window drawing. // The parameter is the window handle and the drawing information pointing to the paintstruct structure. HDC = beginpaint (hwnd, & PS ); // obtain the coordinates of the customer zone. The parameter is the window for obtaining the coordinates and the pointer to the rect structure to store the coordinates getclientrect (hwnd, & rect ); // format the image in the specified area The text // parameter is the device content handle, the character to be displayed, the text length (-1 indicates that it is assumed to end with 0), the rect type address, indicates that the text will be formatted there. Format: the horizontal and vertical verticals are centered drawtext (HDC, text ("Hello, windows7! "),-1, & rect, dt_singleline | dt_center | dt_vcenter); playsound (text (" D:/videos/msg.wav "), null, snd_filename | snd_sync ); // endpaint (hwnd, & PS); Return 0; Case wm_lbuttondown: // the specified area becomes invalid. // playsound (text ("D: /videos/msg.wav "), null, snd_filename | snd_sync); // Changes the specified region to an invalid partition, and then automatically calls the paint function // invalidaterect (hwnd, null, false ); // send a message. The parameter is: Who the message is sent, what the message is sent, and two additional messages // sendmessage (hwnd, wm_mymsg, wparam, lparam); If (wparam & mk_rbutton) // At the same time, right-click playsound (text ("D:/videos/msg.wav"), null, snd_filename | snd_sync); Return 0; Case wm_rbuttondown: If (wparam & mk_lbutton) playsound (text ("D:/videos/msg.wav"), null, snd_filename | snd_sync); Return 0; Case wm_mymsg: playsound (text ("D: /videos/msg.wav "), null, snd_filename | snd_sync); Return 0; // close window message case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam); // execute the default message}

 

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: info-contact@alibabacloud.com 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.