It is really difficult to get the view pointer from the CWinApp in MFC (applies to SDI and MDI)

Source: Internet
Author: User

In SDI and MDI of MFC, you want to get CXXXView from your CXXXApp (of course it inherits from CWinApp) (of course it inherits from CView) the view pointer is not an easy task. The following describes how to get it. OK. Let's get started.

If we want to get the view pointer, first we need to get the pointer to the document and how to get the pointer to the document class, first we need to get the pointer to the Document Template (that is, CDocTemplate ), it is easy to get the pointer to the document template through the CWinApp class.

The specific process is as follows:

1) obtain the document template location

2) Get the document template pointer Based on the template location

3) obtain the document location based on the document template

4) Get the document pointer based on the document location

5) view location based on the document

6) obtain the view pointer Based on The View position. This is what we need.

Do you think it is painful.

See:

OK. The specific implementation code is provided below.

// Get the position of the first document template

POSITION pos = GetFirstDocTemplatePosition ();

// Do we have a valid template position?

If (pos)

{

// Get a pointer to the document template

CDocTemplate * pDocTemp = GetNextDocTemplate (pos );

// Do we have a valid pointer?

If (pDocTemp)

{

// Get the position of the first document

POSITION dPos = pDocTemp-> GetFirstDocPosition ();

// Do we have a valid document position?

If (dPos)

{

// Get a pointer to the document

CTaskingDoc * pDocWnd =

(CTaskingDoc *) pDocTemp-> GetNextDoc (dPos );

// Do we have a valid pointer?

If (pDocWnd)

{

// Get the position of the view

POSITION vPos = pDocWnd-> GetFirstViewPosition ();

// Do we have a valid view position?

If (vPos)

{

// Get a pointer to the view

CTaskingView * pView = (CTaskingView *) pDocWnd-> GetNextView (vPos );

}

}

}

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.