Microsoft KB Archive/126301

= FIX: CMDIFrameWnd Caption Not Updated When Child Closed =

Article ID: 126301

Article Last Modified on 11/21/2006

-

APPLIES TO

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

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 2.0 Professional Edition 

-

<div class="notice_section">

This article was previously published under Q126301

<div class="symptoms_section">

SYMPTOMS
Closing a maximized MDI child window will not update the main frame window caption if the frame window is narrower than the menu bar (menu has wrapped to the second line). This causes the main frame window to show a document name even if no documents are open.

<div class="cause_section">

CAUSE
If the main frame window is narrower than the menu, CMDIChildWnd::OnSize is called twice, which calls GetMDIFrame->OnUpdateFrameTitle(TRUE) to update the frame window caption. The last time OnUpdateFrameTitle(TRUE) is called, it updates the frame window caption with the previous document name, even though the document is closed.

<div class="resolution_section">

RESOLUTION
Override CMDIChildWnd::DestroyWindow to turn off FWS_ADDTOTITLE for the main frame window during the destroy window call for maximized child windows. Follow these steps:

<ol> Derive a new MDI child window class from CMDIChildWnd, perhaps by using Class Wizard.</li>  For the new class, override the DestroyWindow function by using the following source code: BOOL CMyMDIChildWnd::DestroyWindow {   // Turn off the FWS_ADDTOTITLE style for the main frame window // if it is a maximized main frame window. BOOL bMaximized = (GetStyle & WS_MAXIMIZE); CMDIFrameWnd* pFrameWnd = GetMDIFrame; DWORD dwStyle; if (bMaximized) {       dwStyle = ::SetWindowLong(pFrameWnd->m_hWnd, GWL_STYLE,            pFrameWnd->GetStyle & ~FWS_ADDTOTITLE); }

// Go ahead and destroy and MDI child window. BOOL bResult = CMDIChildWnd::DestroyWindow;

// Restore the default window style for the main frame window. if (bMaximized) {       ::SetWindowLong(pFrameWnd->m_hWnd, GWL_STYLE, dwStyle); }

return bResult; }                       </li>  In the project .CPP file, be sure to use the newly derived MDI child window class to replace CMDIChildWnd in the CMultiDocTemplate statement, as in this example: #include "mdichild.h"         // header file for CMyMDIChildWnd

BOOL CMyWinApp::InitInstance {   ...    CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate(       IDR_PROJECTTYPE,        RUNTIME_CLASS(CMyDocument),        RUNTIME_CLASS(CMyMDIChildWnd),     // use it here!!!        RUNTIME_CLASS(CMyView));

AddDocTemplate(pDocTemplate); ...  }                        </li></ol>

Here PROJECT is the project name, CMyDocument is the CDocument class, CMyMDIChildWnd is the CMDIChildWnd class, and CMyView is the CView class.

<div class="status_section">

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Visual C++ version 2.10 for Windows NT.

<div class="moreinformation_section">

MORE INFORMATION
When the child window is maximized and the main frame window is wider than the menu, the CMDIChildWnd::OnSize function does not get called when the child window is closed. However, when the frame window is narrower than the menu, CMDIChildWnd::OnSize gets called twice when the child window is closed, first with SIZE_MAXIMIZED and then with SIZE_RESTORED.

CMDIChildWnd::OnSize calls GetMDIFrame->OnUpdateFrameTitle(TRUE). The first time CMDIChildWnd::OnSize is called (with SIZE_MAXIMIZED), OnUpdateFrameTitle updates the frame window's caption correctly, but redundantly. The second time MDIChildWnd::OnSize is called (with SIZE_RESTORED), OnUpdateFrameTitle decides there is a current document because its call to GetActiveDocument returned a valid pointer and it updates the frame window's caption with the document name.

This bug can be seen in the MFC sample "MULTIPAD" included with Visual C++.

Additional query words: 1.50 1.51 1.00 2.00 2.50 2.51 2.10 3.00

Keywords: kbbug kbdocview kbfix kbmdi kbnoupdate kbprogramming kbuidesign kbvc210fix KB126301

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.