The mfc mdi program obtains the class pointer of the current active window.

Source: Internet
Author: User

1. Get the pointer of the document class from the View class

As mentioned above, before you reference the document class in the View class, use the following statement:
CTextDoc * pDoc = (CTestDoc *) GetDocument ();
Then you can use the pDoc pointer to access the document class.
The forced type conversion here is not required in the Test application, because there is only one view class in the program, and the SDI document template is assembled in Initstance (), you can Test. the following statement is displayed in the Initstance () method in cpp:
CSingleDocTemplate * pDocTemplate;
PDocTemplate = newCSingleDocTemplate (IDR_MAINFRAME, RUNTIME_CLASS (CTestDoc), RUNTIME_CLASS (CMainFrame ),
RUNTIME_CLASS (CTestView ));
AddDocTemplate (pDocTemplate );
And online definitions in TestView. h:
InlineCTestDoc * CTestView: GetDocument ()
{Return (CTestDoc *) m_pDocument ;}
In short, that is to say, the GetDocument () function of CTestView naturally thinks that CTestDoc is "matched" with it. When an application with multiple view classes is generated (such as CSplitterWnd) the window is divided into two columns, but these two columns are not derived from the same view class. The specific implementation is beyond the scope of this article). Only one view class can be assembled with a unique document class using a document template, in order to obtain the pointer of the document class in another unassembled class, it takes time to forcibly convert the type.

2. Get the pointer of the View class from the document class

The CDocument class provides two functions for positioning the View class: GetFirstViewPosition () and GetNextView (). The syntax is as follows:
VirtualPOSITIONGetFirstViewPosition () const;
VirtualCView * GetNextView (POSITION & rPosition) const;
Note: The parameters in the GetNextView () Brackets use the reference method, so the value may change after execution.
GetFirstViewPosition () is used to return the POSITION of the first view (not a view pointer, but a POSITION value). 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 ):
CTestView * pTestView;
POSITIONpos = GetFirstViewPosition ();
PTestView = GetNextView (pos );
In this way, you can get the pointer pTestView of the CTestView class. After executing a few sentences, the variable pos = NULL, because there is no next View class, naturally there is no POSITION of 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. When judging whether a class Pointer Points to an instance of a class, you can use the IsKindOf () member function to perform a row check, for example:
PView-> IsKindOf (RUNTIME_CLASS (CTestView ));
You can check whether pView refers to the CTestView 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:
CView * CTestDoc: GetVieww (CRuntimeClass * pClass)
{CView * pView;
POSITIONpos = GetFirstViewPosition ();
While (pos! = NULL)
{
PView = GetNextView (pos );
If (pView-> IsKindOf (pClass ))
Break;
}
If (! PView-> IsKindOf (pClass ))
ReturnNULL;
ReturnpView ;}
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 last view and is the last view class, it is cited as follows. 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.

3. Get the pointer of another view class from one view class

Combining 1 and 2, it is easy to obtain the method of getting pointers between view classes: it is to use the document class as a transit, first use the 1 method to get the pointer of the document class, and then use the 2 method, use the view locating function of the document class to obtain another view class. Similarly, you can implement a function:
(Assume that you want to obtain a pointer to other view classes from CTestAView)
CView * CTestAView: GetView (CRuntimeClass * pClass)
{CTestDoc * pDoc = (CTestDoc *) GetDocument ();
CView * pView;
POSITIONpos = pDoc-> GetFirstViewPosition ();
While (pos! = NULL)
{
PView = pDoc-> GetNextView (pos );
If (pView-> IsKindOf (pClass ))
Break;
}
If (! PView-> IsKindOf (pClass ))
ReturnNULL;
ReturnpView ;}
Compared with GetView () in 2, this function adds the first sentence to get the document class pointer, and adds the document class pointer before GetFirstViewPosition () and GetNextView, to indicate that they are document class member functions.
With this function, to obtain the CTestBView pointer from CTestAView, you only need:
CTestBView * pTestbView = (CTestView *) GetView (RUNTIME_CLASS (CTestBView ));

4. Obtain the View class pointer from the primary frame window class

This is simple for the SDI program Test mentioned in this article. You only need to use the GetActiveView () member function of the CFrameWnd class. The format is as follows:
CFrameWnd: GetActiveView ()
However, when this function is applied to the CMDIFrameWnd of the MDI application, it does not obtain the View class of the current active subwindow as expected, but returns NULL. This is a essentials problem. In the MDI program, CMDIFrameWnd does not have a relationship with any view class, that is, no view class directly belongs to it. Only the subframe window class javasichildwnd is the parent window of all child window view classes. The parent window of the sub-frame window is CFrameWnd. Therefore, the correct method to obtain the active View class in the MDI program should be: first obtain the active sub-frame window, and then obtain the activity View class from the active sub-frame window:
// Obtain the activity sub-frame window
CMDIChildWnd * pChild = (CMDIChildWnd *) GetActiveFrame ();
// Or: CMDIChildWnd * pChild = MDIGetActive ();
// Obtain the activity view of the activity sub-frame window
CMyView * pView = (CMyView *) pChild-> GetActiveView ();

5. Obtain the primary frame window class pointer from the View class:

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. The usage is described in detail in InfoViewer.

6. Obtain the application class from any class
You can use the global function AfxGetApp () of MFC.

7. Obtain the primary frame window class from the application class
The CWinThread class has a data member m_pMainWnd. Since the CWinApp class is derived from CWinThread, our application is derived from CWinApp, so our CTestApp class also has a m_pMainWnd member, it is guided by the CMainFrame class. (Appropriate forced type conversion is required ).

Note the following points:
A. When Class A obtains the pointer of Class B, Class A should contain the header file of Class B.
B. In most cases, force type conversion is required, and note the brackets.
The compatibility between the derived class and the parent class pointer type makes it very important to distinguish classes clearly. When you are not sure, it is best to add forced type conversion.

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.