MFC framework classes, document classes, view classes access to each other method _ document

Source: Internet
Author: User

1. Get Application pointers
cmyapp* papp= (cmyapp*) AfxGetApp ();

2. Get the main frame pointer
The public member variable in CWINAPP m_pMainWnd is the pointer to the main frame
cmainframe* pMainFrame = (cmainframe*) (AfxGetApp ()->m_pmainwnd);
Or
cmainframe* pMainFrame = (cmainframe*) AfxGetMainWnd ();

3. Get menu pointer
cmenu* Pmenu = AfxGetMainWnd ()->getmenu ();

4, get the toolbar, status bar pointers
The main frame can be directly used m_wndToolBar, M_wndstatusbar
Other:

ctoolbar* Ptoolbar = (ctoolbar*) AfxGetMainWnd ()->getdescendantwindow (Afx_idw_toolbar);
cstatusbar* Pstatusbar = (cstatusbar*) AfxGetMainWnd ()->getdescendantwindow (Afx_idw_status_bar);

5. Get Control pointer
First use GetDlgItem () and then convert, such as:

cbutton* Pbutton = (cbutton*) GetDlgItem (Idc_mybutton);

6, through the framework to obtain documents, view pointers

Sdi:
cmainframe* pMainFrame = (cmainframe*) AfxGetMainWnd ();
cyourdoc* PDoc = (cyourdoc*) pmainframe->getactivedocument ();
cyourview* PView = (cyourview*) Pmainframe->getactiveview ();

Mdi:
cmainframe* pMainFrame = (cmainframe*) AfxGetMainWnd ();
cchildframe* pchildframe = (cchildframe*) pmainframe->getactiveframe ();
cyourdoc* PDoc = (cyourdoc*) pchildframe->getactivedocument ();
cyourview* PView = (cyourview*) Pchildframe->getactiveview ();

7. Documents, views

To get a document pointer from a view:
cyourdoc* PDoc = GetDocument ();

To get a view pointer from a document:
Using member functions GetFirstViewPosition () and GetNextView () traversal
Virtual POSITION getfirstviewposition () const;
Virtual cview* GetNextView (position& rposition) const;

Sdi:
cyourview* PView;
POSITION pos = getfirstviewposition ();
PView = GetNextView (POS);

Mdi:

Defining functions
cview* Cyourdoc::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.");
return NULL;
}
return pView;
}

Use the following:
cyourview* pview= (cyourview*) GetView (Runtime_class (Cyourview));

8, document template, document

To obtain a document template pointer from a document:
cdoctemplate* getdoctemplate () const;

To get a document pointer from a document template:
Viaual POSITION getfirstdocposition () const = 0;
Visual cdocument* Getnextdoc (POSITION & rpos) const = 0;

9. Get a pointer to each view in the Split view

Defined in the main frame: CSplitterWnd m_wndsplitter;

Define two view classes: CView1, CView2

Overload in a framework class:
BOOL cmainframe::oncreateclient (lpcreatestruct, ccreatecontext* pContext)
{
VERIFY (M_splitter. CreateStatic (this,2,1)); Split into two rows and one column
VERIFY (M_splitter. CreateView (0,0,runtime_class (CView1), CSize (100,100), pContext));
VERIFY (M_splitter. CreateView (1,0,runtime_class (CVIEW2), CSize (100,100), pContext));
return TRUE;
}

Get Split View pointer
cview1* pView1 = (cview1*) m_wndsplitter.getpane (0,0);
cview2* pView2 = (cview2*) m_wndsplitter.getpane (1,0);

10, through the mouse to obtain child window pointer

cwnd* Childwindowfrompoint (point point) const;
cwnd* Childwindowfrompoint (Point point,uint nflags) const;
Used to determine the child window that contains the specified point
If the specified point is outside the client area, the function returns null;
If the specified point is in the client area but does not belong to any of the child windows, the function returns the pointer to the CWnd;
If multiple child windows contain a specified point, the pointer to the first child window is returned.
Also note that the function returns a pseudo window pointer and cannot be saved for later use.
There are several meanings for the second argument nflags:
Cwp_all file://does not ignore any child windows
Cwp_skipnivsible file://ignore Invisible child windows
cwp_skipdisabled file://Ignore prohibited child windows
Cwp_skipransparent file://Ignore transparent child windows

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.