Chapter 5 windows program design notes

Source: Internet
Author: User

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.

 

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.