MFC beginners: Conventional methods of mutual access between classes in the MFC program.

zhaozj2021-02-16  45

This is what I have organized in the process of learning MFC. In fact, it is a small study notes, countless articles, sharing, just hope to help those MFC beginners, and hope to get the correct of the old birds. Oh, gossip does not say into the body. Based on the document-view structure application, data can be separated from the user to the observation of the data. In such applications, the data is typically placed in the document class, and the display is generally responsible by the view view class. Taking the display image as an example, the data of the image is stored in the document class, and the data to be displayed in the discovery class is to obtain the data in the document class. If the application created by AppWizard is used in the view class, there is a member function of a getDocument () in the view class to obtain the document class's pointer, and the document class can be obtained using the document class. However, interviews in variables between classes in the MFC program are not limited thereto, but with multiple document interface applications as an example, the program mainly includes trial class, document class, MDI sub-frame class, MDI parent framework class, and application classes. There is also other dialog classes generated by users, and the like. How to implement the variables between these classes, is the problem that MFC programming beginners should understand, and it is also a question to discuss this article.

In general, if you want to access a variable in another class in a class, it is easier to think that you want to access the pointer to the class. The method for obtaining a pointer to access the class is to utilize a specific global API function and a member function provided by each class (access class). The two most important functions in the function of obtaining a particular class pointer is: Get an AFXGetApp () function of the application class pointer and get the AFXGETMAINWND () function of the primary framework pointer. As long as the flexible application of these two functions, the mutual access between classes can be implemented. The specific discussion is as follows: 1. Method for accessing other class variables can be used to obtain a pointer to the application class with a function AFXGetApp (), the code is as follows: cclass *** app * m_PApp = (cclass *** app *) AFXGetApp (); In order to obtain a pointer to the current active sub-frame class, the main framework class mainly provides two member functions: mdigetActive () and getActiveFrame (). The code for the two function applications is as follows: cchildframe * m_pchildfrm = (cchildframe *) mdigetActive (); CchildFrame * m_pchildfrm = (cchildframe *) getActiveFrame (); gets a pocket of the sub-frame class, you can use the two member functions of the subframe class to getActiveDocument () and getActiveView () functions to get the document class and view class of the current activity. pointer. The code is as follows: cclass *** doc * m_pdoc = (cclass *** doc *) m_pchildfrm-> getActiveDocument (); cclass *** view * m_pView = (cclass *** view *) m_pchildfrm-> getActiveView (); 2 . Applications to access other class variables You can know above, as long as you get the pointer of the primary framework class, the remaining subframe class document class view class's pointer can be obtained. A method for acquiring the main framework pointer is to use the AFXGETMAINWND () function, code as follows: CMAINFRAME * m_pmianfrm = (cmainframe *) AFXGETMAINWND (); In addition, we know that there is a CWND * in the application class with a cwnd * type no m_pmainwnd variable, used Store the pointer to the application main framework class. Therefore, it can also obtain the pointer of the main frame class, the code is as follows: CMAINFRAME * m_pmainfrm = (cmainframe *) m_pmainwnd; the rest can refer to 1. 3. Subframe Class Accessing Other Class Methods Getting Application Class and Main Framework Pointers You can use global functions AFXGetApp () and AFXGETMAINWND (), get the document class and view clauses to get throughctiveDocument () and getActiveView (), The specific code can refer to two above. 4. View Class Accessing Other Class Methods Getting the application class and the main framework class can use global function AFXGetApp () and AFXGETMAINWND (), and get the sub-framework pointer can be done with its member function getParent (), the code is as follows: CchildFrame * m_pchildfrm = (cchildframe *) getParent (); Get Document class pointer can be done with the member function of the subframe class, but from the efficiency angle, the best use of the member function getDocument () to complete . 5. Document Class Accessing other classes Getting a pointer to the application class and the main framework class can be used with the Global Function AFXGetApp () and AFXGETMAINWND (), and the subframe class can be indirectly obtained by the members of the primary framework class.

In many applications, variables in the view class are more important, such as saving the position of the mouse, selecting the size and location of the area. Since a document class can correspond to multiple view classes, the document class has a cptrlist type variable M_ViewList to save multiple view clauses and provide two member functions: getFirstViewPosition () and getNextView () to access the corresponding view class. Pointer. Below will give two methods for obtaining view clauses. Method 1: Indirect through the member function getActiveView () of the sub-frame. Method 2: GetFirstViewPosition () and getNextView () to apply document class, the specific code is as follows: position pos = getFirstViewPOSISTON () (); // Get the position of the pointer to the first view class CCLASS *** View * m_pView = (Cclass *** view *) GetNextView (POS); // Get the first view class pointer, the position variable POS is shifted downward down the next view class pointer If it is a single view, the first call GetNextView () The pointer to the view class can be obtained. If multiple view clauses can be used to identify the desired view pointers in the MFC base class with the While cycle statement. The code is as follows: CWANTVIEW * m_pwantview; // Want to get the view class of the map class CView * lpview; // Temporary view class pointer position pos = getFirstViewPOSITON (); // Get and save the first view class pointer location While (POS! = NULL) {LPVIEW = GetNextView (POS); // Get the first view class pointer, the position variable POS move downward down the next view class Pointer IF (LPView-> iskindof (runtime_class (cwantview) )) // Determine whether to point to the pointer {m_pwantview = (cwantview *) lpview; // get the pointer}}}} reminder A description of the CView class in front of the document header file, increase the CVIEW class Cclass *** view; if there is no pre-explanation, there is a hassle when compiling, because it is not done, it will cause a loop to contain. Class Description Avoid unnecessary #include instructions, but it is suitable for class pointers. 6. Other classes Access the above class Most applications In addition to the above-mentioned common types, there are many different dialog classes, dialog classes, and more, sometimes these classes require document class data, view class mouse Location, the size of the scroll size and other information. In summary, as long as the gestational function AFXGetApp () and AFXGETMAINWND () have obtained pointers of the application class and the main framework class, the pointer of the active sub-frame class can be obtained, and the document class and view clauses can also be very convenient. Get. A simple example of applying the above method is given below. We know for a multi-document application, if the open sub-window is more, and sometimes you want to close these sub-windows all close, using the MDiGetActive () function of the main frame class to achieve the purpose. The specific method is to add a menu, map the message mapping function to the main frame class, and you can add the following code to all the functions of all the sub-windows can be implemented in the mapping function of the message. CMICHILDWND * PCHILD; while ((pchild = mdiGetActive ())! = Null) {pchild-> sendMessage (wm_close);} Top of some classes between variables often use each other, pay attention to defining the pointer variable must put the corresponding The class definition header file contains it, otherwise the compile will be wrong.

转载请注明原文地址:https://www.9cbs.com/read-15416.html

New Post(0)