《2022年学习笔记2.pdf》由会员分享,可在线阅读,更多相关《2022年学习笔记2.pdf(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、孙鑫 vc+学习笔记( Sun Xin vc+ learning notes)1. about creating window applications Understand the capabilities of the windows program, the API function, the relationship between the message queue and the operating system, the input and output devices Window programming, sub background and front desk Front
2、 desk for what kind of window, how to display the title, and the destruction of the window, the day after tomorrow continue to run Background is the design purpose, he is through the window message queue continuous access to messages (GetMessage and translation), that is, the winprocess function of
3、the sub function to call (callback function) to achieve, written by the user himself Messages are implemented through applications that provide functions to the system platform to tune the effects of hardware, sound, or images. Applications are built on application systems The concrete steps are as
4、follows The construction of new Win32 Application source program written in empty project file with C program source file that contains the windows.h file to the WinMain function to write it contains the main function of this program with the function of DOS under the same entrance when the applicat
5、ion ends its WinMain function at the end is called by the operating system 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 1 页,共 11 页 - - - - - - - - - - allocates space assignment The program design includes: the definition of WinMain function to create a window to create a mes
6、sage loop write window procedure four step window: design a window class (fill in the blank window display status includes cursor display background loading icon loadicon window function lpfnWndProc message response function by its own operating system, but need to write code to call) registered win
7、dow class Registerclass to create a window (createWindow window size coordinate menu handle and update updateWindow ShowWindow) display window of each function type operating system is basically fixed again to set a message loop while (GetMessage (corresponding to the message size and member variabl
8、es) the corresponding application message queue) to achieve continuous operation program (background) TranslateMessage (converted to wchar) The distribution (dispatchmessage to the middle layer of the callback function for operating system) the last message editing window function switch (process to
9、 deal with the mouse button to destroy the window and the end of the news program plus default) prompt: the system is through the window function address (pointer) to invoke the process window function 2, about c+ C+ has more advantages over C, mainly in encapsulation, inheritance and polymorphism S
10、elect new project Win32 console application c+ source file written in structure c+ body can write function of foreign body and the structure can call the member variable c+ function in 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 11 页 - - - - - - - - - - similar but by
11、default to the member variable access is not the same An instance of a class and object instantiated with an object that defines a structure variable is to produce an object Constructor: the important function is to create the object itself, and each object must have a constructor that has the same
12、form as the class name and has no return value, In a statement object will automatically call the default constructor is a constructor with no parameters are only responsible for creating object not initialized if the definition of a parameter or a zeroary constructor, c+ will not create a default c
13、onstructor Destructor: the object is responsible for retrieving its memory space at the end of the objects life. In contrast to the constructor, the arguments and return values are not allowed, and a class can only have one destructor. Overloading of functions: if the constructor overloads, c+ autom
14、atically matches the call to the corresponding function, overloading the condition: the parameter type of the function and the number of arguments This pointer: an implicit pointer to an object corresponding to itself, representing the address of the object (this=&pt). This makes it easy to write th
15、e member variables in the object (using VCs automatic listing of member functions, using this ) 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 3 页,共 11 页 - - - - - - - - - - Base class inheritance and access characteristics of the three kinds of subclasses to inherit father to
16、see the definition of property inheritance characteristics and the parent class member function decision, protected in subclassing but outside (such as fish.breath (main) in the function cannot be called by the fish class but can not be inherited) visit the subclass inheritance, if the parent class
17、with parameters, the subclass inheritance should also add parameters, the constructor call the parent class and subclass, and the destructor calls in reverse order Overlay of functions: a member variable that occurs between the two classes of a parent class and a subclass. The name of the function i
18、s consistent. Scope identifier (:) used to indicate which class of member variables the function belongs to and prevents the coverage of the member functions of the parent class Polymorphism: virtual in the base class member function, sub class inheritance, VC sub class visit, sub class has a member
19、 function with the same name is called sub classes, subclasses did not call the parent class. Another: pure virtual functions appear in base classes, but are implemented by derived classes Reference and pointer: int a=5; int &b=a; a, B, point to the same memory space at the same time, AB is an alias
20、 pointer, and the variable itself occupies memory, which stores the content as the address pointing to the variable For write method of.Cpp file and.H file, create different files 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 4 页,共 11 页 - - - - - - - - - - in the open the proj
21、ect can also be found in the project file new text file (later renamed F2, VC function keys) to add one to write different engineering documents VC program to compile and link mechanism: precompiled , and when the project file is established to find the solution to the project file, header file cont
22、ains repeated problems: #ifdef XX #define XX (preparation) after adding ENDIF, repeat this definition can prevent the error, prevent the VC compilation process mechanism integration: the pre processor to the pre compiled instruction compiler (#) to project source code (.Cpp) separately compiled mach
23、ine instructions contains different object file (debag.Obj file), and finally by the connector to the target file (.Obj) and c+ standard library function standard library (.Lib) to generate a the executable file (.Exe). Tip: compile time to troubleshoot.Cpp source files individually! 3 MFC programmi
24、ng An object oriented library of functions is provided to users in a class manner MFC AppWizard is used to create a single document based on the MFC interface (SDI) applications, the new MFC AppWizard generation program dialog box click OK can see the operation of the program, on the left side of VC
25、 workspace classview tag can be seen in the five class of the automatically generated, only need to turn on the following class you can write code First of all, to understand the internal mechanism of MFC, you 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 5 页,共 11 页 - - - - -
26、- - - - - can find that the WinMain function is not found in findfile, but search the WinMain function (not the file) in the MFC file SRC file (the source code of MFC) in the installation directory Under the care, five classes (where CMainFrame is a fixed name, which is derived from CFrameWnd and CW
27、nd derived from the window class) is how to combine with WinMain. (hint: for all the global variables in global), the global object global variables are assigned to memory before WinMain you can search the cwinapp, initialization function view defines its constructor good (window tab to switch VC),
28、An example application is told by the instance handle (parameter hinstance WinMain function) to mark, and for MFC applications through the object application class to uniquely identify the application examples of each MFC program and only a copy of the application class (CwinApp) derived class, ever
29、y MFC there is only one instance of the derived class instantiation of objects, theApp is the global object, the object that the application itself, before the theApp CTestApp object constructor call will call the superclass constructor, and from associating our own program to create the class and M
30、icrosoft provides the base class. The CWinApp constructor completes some initialization when the program is running. The lookup cwinapp will find that it is a constructor with a parameter, and we do not pass arguments to the base class when we inherit, because cwinapp is the constructor with the def
31、ault value. When the program calls the constructor of the cwinapp class, and the implementation of the ctestapp class constructor, and produced a theapp object, then enter the WinMain function, the 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 6 页,共 11 页 - - - - - - - - - - Wi
32、nMain function is actually can be found by calling the afxwinmain function to complete the function (AFX for the application frame function) In accordance with the procedures for the preparation of Win32 steps to design the window class and register window class should create the window window creat
33、ed in MFC function is realized by createx CWnd functions of the function declaration in the afxwin.h file Cframe is the operation of the window function is marked CView window when the program is running the following blank window precreatwindow (using virtual function polymorphism allows programmer
34、s to modify the appearance of the window in the subclass) it extended function in creatwindow before the execution of MFC suffix function called ex is MFC window class internal operation summary: 1, first, you start the application with the global application object theapp, which is the global objec
35、t, and the this pointer in the base class cwinapp can point to the object. Without this global object, the program does not make errors at compile time, But there will be errors at runtime 2 call the global application object constructor, which will be the base class constructor cwinapp electrophore
36、sis, the latter performs some initialization application, and the application of object pointers stored 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 7 页,共 11 页 - - - - - - - - - - 3 enter the WinMain function, in the afxwinmain function. In the afxwinmain function can obtain
37、the subclass (for test applications, is the ctestapp class pointer, the pointer) by calling a virtual function: InitInstance function, which performs some initialization application, including the window class is registered, create, display and update. The createx function is called multiple times b
38、ecause a single document MFC application has multiple windows, including the frame window, toolbar, status bar, and so on 4 entering the message loop, although the default process function is set, the MFC application uses a messaging mechanism to process various messages, and when you receive the wm
39、_QUIT message, exit the message loop and end the program The cdocument class is a document class that is not derived from the CWnd class, so it is not a window class. MFC provides a document / view architecture, including document refers to the cdocument class, as is CView, data storage and loading
40、is accomplished by a document type, display and modify the data to be completed by the visual category, thus the data management and display method of separation The caboutdlg class is derived from the CDialog class, which is born in CWnd, so it is the window class, which primarily provides program
41、related help information, such as version numbers Tip: in the definitions of the member functions themselves, if the call API (provided by window) function and their function is different, so the API function name can not be added: symbols, 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - -
42、 - - - -第 8 页,共 11 页 - - - - - - - - - - the compiler automatically identifies the API function, but if the API function member function and internal call currently defined is the same as that of the latter the front must be added: symbols, otherwise the program will fail at runtime or compile Use t
43、he source code to write a button control, pay attention to add to the corresponding class become the private member variables, and the corresponding function create create display control code written in vc+, as a kind of add a message processing function method is: in the classview tab, right click
44、 on the the class name, select the add window message handler. menu command from the shortcut menu pop-up, then pop-up selection dialog box, select the key position to add the wm_create control message. See this pointer to the class, rather than the source position This chapter finds that many windo
45、w class function calls no longer need to pass window handles because they all maintain a window handle member variable internally 4. simple drawing Analyze the MFC message mapping mechanism, design a drawing program to understand messaging and capture After analysis, a MFC message response function
46、has three relevant information in the program: the function prototype, the function implementation, and the macro used to correlate the message and the message response function. The statement in the header file that is the prototype of the message response function between the two AFX_MSG_ annotati
47、on macros. There are 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 9 页,共 11 页 - - - - - - - - - - two sources in the file: one is the message mapping macro between the two AFX_MSG_MAP annotations, Through this macro, the message is associated with the message response function
48、; the other is the implementation code of the message response function in the source file. In the first chapter of the news cycle, when the message is generated, the operating system will put the message into the application queue, took out a specific message through GetMessage function and dispatc
49、hmessage function through the message to the operating system, the window procedure call application wndproc processing, switch - case structure. The message and the discriminant function, and in the MFC is not in accordance with this method, as long as the definition of three messages associated wi
50、th the message, you can achieve the response message. That is, the MFC message mapping mechanism. In fact, message routing can have a variety of ways, one of which is the definition of a virtual function for each message in the base class, the polymorphism. But this approach is not desirable, becaus