Obtain and apply various types of pointers in the MFC class

Source: Internet
Author: User

The access between documents, views, and frameworks in MFC has been a common topic, but I don't think there are any detailed descriptions.

For those with poor English, I checked some blogs and summarized them! I hope it will be helpful to people like me!
I:
1: for SDIProgramThe main framework window is the document box window (if you do not know this, you need to check the composition of the single document under MFC ).
The following is a single document.

Example: how to obtain the View class pointer in the cmainframe framework.
You can get the framework pointer first, and then call the getactiveview function to point to the current activity view.
 

Cview * Pview;
Pview = (Cview * ) (Cframewnd * ) Afxgetapp () -> M_pmainwnd) -> Getactiveview ();

Of course, this may be the case, but some people may not understand what the real m_pmainwnd and afxgetapp () mean.
You may know how to obtain the mainframe pointer (framework class) in the app: The m_pmainwnd variable in the cwinapp is the pointer of the cmainframe.

Therefore, in other classes, you can first get m_pmainwnd and get the mainframe pointer. to obtain the View class pointer, you must first obtain the cframewnd pointer m_pmainwnd, and then call getactiveview under framewnd to direct it to the current active view.
M_pmainwnd:
Each MFC application has a cwinapp derived class object. This object corresponds to the main thread of the program. The cwinapp class has a cwnd * m_pmainwnd member variable. This member variable records the main window of the application.
When you create an MFC application, m_pmainwnd is assigned to the initinstance virtual function. the only exception is the single-document interface's MFC application. You cannot see this section in the initinstance function. Code Because it has been hidden in the processshellcommand function. Then you can draw a conclusion: as long as you create your own window class, you must assign the object of this class to m_pmainwnd. this member can only be used in the C ** app class. So how can I use the cwnd type variable in the cwinapp class to get the pointer of the main frame window ??
The afxgetapp function can be used, because afxgetapp () obtains the cwinapp class object, and afxgetapp returns the cwinapp Object Pointer, which is the c ** app generated by MFC. the object defined in CPP (the pointer to the object theapp ).

Because you get the cwndapp member function or member variable in your own project, you must forcibly convert it to the class in your project to find the member function or variable.
Note: in a single document, the simplest way to obtain the pointer is
(C ** view *) cframewnd: getactiveview ())

2: Of course, you can also get the pointer of the document class in framewnd:

Cmydocument * Pdoc;
Pdoc = (Cmydocument * ) (Cframewnd * ) Afxgetapp () -> M_pmainwnd) -> Getactivedocument ();


3: You can see how to obtain the mainframe pointer in the view.
Cmainframe * pmain = (cmainframe *) afxgetapp ()-> m_pmainwnd;
 

Note: Obtain the main frame window class pointer from the View class: Use the function: cwnd: getparentframe () or afxgetmainwnd ().
Can achieve the goal. Getparentframe () works by searching in the parent window chain until cframewnd or its derived class is found and Its pointer is returned.

(Cmainframe * ) Cwnd: getparentframe ())
Or
(Cmainframe * ) Afxgetmainwnd ())


II:
Of course, for the MDI program, because the child window is the document box window, you must first use getactiveframe () to obtain the activity sub-frame window, and then obtain the activity view and document through this sub-window:

Cmdichildwnd * Pchild;
Pchild = (Cmdichildwnd * ) (Cframewnd * ) Afxgetapp () -> M_pmainwnd) -> Getactiveframe ();

 
Get activity View:

Cmyview * Pview = (Cmyview * ) Pchild -> Getactiveview ();

Get activity documents:

Cmydocument * Pdoc = Pchild -> Getactivedocument ();

Note: You can also use this method to obtain the view pointer in multiple documents.

// Get activity sub-Framework Window
Cmdichildwnd * Pchild = (Cmdichildwnd * ) Getactiveframe ();
// Or:
Cmdichildwnd * Pchild = Mdigetactive ();
// Obtains the activity view of the activity sub-frame window.
Cmyview * Pview = (Cmyview * ) Pchild -> Getactiveview ();

 
1. Get the pointer of the document class from the View class
Before you reference a document class in a view class, use the following statement:

Cdocument * Pdoc = (Cdocument * ) Getdocument ();

Then you can use the pdoc pointer to access the document class.
2. The cdocument class pointer for obtaining a View class from the document class provides two functions for locating the View class:

Getfirstviewposition () and getnextview ()

Note: The parameters in the getnextview () Brackets use the reference method, so the value may change after execution. getfirstviewposition () is used

Returns the position of the first view. getnextview () has two functions: returns the pointer of the next View class and changes the value of the input position parameter by reference transfer. Obviously, there is only one view class in the test program. Therefore, you only need to call these two functions once to obtain the ctestview pointer as follows (you need to define a position structure variable to assist in the operation ):

Cview * Pview;
Position POS = Getfirstviewposition ();
Pview = Getnextview (POS );

In this way, you can get the pointer pview of the c * View class. after executing a few sentences, the variable Pos = NULL, because there is no next View class, naturally there is no position for the next View class. however, these statements are too simple to have strong versatility and security features. As mentioned earlier, when you want to return a pointer to a specified class in multiple views, we need to traverse all view classes until the specified class is found. Checks whether a class Pointer Points to an instance of a class.

For example:

Pview -> Iskindof (runtime_class (cview ));

You can check whether pview is a cview class.
With the above foundation, we can get any class pointer from the document class. For convenience, we use it as a member function of the document class. It has a parameter that indicates the class pointer to be obtained. The implementation is as follows:

Two View class member functions iskindof () are used to determine whether to exit the while loop. There are three possible reasons:
1. If POS is null, no view class exists;
2. pview has met the requirements.
3.1 and 2 are the same. This is because the getnextview () function is to change the current view pointer to the position of a view and return the current view pointer. Therefore, POS is the position of the next View class of pview, it is possible that both Pos = NULL and pview meet the requirements. When the required view is the final

A view is cited as the last view class. Therefore, two judgments are required.
Use this function in the following format (taking the ctestview pointer as an example ):
Ctestview * ptestview = (ctestview *) getview (runtime_class (ctestview ));
Runtime_class is a macro. It can be understood simply as a function of converting the class name to cruntimeclass as a pointer.
As for forced type conversion, it is also for the sake of security, because the pointer types from the same base class are mutually compatible. This forced type conversion may not be necessary, but it can avoid some possible troubles.

Cview * Cdocument: getview (cruntimeclass * Pclass)
  {Cview * Pview;
Position POS = Getfirstviewposition ();
  While (Pos ! = Null) {
Pview=Getnextview (POS );
 If(!Pview->Iskindof (Pclass ))
 Break;}
  If ( ! Pview -> Iskindof (Pclass )) {
Afxmessagebox ("Connt locate the view.");
 ReturnNULL ;}
  Return pview ;}

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.