Methods for getting pointers in MFC

Source: Internet
Author: User

1. Obtain the applicationProgramPointer

Cmyapp * PAPP = (cmyapp *) afxgetapp ();

2. Obtain the master framework pointer

The Public member variable m_pmainwnd in cwinapp is the pointer of the main framework.

Cmainframe * pmainframe = (cmainframe *) (afxgetapp ()-> m_pmainwnd );

Or

Cmainframe * pmainframe = (cmainframe *) afxgetmainwnd ();

3. Get menu pointer

Cmenu * pmenu = afxgetmainwnd ()-> getmenu ();

4. Get the toolbar and status bar pointer

M_wndtoolbar and m_wndstatusbar can be directly used in the main framework.

Others:

Ctoolbar * ptoolbar = (ctoolbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_toolbar );

Cstatusbar * pstatusbar = (cstatusbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_status_bar );

5. Get the control pointer

Use getdlgitem () before conversion, for example:

Cbutton * pbutton = (cbutton *) getdlgitem (idc_mybutton );

6. Get documents and 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 and views

Get document pointer from view:

Cyourdoc * pdoc = getdocument ();

Get view pointer from document:

Traverse using the member functions getfirstviewposition () and getnextview ()

Virtual position getfirstviewposition () const;

Virtual cview * getnextview (Position & rposition) const;

SDI:

Cyourview * pview;

Position Pos = getfirstviewposition ();

Pview = getnextview (POS );

MDI:

Define 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:

Cyourview * pview = (cyourview *) getview (runtime_class (cyourview ));

8. document templates and documents

Getting the document template pointer from the document:

Cdoctemplate * getdoctemplate () const;

Get the document pointer from the document template:

Viaual position getfirstdocposition () const = 0;

Visual cdocument * getnextdoc (Position & rpos) const = 0;

9. Obtain the pointer of each view in the split view

Defined in the main framework: csplitterwnd m_wndsplitter;

Define two view classes: cview1 and cview2

Reload in the framework class:

Bool cmainframe: oncreateclient (lpcreatestruct, ccreatecontext * pcontext)

{

Verify (m_splitter.createstatic (this,); // split it into two rows and one column

Verify (m_splitter.createview (100,100, runtime_class (cview1), csize (), pcontext ));

Verify (m_splitter.createview (100,100, runtime_class (cview2), csize (), pcontext ));

Return true;

}

Obtain the split view pointer

Cview1 * pview1 = (cview1 *) m_wndsplitter.getpane (0, 0 );

Cview2 * pview2 = (cview2 *) m_wndsplitter.getpane (1, 0 );

10. Obtain the Child Window pointer with the mouse

Cwnd * childwindowfrompoint (point) const;

Cwnd * childwindowfrompoint (point, uint nflags) const;

Used to determine the subwindow containing the specified Vertex

If the specified point is outside the customer area, the function returns NULL;

If the specified point is in the customer region but does not belong to any subwindow, the function returns the cwnd pointer;

If multiple subwindows contain the specified vertex, the pointer of the first subwindow is returned.

Note that the function returns a pseudo window pointer and cannot be saved for future use.

The second parameter nflags has several meanings:

Cwp_all file: // do not ignore any subwindow

Cwp_skipnivsible file: // ignore invisible subwindow

Cwp_skipdisabled file: // ignore the prohibited subwindow

Cwp_skipransparent file: // ignore transparent subwindow

Get the pointer of the toolbar

By default, there is a default tool bar afx_idw_toolbar. We can obtain the tool bar pointer based on the corresponding ID. The method is as follows:

Ctoolbar * ptoolbar = (ctoolbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_toolbar );

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.