Obtain various MFC pointers

Source: Internet
Author: User

 

Obtain various MFC pointers

1. Get the application pointer

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 point) const;

CWnd * ChildWindowFromPoint (POINT 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 );

Is it easy?

 

Get the pointer of the status bar

In the default status, there is a default status bar AFX_IDW_STATUS_BAR. We can also obtain the status bar pointer based on the corresponding ID, as follows:

CStatusBar * pToolBar = (CStatusBar *) AfxGetMainWnd ()-> GetDescendantWindow (AFX_IDW_STATUS_BAR );

 

There are two methods.

1. Call CWnd: GetDlgItem to obtain a CWnd * pointer and call the member function. For example, to obtain the CButton pointer, use the following method:

CButton * pButton = (CButton *) GetDlgItem (IDC_MYBUTTON );

2. You can use ClassWizard to associate the control with the member variables. In ClassWizard, simply select the Member Variables tag, and then select Add Variable... Button. In the dialog resource editor, press Ctrl and double-click the control to go to The Add Member Variable dialog.

We can use the GetFirstView () and GetNextView () member functions of the document class to traverse the view.

 

In fact, there is a ready-made member function in the View class for us to use, that is: GetDocument (); using it, we can easily get the document class pointer. Let's take a look at GetDocument () first () function implementation:

CColorButtonDoc * CColorButtonView: GetDocument ()

{

ASSERT (m_pDocument-> IsKindOf (RUNTIME_CLASS (CColorButtonDoc )));

Return (CColorButtonDoc *) m_pDocument;

}

In fact, m_pDocument is forcibly converted to CColorButtonDoc *, which is what we want.

Here we can use GetActiveXXXXX () to remove the currently activated documents and views:

CMyDoc * pDoc = (CMyDoc *) GetActiveDocument ();

CMyView * pView = (CMyView *) GetActiveView ();

 

This is simple. one sentence:

CMyApp * pApp = (CMyApp *) AfxGetApp ();

In the CWinThread class, there is a public member variable: CWnd * m_pMainWnd; its main purpose is to provide us with the CWnd pointer, which we can use to achieve our goal:

CMainFrame * pFrame = (CMainFrame *) (AfxGetApp ()-> m_pMainWnd );

 

Obtain the Child Window pointer with the mouse

Here we need to use a less commonly used function: ChildWindowFromPoint. The prototype is as follows:

CWnd * ChildWindowFromPoint (POINT point) const;

CWnd * ChildWindowFromPoint (POINT point, UINT nFlags) const;

This function is used to determine the subwindow that contains the specified vertex. If the specified vertex is outside the customer zone, the function returns NULL. If the specified vertex is in the customer zone, 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. However, it should be noted that the function returns a pseudo window pointer and cannot be saved for future use.

Ignore any sub-windows that are slightly invisible to sub-windows that are slightly forbidden

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 get 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 the moban object of the current document.

6. You can 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, and 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.

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,

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 ");

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 ));

Multiple document templates can also be added to a single document, but the development usually 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.sixxs.org/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.

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.