Methods for obtaining other class pointers in MFC windows

Source: Internet
Author: User
Tags function prototype split

Methods for obtaining other class pointers in MFC windows

Accessing other classes of applications
Get CWinApp:
-Call AfxGetApp () directly in Cmainframe,cchildframe,cdocument,cview or with Theapp
-Only use AfxGetApp () in other classes
Get CMainFrame:
-use AfxGetMainWnd () or m_pMainWnd in Cminapp
-Available in CChildFrame getparentframe ()
-AfxGetMainWnd () in other classes
Get CChildFrame:
-Use GetParentFrame () in CView
-use MDIGetActive () or GetActiveFrame () in CMainFrame
-In other Classes AfxGetMainWnd ()->mdigetactive () or AfxGetMainWnd ()->getactiveframe ()
Get CDocument:
-Use GetDocument () in CView
-In CChildFrame with GetActiveView ()->getdocument ()
-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 ()
Get CView:
-POSITION pos = GetFirstViewPosition () in CDocument; GetNextView (POS)
-GetActiveView () in CChildFrame
-In the CMainFrame
-if Sdi:getactiveview ()
-if mdi:mdigetactive ()->getactiveview ()
-In other classes
-if Sdi:afxgetmainwnd ()->getactiveview ()
-if Mdi:afxgetmainwnd ()->mdigetactive ()->getactiveview ()

Note, however, that in doc you want to get the pointer to view C*view notice the problem with the class C*view declaration,
Because MFC already contains *doc.h in *view.h by default, if you include it in *doc.h
*view.h, it will cause nested inclusion problems, in order to add Class C*view in *doc.h;
and add #include "*view.h" to the *doc.cpp.
//////////////////////////////////////////////////////////////////
In fact, you can add a variety of visual or document pointers in CYourApp, at the beginning of those view or document
When you start, pass the pointer to the corresponding variable in the CYourApp, so that you don't have to use it later
The pointer simply (cyourapp*) AfxGetApp () takes its attribute variable, clear and clear
Convenient I have been specially operated to say:)
//////////////////////////////////////////////////////////////////
I throw a brick first, there is Jade to smash.
When and where you can get exactly one object (Application,doctemplate,document,view,frame) in the following ways
1. Get the current App object through AfxGetApp ();
2. Get the main window through AfxGetMainWnd ();
3. Get the current Activity window through cmdiframewnd::getactiveframe;
4. Through the Getnextwindow () all the sub-windows, (if you want to get the child window, you can use a specific member variable to flag);
5. Use a combination of cwinapp::getfirstdoctemplatepostion () and Cwinapp::getnextdoctemplate () to traverse all DocTemplate objects, and CDocTemplate :: GetDocString () to determine which of the currently obtained document board object is.
6. Traverse all of the template's document objects with the Cdoctemplate::getfirstdocposition () and CDocTemplate's Getnextdoc () combination, and use the Cdocument::getdoctemplate () To get the document template, use Cdocment::gettitle () or GetPathName () to determine which of the current document is.
7. Through the CDOCUEMT Getfirstviewpositon () and GetNextView () to traverse the view object, generally by accessing the view's member variable to distinguish each view, through the cview::getdocument () to obtain the Document object;
8. Frame->view: Through the GetActiveView method;
9. Frame->doc: Through GetActiveDocument ();
10. View->frame:getparentframe ();
11. VIEW->DOC:GETDOCUEMT ()//The front has been said.
12. Doc->view: the front said;
13. Doc->frame: I don't know if there is a direct way.
Use of pointers in MFC applications
1) Get doc pointer in view
2) Get mainframe pointer in app
3) Get mainframe pointer in view
4) Get the view (established) pointer
5) Get the current document pointer
6) Get the status bar with the tool bar pointer
7) Get the status bar and toolbar variables
8) Get the menu pointer in mainframe
9) Get the application class in any class
10) Pointer to the view class from the document class (1)
11) Get the document template pointer in the app
12) Get the document class pointer from the document template
13) Get the document template pointer in the document class
14) Pointer to the view class from the document Class (2)
15) Get a pointer to another view class from one view class
VC programming for students who have just begun to learn, the biggest obstacle and problem is the message mechanism and pointer acquisition and
Operation. In fact, these content is basically every VC learning reference book must be said, and through the MSDN many
Problems can be solved. The following text is mainly personal in the programming of the use of pointers to some of the experience, said inappropriately
The party please correct me. In general, the framework we use is the MFC App Wizard (EXE) framework provided by the VC that the wizard generates,
There are pointer acquisition and manipulation issues for both multiple documents and single documents. The following section is mainly about general
The framework, and then the use of pointers in multi-threading. The class to use requires a header file that contains the response. First of all
Generally obtained this class (see, Document, dialog box support) instance pointer this, for the purpose of this, can mainly pass
A function in a class that sends pointers to other classes or functions in order to manipulate and use a class in a class other than the
Function.
1) Get doc pointer in view Cyousdidoc *pdoc=getdocument (); One view can only have one text
File.
2) Get mainframe pointer in app
The m_pMainWnd variable in CWINAPP is the pointer to mainframe.
can also: CMainFrame *pmain = (CMainFrame *) AfxGetMainWnd ();
3) Get mainframe pointer in view CMainFrame *pmain= (Cmaimframe *) AfxGetApp ()->m_pmainwnd;
4) Get view (established) pointer CMainFrame *pmain= (CMAIMFRAME *) AfxGetApp ()->m_pmainwnd;
Cyouview *pview= (Cyouview *) Pmain->getactiveview ();
5) Get the current document pointer CDocument * Pcurrentdoc = (CFRAMEWND *) m_pmainwnd->getactivedocument ();
6) Get the status bar with the tool bar pointer CStatusBar * pstatusbar= (CStatusBar *) AfxGetMainWnd ()->getdescendantwindow (Afx_idw_status_bar);
CToolBar * ptoolbar= (CToolBar *) AfxGetMainWnd ()->getdescendantwindow (Afx_idw_toolbar);
7) If you add toolbars and status bar variables to the frame, you can also
(CMainFrame *) GetParent ()->m_wndtoolbar;
(CMainFrame *) GetParent ()->m_wndstatusbar;
8) in mainframe get the menu pointer CMenu *pmenu=m_pmainwnd->getmenu ();
9) Get the application class in any class
obtained by using the MFC global Function AfxGetApp ().
10) Get a pointer to the view class from the document class
I learned it from http://download.cqcnc.com/soft/program/article/vc/vc405.html.
Get view class pointers from a document purpose generally in order to control the positioning of multiple views of the same document, my experience
In particular, word processing CEditView This functionality is very much needed when multiple view classes are produced.
The CDocument class provides two functions for locating a view class:
GetFirstViewPosition () and GetNextView ()
Virtual POSITION getfirstviewposition () const;
Virtual cview* GetNextView (position& rposition) const;
Note: The parameters in GetNextView () are referenced in parentheses, so the post-execution value may change.
GetFirstViewPosition () returns the first view position (not a pointer to the view class, but a
Position type value), GetNextView () has two functions: Returns a pointer to the next view class and
The value of the passed-in position type parameter is changed by referring to the calling method. Obviously, in the test program, only
A view class, so just call these two functions once to get the Ctestview pointer as follows (you need to set
A position structure variable to assist the operation):
ctestview* Ptestview;
POSITION pos=getfirstviewposition ();
Ptestview=getnextview (POS);
This makes it possible to reach the pointer Ptestview of the Ctestview class. After executing a few sentences, the variable pos=null, because no
There is the next view class, and naturally there is no position for the next view class. But these statements are too simple to
Have too strong commonality and security features; When you want to return a reference in multiple views as you said earlier
When specifying a pointer to a class, we need to traverse all view classes until the specified class is found. Judge a pointer to a class that refers to
When an instance of a class is given, the iskindof () member function is used to check the rows, such as:
Pview->iskindof (Runtime_class (Ctestview));
You can check whether the PView refers to the Ctestview class.
With the above basics, we can already get pointers to any class from the document class. For convenience, we will make it
As a member function of a document class, it has a parameter that represents the pointer to which class to get. 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;
}
This is determined by the member function IsKindOf () of the two-view class, because there are three types of exit while loops
possible:
1.pos is null, that is, the next view class does not exist for operation;
2.pView has met the requirements.
1 and 2 are satisfied. This is because the function of GetNextView () is to change the current view pointer to a single view
Position returns the current view pointer at the same time, so POS is the position of the next view class of PView, fully
It is possible that both Pos==null and PView meet the needs. When the desired view is the last view is the last one
The view class is the same as a citation. Two judgments are therefore required.
Use this function in the following format (for example, to obtain the Ctestview pointer):
ctestview* ptestview= (ctestview*) GetView (Runtime_class (Ctestview));
Runtime_class is a macro that simply understands what it does: Turn the name of the class into
CRuntimeClass is the pointer. Coercion type conversions are also considered for security reasons, since the same
The pointer types between the base classes are mutually compatible. This type of coercion may not be necessary, but it can avoid a
Some of the problems that may arise.
3. Get pointers to another view class from one view class synthesis of 1 and 2, it is easy to conclude that the view classes get to each other
Pointer method: is to use the document class as a relay, first use 1 method to get the pointer of the document class, and then use the 2 method,
Take another view class with the view positioning function of the document class. Again, it can be implemented as a function:
(Suppose you want to get pointers 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;
}
This function is compared with the GetView () in 2, one is more than the first sentence to get the document class pointer, and the second is in
GetFirstViewPosition () and GetNextView () are preceded by a document class pointer to indicate that they are documents
The class member function. With this function, when you want to get the Ctestbview pointer from Ctestaview, simply
Under: ctestbview* ptestbview= (ctestview*) GetView (Runtime_class (Ctestbview));
11) Multiple document templates can also be added to a single document, but general development is developed using the MDI approach
Multiple file template, the method is very close to the method of obtaining the above view, here a little explanation, if not clear,
Please refer to MSDN, (the following four items (11, 12, 13, 14) Sources:
http://sanjianxia.myrice.com/vc/vc45.htm)
The first document template that you can use Cwinapp::getfirstdoctemplatepostion to get application registration
Use this value to call the Cwinapp::getnextdoctemplate function to get the first
The CDocTemplate object pointer. POSITION getfirstdoctemplate (const;
CDocTemplate *getnextdoctemplate (POSITION & pos const;
The second function returns a document template identified by the Pos. Position is an MFC-defined one for iteration or object
The value retrieved by the pointer. With these two functions, the application can traverse the entire list of document templates. If it is retrieved
Document template is the last one in the list of templates, the POS parameter is set to NULL.
12) A document template can have multiple documents, each of which retains and maintains an all correspondence
A list of pointers to the document.
Use the Cdoctemplate::getfirstdocposition function to get the first in the collection of documents related to the document template
and position values as Cdoctemplate::getnextdoc parameters to iterate through the
A list of documents related to the template. The function prototype is:
Viaual POSITION getfirstdocposition (const = 0;
Visual CDocument *getnextdoc (POSITION & rpos) const = 0;
If the list is empty, RpoS is set to null.
13) In the document, you can call cdocument::getdoctemplate to get a pointer to the document template.
The function prototype is as follows: CDocTemplate * getdoctemplate (const;
If the document is not part of the document template management, the return value is null.
14) A document can have multiple viewports. Each document retains and maintains a list of all related viewports.
CDocument::AddView connect a view to a document, add that view to the list of links that the document is associated with
and points the document pointer to the document as it is viewed. When there are file/new, File/open, windows/new, or
When a newly created view object is connected to a document by the Window/split command, MFC automatically calls the
The function that the framework links the document and the view through the document/view structure. Of course, programmers can also
Need to call the function yourself.
Virtual POSITION getfirstviewposition (const;
Virtual CView * GetNextView (POSITION &rposition) cosnt;
The application can call Cdocument::getfirstviewposition to return the view that is associated with the calling document.
The position of the first viewport in the list, and calls Cdocument::getnextview to return the view at the specified position and
The value of the Rpositon is set to the position value of the next view in the list. If found to be considered the last in the list
The rposition is set to null.
15) Get a pointer to another view class from one view class
This application is widely seen in multi-view applications, generally if you are in the main program or the main frame to change
Volume notation, can also be obtained, there is more common is to use the document class as a relay, the document Class View traversal
Locate and get another view class. This feature is available from the 10th item in this article.

//////////////////////////////////////////////////////////

Add:

16) When using the Split window method

Ctestview1*pview= (ctestview1*) ((cmainframe*) AfxGetMainWnd ())->m_wndsplitter->getpane (0,1);

17) If the window is inactive

Use the SetActiveView function to set the view you want to get to the active view

Related Article

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.