Obtain pointers in an MFC Application

Source: Internet
Author: User
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 ).
A function in the class sends a pointer to another class or function, so that you can operate and use
Function.

1) Get the doc pointer cyousdidoc * pdoc = getdocument () in the view. Only one object can have one object.
File.
2) obtain the mainframe pointer in the app
The m_pmainwnd variable in cwinapp is the mainframe pointer.
Alternatively, you can: 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
Obtain it using the global function afxgetapp () of MFC.

10) obtain the pointer of the View class from the document class
I learned from http://download.cqcnc.com/soft/program/article/vc/vc405.html,
The purpose of getting a view pointer from a document is generally to control the positioning of multiple views in the same document.
In particular, this function is very required when the Text Processing ceditview generates multiple view classes.
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 first view position (the returned result is not a view class pointer, but
Position value), getnextview () has two functions: return the pointer of the next View class and use
Reference the 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 (
Define a position structure variable to assist in operations ):
Ctestview * ptestview;
Position Pos = 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 no
There is a next View class, and naturally there is no position for the next View class. But these statements are too simple, not
It has strong versatility and security features. As mentioned above, when multiple views are required
When specifying the class pointer, We need to traverse all view classes until the specified class is found. Judge a class pointer
To check whether a class instance is available when the iskindof () member function is available, such:
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 will
A member function of a 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.
Possible:

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 function of getnextview () is to change the current view pointer to a view.
Returns the current view pointer at the same time. 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
Views are cited. 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 that can be used to convert the class name
Cruntimeclass is a pointer. As for forced type conversion, it is also for the sake of security, because from the same
The pointer types between base classes are compatible with each other. 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 and combine 1 and 2. It is easy to obtain mutual access between view classes.
Pointer method: 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;
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 class pointer of the document, and
Before getfirstviewposition () and getnextview (), a document class pointer is added to indicate that they are documents.
Class member functions. With this function, when you want to obtain the ctestbview pointer from ctestaview, you only need
Under: ctestbview * ptestbview = (ctestview *) getview (runtime_class (ctestbview); 11) multiple document templates can be added to a single document. However, development in general uses the MDI method.
The Method of Multi-document templates is very similar to the method for obtaining the preceding view. Here, I will explain it a bit. If it is not clear,
Please refer to msdn (the following four sources (11, 12, 13, and 14:
Http://sanjianxia.myrice.com/vc/vc45.htm)

You can use cwinapp: getfirstdoctemplatepostion to obtain the first document template registered by the application.
Use this value to call the cwinapp: getnextdoctemplate function to obtain the first
Cdoctemplate object pointer. Position getfirstdoctemplate () const;
Cdoctemplate * getnextdoctemplate (Position & Pos) const;

The second function returns the document template identified by the POS. Position is defined by MFC for iteration or object
The value retrieved by the pointer. With these two functions, the application can traverse the entire document template list. If
The document template is the last in the template list, and the POs parameter is set to null.

12) A document template can have multiple documents. Each document template retains and maintains all corresponding documents.
Pointer list.
Use the cdoctemplate: getfirstdocposition function to obtain the first part of the document set related to the document template.
Documents, and use the position value as the cdoctemplate: getnextdoc parameter to repeatedly traverse and
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 and adds the view to the list of associated views of the document.
. When file/New, file/open, Windows/new, or
Window/split command to connect a newly created object to the document, MFC will automatically call
This function is used by the Framework to associate documents with views 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 view associated with the call document
The location of the first view in the list, and call cdocument: getnextview to return the view at the specified position.
The value of rpositon is set to the position value of the next view in the list. If you find
Set rposition 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 change it in the main program or main framework
Mark, which can also be obtained. What is more common is to use the document class as a transit and traverse the view of the document class.
Locate to get 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 ()

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.