Summary: This chapter mainly describes a typicalWindowsProgramComponents of the design! Understanding and understandingWindowsMessage-driven features. This chapter describes the various parts of a program from a typical example!
1 # Include <windows. h> 2 # Include <stdafx. h> 3 Lresult callback wndproc (hwnd, uint, wparam, lparam ); 4 Int Winapi winmain (hinstance, hinstance hprestacne, pstr sz1_line,Int Icmdline) 5 { 6 Static Tchar szappname [] = text ( " Hello World " ); 7 Hwnd; 8 MSG; 9 Wndclass; 10 Wndclass. Style = cs_hredraw | Cs_vredraw; 11 Wndclass. lpfnwndproc = Wndproc; 12 Wndclass. cbclsextra = 0 ; 13 Wndclass. cbwndextra = 0 ; 14 Wndclass. hinstance = Hinstance; 15 Wndclass. hcursor =Loadcursor (null, idc_arrow ); 16 Wndclass. hicon = Loadicon (null, idi_application ); 17 Wndclass. hbrbackground = (Hbrush) getstockobject (white_brush ); 18 19 Wndclass. lpszclassname = Szappname; 20 Wndclass. lpszmenuname = NULL; 21 If (! Registerclass (&Wndclass )) 22 { 23 MessageBox (null, text ( " The programe requires Windows NT " ), Szappname, mb_iconerror ); 24 Return 0 ; 25 } 26 Hwnd =Createwindow (szappname, 27 Text ( " The Hello world programe " ), 28 Ws_overlappedwindow, 29 Cw_usedefault, 30 Cw_usedefault, 31 Cw_usedefault, 32 Cw_usedefault, 33 Null, 34 Null, 35 Hinstance, 36 Null ); 37 Showwindow (hwnd, icmdline ); 38 Updatewindow (hwnd ); 39 While (Getmessage (& MSG, null, 0 , 0 )) 40 { 41 Translatemessage (& MSG ); 42 Dispatchmessage (& MSG ); 43 } 44 Return MSG. wparam; 45 46 } 47 Lresult callback wndproc (hwnd, uint message, wparam, lparam) 48 { 49 HDC; 50 Rect; 51 Paintstruct pS; 52 Switch (Message) 53 { 54 Case Wm_create: 55 Return 0 ; 56 Case Wm_paint: 57 HDC = beginpaint (hwnd ,& PS ); 58 Getclientrect (hwnd ,& Rect ); 59 Drawtext (HDC, text ( " Hello World " ),- 1 , & Rect, dt_singleline | dt_center | Dt_vcenter ); 60 Endpaint (hwnd ,& PS ); 61 Return 0 ; 62 Case Wm_destroy: 63 Postquitmessage ( 0 ); 64 Return 0 ; 65 } 66 Return Defwindowproc (hwnd, message, wparam, lparam ); 67 }
Aside from this program, let's talk about several concepts first.
1. What does event-driven mean?
MostWindowsThe main purpose of program design is to design message processing functions,WindowsThe operating system will save a lot of messages to the Message Queue of the process. When the mouse, dialog box, keyboard, and other events are moved, messages will be generated,WindowsSave these messages to the queue and send them to the message processing function in the FIFO order! This is event-driven!
2. What is a handle?
The handle is essentially an integer. However, to understand the actual meaning of a handle, we must start with the kernel object.
Common kernel objects include file objects and process objects,I/OObject. In fact, each object is only a memory block, which is allocated by the kernel and can only be accessed by the kernel.(Programmers passAPI (WindowsFunctions provided by the system)To access these memory objects)
,When you call a function that creates a kernel object, a handle is returned !, It indicates the created kernel object. This handle is passed to variousWindowsThe function indicates that you want to perform various operations on this kernel object! This is the most basic understanding of the handle!
Description of each part of the program
First, createWndclassObject, a window is always created based onWindowsClass. Multiple windows can be created based on the same class.MsdnHere, we will not describe them one by one. Only two important members are described. WhereLpfwndprocIs a function pointer, which points to a message processing function, that is, the window function mentioned in the book, meaning that whenWindowsUse this function to process messages sent to this window!
The last member refers to the name of this class!
After initializing the members of this class, we must register the class, that is, useRegisterclassOnce the function is successfully registered, the system has a window type named after the last member of the window type structure! The application can create a window based on the window type!
the next part is how to create this window, using createwindow I don't want to talk about the usage of this function, you can view msdn This function returns the file handle of this window. That is to say, if you want to use this window later, you can pass this file handle to the function of this operation window. When the program runs to this place, a wm_create message
the function showwindow (), and updatewindow (), the last is the cycle of the message, getmessage is used to obtain messages from the message queue and translatemessage is used to convert keyboard information into character information, dispatchmessage () to Windows send message!
Information processing functions:MainlySwitch... caseStructured functions are used to process variousWindowsSent information! You can process some information selectively, and all the information you do not want to process must be handed overDefwindowprocTo process!
Summary: This chapter focuses on a typicalWin32What components should an application be composed!