Getting pointers in MFC

Source: Internet
Author: User

This article from: http://mail.ustc.edu.cn /~ Bywang/programm/vc/sx/8.html

 

Before reading this article, we assume that you have:
1. Know how to create an App Wizard for a single document
2. Familiar with C ++ classes, function overloading, and other simple knowledge
3. Know how to add member variables to the View class or Doc file.
4. It is best to use the IDE debugging tool of MFC. You can copy the program in this article for debugging.
5. Know how to add virtual functions or message processing functions to a framework class.
(This article is reproduced)
Link: http://www.3snews.net/index.php/550/action_viewspace_itemid_7758.html
This chapter is not the content of the original course, but is reposted from the Internet and has not changed.
Writing notes 8 is a waste of time because I didn't always know how to obtain various pointers when I started learning MFC.
There may also be people who are learning VC suffering from this problem. I hope this article will help.

Usage of pointers in MFC applications
1) Get the Doc pointer in the View
2) obtain the MainFrame pointer in the App
3) obtain the MainFrame pointer in the View.
4) obtain the View (created) pointer
5) Get the pointer to the current document
6) Get the status bar and toolbar pointer
7) Get the status bar and toolbar Variables
8) obtain the menu pointer in Mainframe.
9) obtain the application class from any class
10) Get the pointer of the View class from the document class (1)
11) Get the document template pointer in the App
12) obtain the document class pointer from the document template
13) Get the document template pointer in the document class
14) Get the pointer of the View class from the document class (2)
15) obtain the pointer of another view class from one view class
In VC programming, the biggest obstacle and problem for students who have just started learning is the message mechanism and pointer acquisition and
Operation. In fact, these contents are basically required for every VC learning tool book, and there are many
All problems can be solved. The following text mainly refers to some of my experiences on Pointer usage in programming, which is inappropriate.
Please correct. Generally, the framework we use is the MFC App Wizard (exe) Framework generated by the Wizard provided by VC,
There are pointer acquisition and Operation Problems in both multi-document and single-document. The content below is generally
And then talk about the usage of pointers in multiple threads. The class to be used must contain the response header file. First
Generally, the instance pointer this is obtained for this class (as is supported by the class, document, and dialog box ).
Function direction in the classOthersClass or function, so that you can operate and use
Function.
1) Get the Doc pointer CYouSDIDoc * pDoc = GetDocument () in the View. Only one View can have one document.
2) obtain the MainFrame pointer in the App
The m_pMainWnd variable in CWinApp is the pointer to the MainFrame. You can also: CMainFrame * pMain = (CMainFrame *) AfxGetMainWnd ();
3) obtain the MainFrame pointer CMainFrame * pMain = (CmaimFrame *) AfxGetApp ()-> m_pMainWnd in the View;
4) obtain the View (created) pointer CMainFrame * pMain = (CmaimFrame *) AfxGetApp ()-> m_pMainWnd;
CyouView * pView = (CyouView *) pMain-> GetActiveView ();
5) obtain the current document pointer CDocument * pCurrentDoc = (CFrameWnd *) m_pMainWnd-> GetActiveDocument ();
6) obtain the status bar and toolbar pointer CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ()-> GetDescendantWindow (AFX_IDW_STATUS_BAR );
CToolBar * pToolBar = (CtoolBar *) AfxGetMainWnd ()-> GetDescendantWindow (AFX_IDW_TOOLBAR );
7) if you add the toolbar and status bar variables to the framework, you can do the same.
(CMainFrame *) GetParent ()-> m_wndToolBar;
(CMainFrame *) GetParent ()-> m_wndStatusBar;
8) obtain the menu pointer CMenu * pMenu = m_pMainWnd-> GetMenu () in Mainframe ();
9) obtain the application class from any class using the global function AfxGetApp () of MFC.
10) obtain the pointer of the View class from the document class
I am from Hangzhou.
The CDocument class provides two functions for positioning the View class:
GetFirstViewPosition () and GetNextView ()
Virtual POSITION GetFirstViewPosition () const;
Virtual CView * 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: return the pointer of the next View class and use the reference call method to change the value of the input POSITION type parameter. Obviously, in the Test program, only
A View class. 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;
POSITION pos = GetFirstViewPosition ();
PTestView = GetNextView (pos );
In this way, you can access the pTestView pointer 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 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. 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: 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. \ r \ n http://www.VCKBASE.com ");
Return NULL;
}
Return pView;
}
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.
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. the integration of pointers 1 and 2 from one view class to the other, it is easy to obtain the method of mutual pointer acquisition between view classes: it is to use the document class as a transit, first use the method 1 to get the pointer to the document class, and then use the method 2 to get another view class using the view locating function of the document 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;
POSITION pos = pDoc-> GetFirstViewPosition ();
While (pos! = NULL ){
PView = pDoc-> GetNextView (pos );
If (! PView-> IsKindOf (pClass ))
Break;
}
If (! PView-> IsKindOf (pClass )){
AfxMessageBox ("Connt Locate the View .");
Return NULL;
}
Return pView;
}
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 to: CTestBView * pTestbView = (CTestView *) GetView (RUNTIME_CLASS (CTestBView ));

11) multiple document templates can also be added to a single document. However, multiple document templates are generally developed using the MDI method, which is very similar to the preceding view obtaining method, please refer to the following four sources (11, 12, 13, and 14) for details:
Http://sanjianxia.myrice.com/vc/vc45.htm)
You can use CWinApp: GetFirstDocTemplatePostion to obtain the location of the first document template registered by the application. Use this value to call the CWinApp: GetNextDocTemplate function to obtain the pointer of the first CDocTemplate object.
POSITION GetFirstDocTemplate () const;
CDocTemplate * GetNextDocTemplate (POSITION & pos) const;
The second function returns the document template identified by the pos. POSITION is a value defined by MFC for iteration or object pointer retrieval. With these two functions, the application can traverse the entire document template list. If the retrieved document template is the last in the template list, the pos parameter is set to NULL.
12) A document template can have multiple documents. Each document template retains and maintains a pointer list for all corresponding documents.
Use the CDocTemplate: GetFirstDocPosition function to obtain the POSITION of the first document in the document set related to the document template, and use the POSITION value as the CDocTemplate :: getNextDoc parameters are used to repeatedly traverse the list of template-related documents. Function prototype:
Viaual POSITION GetFirstDocPosition () const = 0;
Visual CDocument * GetNextDoc (POSITION & rPos) const = 0;
If the list is empty, rPos is set to NULL.
13) in this document, you can call CDocument: GetDocTemplate to obtain the pointer to this document template.
The original function form is as follows: CDocTemplate * GetDocTemplate () const;
If the document does not belong to document template management, the return value is NULL.
14) a document can have multiple views. Each document retains and maintains a list of all associated views.
CDocument: AddView connects a View to the document, adds the view to the list of associated views of the document, and points the document pointer to the document. When a File/New, File/Open, Windows/New, or Window/Split command connects a newly created object to the document, MFC automatically calls this function, the framework associates the document with the view through the document/view structure. Of course, programmers can also
You must call this function.
Virtual POSITION GetFirstViewPosition () const;
Virtual CView * GetNextView (POSITION & rPosition) cosnt;
The application can call CDocument: GetFirstViewPosition to return the location of the first view in the list of views associated with the call document, and call CDocument: GetNextView to return the view at the specified position, set the value of rPositon to the POSITION value of the next view in the list. If the result is regarded as the last view in the list, the rPosition is set to NULL.
15) obtain the pointer of another view class from one view class
This application is widely seen in multi-view applications. Generally, if you do a variable mark in the main program or the main framework, you can also get it. What is more common is to use the document class as a transit, traverse and locate the view of the document class to obtain another view class. This function can be obtained from the 10th items in this article.

_______________________________________________________________

Access other classes of the application
Obtain CWinApp:
-Directly call AfxGetApp () or use theApp in CMainFrame, CChildFrame, CDocument, and CView
-Only AfxGetApp () can be used in other classes ()
Obtain CMainFrame:
-Use AfxGetMainWnd () or m_pMainWnd in CMinApp
-GetParentFrame () is available in CChildFrame ()
-Use AfxGetMainWnd () in other classes ()
Obtain CChildFrame:
-Use GetParentFrame () in CView ()
-Use MDIGetActive () or GetActiveFrame () in CMainFrame ()
-Use AfxGetMainWnd ()-> MDIGetActive () or AfxGetMainWnd ()-> GetActiveFrame () in other classes ()
Obtain CDocument:
-Use GetDocument () in CView ()
-Use GetActiveView ()-> GetDocument () in CChildFrame ()
-Used in CMainFrame
-If SDI: GetActiveView ()-> GetDocument ()
-If MDI: MDIGetActive ()-> GetActiveView ()-> GetDocument ()
-In other classes
-If SDI: AfxGetMainWnd ()-> GetActiveView ()-> GetDocument ()
-If MDI: AfxGetMainWnd ()-> MDIGetActive ()-> GetActiveView ()-> GetDocument ()
Obtain CView:
-POSITION pos = GetFirstViewPosition (); GetNextView (pos) in CDocument)
-GetActiveView () in CChildFrame ()
-In CMainFrame
-If SDI: GetActiveView ()
-If MDI: MDIGetActive ()-> GetActiveView ()
-In other classes
-If SDI: AfxGetMainWnd ()-> GetActiveView ()
-If MDI: AfxGetMainWnd ()-> MDIGetActive ()-> GetActiveView ()

Another reference article. Source: http://yuelinniao.spaces.live.com/blog/cns! 588F0F46185E082D! 135. entry

Access other classes of the application

Obtain CWinApp:
-Directly call AfxGetApp () or use theApp in CMainFrame, CChildFrame, CDocument, and CView
-Only AfxGetApp () can be used in other classes ()

Obtain CMainFrame:
-Use AfxGetMainWnd () or m_pMainWnd in CMinApp
-GetParentFrame () is available in CChildFrame ()
-Use AfxGetMainWnd () in other classes ()

Obtain CChildFrame:
-Use GetParentFrame () in CView ()
-Use MDIGetActive () or GetActiveFrame () in CMainFrame ()
-Use AfxGetMainWnd ()-> MDIGetActive () or AfxGetMainWnd ()-> GetActiveFrame () in other classes ()

Obtain CDocument:
-Use GetDocument () in CView ()
-Use GetActiveView ()-> GetDocument () in CChildFrame ()
-Used in CMainFrame
-If SDI: GetActiveView ()-> GetDocument ()
-If MDI: MDIGetActive ()-> GetActiveView ()-> GetDocument ()
-In other classes
-If SDI: AfxGetMainWnd ()-> GetActiveView ()-> GetDocument ()
-If MDI: AfxGetMainWnd ()-> MDIGetActive ()-> GetActiveView ()-> GetDocument ()

Obtain CView:
-POSITION pos = GetFirstViewPosition (); GetNextView (pos) in CDocument)
-GetActiveView () in CChildFrame ()
-In CMainFrame
-If SDI: GetActiveView ()
-If MDI: MDIGetActive ()-> GetActiveView ()
-In other classes
-If SDI: AfxGetMainWnd ()-> GetActiveView ()
-If MDI: AfxGetMainWnd ()-> MDIGetActive ()-> GetActiveView ()

However, pay attention to the class C * view declaration to get the View pointer in the doc,
By default, mfc already contains * Doc. h In * View. h. If * Doc. h contains
* View. h will cause the nested inclusion problem, so that class C * View should be added to * Doc. h;
Add # include "* View. h" to * Doc. cpp"

//////////////////////////////////////// //////////////////////////
In fact, you can add various visual or document pointers to the CYourApp, and send the pointers to the corresponding variables in the CYourApp during the initialization of the visual or document, in this way, no matter where the above pointer is used, you only need to (CYourApp *) AfxGetApp () to obtain its attribute variable. It is clear and it is more convenient for me to always talk about it :)

//////////////////////////////////////// //////////////////////////
I threw a brick first, and the jade smashed it!
You can accurately obtain any object (Application, DocTemplate, Document, View, Frame) by using the following methods)
1. Obtain the current App object through AfxGetApp;
2. Obtain the main window through AfxGetMainWnd;
3. Use CMDIFrameWnd: GetActiveFrame to get the current activity window;
4. Use GetNextWindow () to repeat all the child windows of the example. (To obtain the child window you want, you can mark it with a specific member variable );
5. Use CWinApp: GetFirstDocTemplatePostion () and CWinApp: GetNextDocTemplate () to traverse all DocTemplate objects and use CDocTemplate: GetDocString () to determine which document moban object is currently obtained.
6. Use the CDocTemplate: GetFirstDocPosition () and the GetNextDoc () combination of CDocTemplate to traverse all the document objects of the template, and use CDocument: GetDocTemplate () to obtain the document template. Use CDocment :: getTitle () or GetPathName () to determine the current document.
7. You can use GetFirstViewPositon () and GetNextView () of cdocuemts to traverse View objects. Generally, you can access View member variables to differentiate views. You can use CView: GetDocument () to obtain document objects;
8. Frame-> View: Use the GetActiveView method;
9. Frame-> Doc: GetActiveDocument ();
10. View-> Frame: GetParentFrame ();
11. View-> Doc: GetDocuemt () // as mentioned above.
12. Doc-> View: as mentioned above;
13. Doc-> Frame: I don't know if there is any direct method.

 

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.