The access and application of pointers to various classes in MFC classes

Source: Internet
Author: User

About the documents and views under MFC and the access to the framework, these issues have been commonplace, but I think there is no detailed description, special

Not for the poor English people, I looked at some blog, summed up a bit! I hope it helps people like me!
One:
1: Because for the SDI program, the main frame window is the Document box window (if this also do not know, it is necessary to look at MFC under the form of a single document principle).
The following is about a single document.
Example: How to get a pointer to a view class in the CMainFrame frame.
You can get the frame pointer first, and then call the GetActiveView function to point to the current Activity view.
C **view * PView;
Pview= (c**view*) (cframewnd*) AfxGetApp ()->m_pmainwnd)->getactiveview ();

Of course, these may be known to be so used, but the real m_pMainWnd and AfxGetApp () what the meaning may be some people do not understand.
You may all know how to get the mainframe pointer (frame Class) in the app: the m_pMainWnd variable in CWINAPP is the CMainFrame pointer.

So in other classes can also get m_pMainWnd first, you get the mainframe pointer. So the pointer to the view class must first get the CFrameWnd pointer m_pMainWnd, and then the GetActiveView under the call Framewnd point to the current Activity view.
The origin of m_pMainWnd:
Each MFC application has an object that CWinApp derived classes. 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 a new MFC application, the m_pMainWnd assignment statement appears in the InitInstance virtual function. The only exception is the MFC application for a single document interface, which you cannot see in the InitInstance function because it has been hidden in ProcessShellCommand in this function. So you can draw a conclusion: as long as you create your own window class, you will assign the object of this class to the m_pMainWnd. And this member can only be used in the C**app class, so how to use the CWnd type variables in this CWinApp class to get pointers to the main frame window??
The AfxGetApp function is possible because AfxGetApp () gets the object of the CWinApp class, and the AfxGetApp return value is the CWinApp object pointer, which is the object defined in the C**app.cpp generated by MFC (the pointer to the object Theapp).
Because you get Cwndapp member functions or member variables in the projects you create, you have to cast them into your own project to find the member functions or variables.
Note: In a single document, the easiest way to get a visual pointer is to
((C**view *) Cframewnd::getactiveview ())
2: Of course, you can also get a pointer to a document class in Framewnd:
cmydocument* PDoc;
Pdoc= (cmydocument*) (cframewnd*) AfxGetApp ()->m_pmainwnd)->getactivedocument ();
3: From the above can know: How to get the mainframe pointer in view
CMainFrame *pmain= (CMainFrame *) AfxGetApp ()->m_pmainwnd;

Note: Get the main frame window class pointer from the view class: Using a function: Cwnd::getparentframe () or AfxGetMainWnd () Also
can achieve the purpose. GetParentFrame () works by searching in the parent window chain until it finds CFrameWnd or its derived class, and returns its pointer.

((CMainFrame *) cwnd::getparentframe ())
Or
((CMainFrame *) AfxGetMainWnd ())
Two:
Of course for MDI programs, because the child window is the Document box window, you first get the active child frame window with GetActiveFrame (), and then get the active view and document from that child window:

cmdichildwnd* pchild= (cmdichildwnd*) (cframewnd*) AfxGetApp ()->m_pmainwnd)->getactiveframe ();
To get the active view:
cmyview* pview= (cmyview*) Pchild->getactiveview ();

To get the active document:
cmydocument* pdoc=pchild->getactivedocument ();

Note: You can also use this method to get the visual pointer in multiple documents
Get the active child frame window
cmdichildwnd* pchild= (cmdichildwnd*) getactiveframe ();
Or:
cmdichildwnd* pchild=mdigetactive ();
Get active view of active child frame window
cmyview* pview= (cmyview*) Pchild->getactiveview ();

Three:
1. Get a pointer to a document class from a view class
Before you need to refer to a document class in a view class, use the following statement:
C*doc *pdoc= (c*doc*) getdocument ();
You can use the Pdoc pointer to access the document class later.
2. From the document class to get the view class pointer CDocument class provides two functions for positioning the view class:

GetFirstViewPosition () and GetNextView ()

Note: The parameters in the GetNextView () brackets are referenced, so the value may change after execution. GetFirstViewPosition () for

Returns the first view position (not the view class pointer returned, but a position type value), and GetNextView () has two features: Returns a pointer to the next view class and changes the value of the passed-in position type parameter by using a reference transfer. Obviously, there is only one view class in the test program, so just call the two functions one at a time to get the Ctestview pointer as follows (you need to define a position structure variable to assist the operation):

c*view* PView;
POSITION pos=getfirstviewposition ();
Pview=getnextview (POS);

In this way, the pointer to the C*view class can be pview. After execution completes a few sentences, the variable pos=null, because there is no next view class, Naturally there is no position of the next view class. But some of the statements are too simple, not too strong commonality and security features; As we said earlier, when you want to return a pointer to a specified class in multiple views, we need to iterate through all the view classes until we find the specified class. When you determine whether a class pointer points to an instance of a class, you can check it by using the IsKindOf () member function.

Such as:
Pview->iskindof (Runtime_class (C*view));
You can check whether the PView refers to the C*view class.
With the above basics, we can already get pointers to any class from the document class. For convenience, we use it as a member function of a document class that has a parameter that represents the pointer to which class to get. Implemented as follows:
cview* c*doc::getvieww (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.");
return NULL;}
return PView;}
This is determined by using the member function IsKindOf () of the two-time view class, because there are three possible exits while loops:
1.pos is null, that is, the next view class is no longer available for operation;
2.pView has met the requirements.
3.1 and 2 are satisfied. This is because the function of GetNextView () is to change the current view pointer to a view and return the current view pointer, so the POS is the position of the next view class of PView, and it is entirely possible to be both Pos==null and PView compliant. When the desired view is the last

A view is the last view class when it is cited. Two judgements are therefore required.
Use this function to follow the following format (take the Ctestview pointer as an example):
ctestview* ptestview= (ctestview*) GetView (Runtime_class (Ctestview));
Runtime_class is a macro that simply understands the role of converting a class's name to CRuntimeClass as a pointer.
the coercion of type conversions is also considered for security features because the pointer types from the same base class are compatible with each other. This coercion type conversion may not be necessary, but it can avoid some of the problems that may arise ~

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.