Microsoft KB Archive/129649

= PRB: GetActiveView Returns NULL for an MDI Frame Window =

Article ID: 129649

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Learning Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q129649

<div class="symptoms_section">

SYMPTOMS
The GetActiveView function returns the active view (if any) attached to a frame window (CFrameWnd). This function returns NULL when called for an MDI main frame window (CMDIFrameWnd).

<div class="cause_section">

CAUSE
In an MDI application, the MDI main frame window (CMDIFrameWnd) does not have a view associated with it. Instead, each individual child window (CMDIChildWnd) has one or more views associated with it. Therefore, a call to the GetActiveView function for an MDI main frame window returns NULL. The "More Information" section of this article gives the layout of the different windows in an MDI application.

<div class="resolution_section">

RESOLUTION
To resolve the problem, find the active MDI child window (if any), and then call the GetActiveView function for that child window. The active MDI child window can be found by calling the MDIGetActive or GetActiveFrame function, as demonstrated in this code: CMDIFrameWnd *pFrame = (CMDIFrameWnd *)AfxGetApp->m_pMainWnd; // Get the active MDI child window. CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame; /* or CMDIChildWnd *pChild = pFrame->MDIGetActive; */   // Get the active view attached to the active MDI child window. CMyView *pView = (CMyView *) pChild->GetActiveView;

<div class="moreinformation_section">

MORE INFORMATION
The client area of the MDI frame window is covered by the client window referred to as the MDICLIENT, also known as the Workspace. This client window in turn contains zero or more child windows, each of which can display a view, as in this diagram: <pre class="fixed_text">                   --- |     Frame Window         |  ---> CMDIFrameWnd | (main application window) | ---                            |                   |   Client Window    |  ---> MDICLIENT window class | (no view attached) | |        ---         |                            | -       -      As illustrated in this diagram, the MDI frame window does not have a view directly associated with it. The views are actually associated with the MDI child windows.
 * Child Window 1   |    |   Child Window 2   |  ---> CMDIChildWnd
 * (one or more views) |   | (one or more views |

Therefore, calling the GetActiveView function for an MDI main frame window returns NULL, implying that there is no view directly associated with MDICLIENT. The active view in an MDI application can be obtained by first finding the active MDI child window and then finding the active view for that child window as outlined in the Resolution section of this article.

<div class="references_section">