MFC beginners: a common method for mutual access between classes in the MFC program.

Source: Internet
Author: User

This is my work in the process of learning MFC. In fact, it is just a small learning note, which is not an article. Sharing it is just a hope to help those beginners of MFC a little bit, and hope to get the old birds correct. Well, let's skip the text.
Applications based on the document-view structure can separate data from user observation. In such applications, data is generally stored in the document class, and the display of applications is generally the responsibility of the View class. Take the image display as an example. The image data is stored in the document class. to display the image in the attempt class, you need to obtain the data in the document class. If an application created using Appwizard has a getdocument () member function in the View class, the pointer of the document class can be obtained, and the data of the document class can be obtained by using the pointer of the document class. However, variable mutual access between classes in the MFC program is not limited to this. The multi-document interface application is used as an example, the program mainly includes the attempt class, document class, MDI sub-framework class, MDI parent framework class, and application class. In addition, there are other user-generated dialog box classes. How to implement the mutual access of variables between these classes is a problem that should be understood by beginners of MFC programming. It is also a problem to be discussed in this article.

In general, to access the variable (public) in another class in one class, the easiest thing to think of is to get the pointer to the category class. To obtain pointers to the callback class, you generally use a specific global API function and a member function provided by each class. The two most important functions in obtaining a specific class pointer are: The afxgetapp () function for obtaining the application class pointer and the afxgetmainwnd () function for obtaining the class pointer of the main framework. As long as these two functions are applied flexibly, the inter-class variables can be accessed over and over again. The details are as follows:
1. Methods for accessing other class variables in the main framework class
You can use the afxgetapp () function to obtain the pointer of an application class. The Code is as follows:
CClass *** app * m_papp = (cClass *** app *) afxgetapp ();
To obtain pointers to the currently active sub-framework classes, the main framework class mainly provides two member functions: mdigetactive () and getactiveframe (). The Code of the two function applications is as follows:
Cchildframe * m_pchildfrm = (cchildframe *) mdigetactive ();
Cchildframe * m_pchildfrm = (cchildframe *) getactiveframe ();
Obtain the pointer of the sub-framework class. You can use the two member functions getactivedocument () and getactiveview () provided by the sub-framework class to obtain the pointer of the current active document class and View class. The Code is as follows:
CClass *** Doc * m_pdoc = (cClass *** Doc *) m_pchildfrm-> getactivedocument ();
CClass *** view ** m_pview = (cClass *** view *) m_pchildfrm-> getactiveview ();
2. Application class access to other class variables
As you can see from the above introduction, as long as you get the pointer of the main framework class, the pointer of the Class View class of the other sub-framework class documents can be obtained. One way to get the main framework class pointer is to use the afxgetmainwnd () function. The Code is as follows:
Cmainframe * m_pmianfrm = (cmainframe *) afxgetmainwnd ();
In addition, we know that there is a cwnd * type in the application class without the m_pmainwnd variable, which is used to store the pointer of the main framework class of the application. Therefore, you can use it to obtain the pointer of the main framework class. The Code is as follows:
Cmainframe * m_pmainfrm = (cmainframe *) m_pmainwnd;
For the rest, refer to 1.
3. Methods for accessing other classes in the sub-framework class
The global functions afxgetapp () and afxgetmainwnd () can be used to obtain pointers of application classes and main framework classes. The getactivedocument () function can be used to obtain pointers of document classes and view classes () and getactiveview (). For specific code, refer to the above two.
4. View class access to other classes
You can use the global functions afxgetapp () and afxgetmainwnd () to obtain the pointers of the application class and main framework class. You can use the member function getparent () to obtain the sub-framework class pointer. The Code is as follows:
Cchildframe * m_pchildfrm = (cchildframe *) getparent ();
The getactivedocument () function of the sub-framework class can be used to obtain the document class pointer. However, it is best to use the getdocument () function of the View class from the perspective of efficiency.
5. Methods for accessing other classes in the document class
The global functions afxgetapp () and afxgetmainwnd () can be used to obtain pointers of application classes and main framework classes. The sub-framework classes can be obtained indirectly through the getactiveframe () member function of the main framework class. In many applications, it is important to obtain the variables in the View class, such as saving the mouse position and selecting the area size and position. A document class can correspond to multiple view classes. Therefore, the document class has a cptrlist variable m_viewlist to save pointers of multiple view classes, and provides two member functions:
Getfirstviewposition () and getnextview () are used to access the pointer of the corresponding View class.
The following describes two methods to obtain View class pointers.
Method 1: It is indirectly completed through the member function getactiveview () of the sub-framework.
Method 2: getfirstviewposition () and getnextview () of the application document class are completed. The Code is as follows:
Position Pos = getfirstviewpositon (); // obtain and save the pointer position of the first view class
CClass *** view ** m_pview = (cClass *** view *) getnextview (POS); // obtain the pointer of the first view class, pointer of position variable POs to the next View class
In the case of a single view, call getnextview () for the first time to obtain the pointer to the View class. If multiple view classes are called, The while loop statement can be used in combination with iskindof () in the MFC base class () to identify the required view pointer. The Code is as follows:
Cwantview * m_pwantview; // pointer variable of the View class to be obtained
Cview * lpview; // temporary View class pointer
Position Pos = getfirstviewpositon (); // obtain and save the pointer position of the first view class
While (Pos! = NULL)
{
Lpview = getnextview (POS); // obtain the pointer of the first view class. The position variable POS moves down to the pointer of the next View class.
If (lpview-> iskindof (runtime_class (cwantview) // determines whether it is a pointer to the desired class
{
M_pwantview = (cwantview *) lpview; // get the pointer of the expected class
}
}
Note that the class cClass *** view of the cview class is added before the class definition in the Class header file of the document. If no pre-description is provided, the compilation may be troublesome because it is not necessary to do so, this will cause loop inclusion. Class description to avoid unnecessary # include commands, but it is suitable for class pointers.
6. Access methods of the above classes by other classes
In most applications, apart from the common classes mentioned above, there are also many different dialog box classes and dialog bar classes. Sometimes these classes also need document data, the mouse position of the View class and the scroll size of the image. To sum up, as long as the global functions afxgetapp () and afxgetmainwnd () are used to obtain the pointer of the application class and the main framework class, the pointer of the active sub-framework class can be obtained, the pointer of the document class and View class can be easily obtained.
The following is a simple example of the above method. We know that for a Multi-document application, if many sub-windows are opened, and sometimes we want to close all these sub-windows at once, we can use the mdigetactive () of the main framework class () function. The specific method is to add a menu to map the message ing function to the main framework class, you can add the following code to the ing function of the message to disable all subwindows.
Cmichildwnd * pchild;
While (pchild = mdigetactive ())! = NULL)
{
Pchild-> sendmessage (wm_close );
}
The above is the method that is often used to access variables between classes. Note that the header file of the corresponding class definition must be included when defining pointer variables. Otherwise, the compilation may fail.
Of course, in addition to the conventional methods described above, there are many unconventional methods for inter-class variables to access each other, such as static variables and global variables. I am still studying and organizing these items. I will share them with you.

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.