1. Windows ApplicationsProgramOperating Mechanism
# Include <windows. h> # Include <Stdio. h> Lresult callback winexample1proc ( // Declare Window Process Functions Hwnd, uint umsg, wparam, lparam ); Int Winapi winmain ( // Win32 application entry function Hinstance, hinstance hprevinstance, lpstr lpcmdline, Int Ncmdshow) {wndclass wndcls; // Create a window class Wndcls. cbclsextra = 0 ; Wndcls. cbwndextra = 0 ; Wndcls. hbrbackground = (Hbrush) getstockobject (white_brush); wndcls. hcursor = Loadcursor (null, idc_cross); wndcls. hicon = Loadicon (null, idi_error); wndcls. hinstance = Hinstance; wndcls. lpfnwndproc =Winexample1proc; wndcls. lpszclassname = (Lpcwstr) " Example1 " ; Wndcls. lpszmenuname = NULL; wndcls. Style = Cs_hredraw | Cs_vredraw; registerclass ( & Wndcls ); // Registration window class Hwnd; // Create a window and define a handle to uniquely identify the window Hwnd = createwindow (lpcwstr)" Example1 " , (Lpcwstr) " A Windows application window " , Ws_overlappedwindow, 0 , 0 , 500 , 500 , Null, null, hinstance, null); showwindow (hwnd, sw_shownormal ); // Display and update window Updatewindow (hwnd); MSG; While (Getmessage (& MSG, null, 0 , 0 )) // Define message struct and perform message loop {Translatemessage ( & MSG); dispatchmessage ( & MSG );} Return MSG. wparam;} lresult callback winexample1proc ( // Compile Window Process functions for Message Processing Hwnd, uint umsg, wparam, lparam ){ Char Exchar2 [] = { " The first Windows Application " }; Switch (Umsg ){ Case Wm_char: Char Exchar1 [ 40 ]; Sprintf (exchar1, " The ASCII value of the input character on the keyboard is % d. " , Wparam); MessageBox (hwnd, (lpcwstr) exchar1, (lpcwstr) " Char " , 0 ); Break ; Case Wm_rbuttondown: MessageBox (hwnd, (lpcwstr) " Right-click and press " , (Lpcwstr)" Message " , 0 ); Break ; Case Wm_close: If (Idyes = MessageBox (hwnd, (lpcwstr) " Want to end? " , (Lpcwstr) " Tip: " , Mb_yesno) {destroywindow (hwnd );} Break ; Case Wm_destroy: postquitmessage ( 0 ); Break ; Case Wm_paint: HDC; paintstruct pS; HDC = Beginpaint (hwnd ,& PS); rect RT; getclientrect (hwnd, &RT); drawtext (HDC, (lpcwstr) exchar2, strlen (exchar2 ), & RT, dt_center); endpaint (hwnd, & PS ); Break ; Default : Return Defwindowproc (hwnd, umsg, wparam, lparam );} Return 0 ;}
From the above program structure, we can find that the Win32 application has a very clear main line: first enter the winmain function, then design the window class, register the window class, generate the window, and register the window, display the window, update the window, and enter the message loop. Route the message to the window function for processing. This is a typical internal operating mechanism for Windows applications.