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