In July and July, we will learn MFC and follow sun Xin's video. However, we still cannot understand the basics of Windows programming. I finally re-read the C ++ basics in July 15, and today I re-read the windows programming basics. I finally understood it in the afternoon. It turned out to be so simple that we could feel the same.
My summary:
There are several steps to create a Windows Application
(1) window class definition (wndclass definition is implemented by assigning values to this structure)
It is equivalent to defining a window template.
(2) register the window class (implemented through registerclass, to be checked)
You must register before using the window class.
(3) create a window (implemented through createwindow)
It is equivalent to the window template defined in step 1 above to create a window
(4) display window (implemented through showwindow)
(5) Update window (implemented through updatewindow)
(6) message loop (implemented through getmessage and dispatchmessage)
The message mechanism of Windows is involved here. Let's briefly describe it:
In Windows, messages are first put into the message queue. The message loop of the application is to retrieve messages from the message queue of the application, and send the messages to the corresponding Window Function for further processing.
Wndproc in the window procedure function in the following program.
# Include "windows. H "lresult callback wndproc (hwnd, uint, wparam, lparam); // process int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {hwnd; // window handle MSG; // message wndclass; // window class/* window class definition is completed by assigning values to window class data structure wndclass */wndclass. style = cs_hredraw | cs_vredraw; wndclass. lpfnwndproc = wndproc; // defines the wndclass window processing function. cbclsextra = 0; wndclass. cbwndextra = 0; wndclass. hins Tance = hinstance; // handle of the current instance. The wndclass cannot be empty. hicon = loadicon (null, idi_application); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. lpszmenuname = NULL; wndclass. lpszclassname = "Hello win";/* window class registration. Windows provides some predefined window classes. The window class must be registered before use */If (! Registerclass (& wndclass) {MessageBox (null, "window registration failed! "," Hello win ", 0); Return 0;}/* create a window and implement */hwnd = createwindow (" Hello win ", "My window", ws_overlappedwindow, cw_usedefault, null, null, hinstance, null);/* display a window through showwindow () implement */showwindow (hwnd, sw_showmaximized); updatewindow (hwnd); // update the window, including the customer area of the window // enter the message loop: when the message retrieved from the application message queue is wm_quit, the loop while (getmessage (& MSG, null,) is exited {// translatemessage (& MSG ); // convert some keyboard messages to dispatchmessage (& MSG); // send messages to the window. Here is wndproc} return MSG. wparam;} // window procedure function definition lresult callback wndproc (hwnd, uint message, wparam, lparam) {Switch (Message) {Case wm_create: // return 0; break; Case wm_lbuttondown: MessageBox (null, "Hello, my visual C ++ world", "Greetings", 0) generated by the window creation ); return 0; break; Case wm_rbuttondown: MessageBox (null, "Fuck", "greeting", 0); Return 0; break; Case wm_destroy: postquitmessage (0); Return 0; break;} return defwindowproc (hwnd, message, wparam, lparam); // execute the default message processing}