Microsoft KB Archive/99182

= How To Change an MFC-Based MDI Child Window's Frame Text =

Article ID: 99182

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++ 1.0 Professional Edition</li></ul>

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

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

 Microsoft Visual C++ 4.1 Subscription</li></ul>

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

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

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q99182

<div class="notice_section">

<div class="summary_section">

SUMMARY
A user may find it desirable to change the title of an MFC-based child window. To do this, PreCreateWindow must be overridden for the child frame, and OnInitialUpdate must be overridden for the view.

<div class="moreinformation_section">

MORE INFORMATION
These are the steps necessary to change the title of a multiple document interface (MDI) child window frame. When doing this, follow the rules of &quot;The Windows Interface: An Application Design Guide&quot; for child frame titles, which states the following: <pre class="fixed_text">  ...a document window title bar should contain a caption that displays the name of the document in the window. Add your customized information in addition to that. <ol> <li>Create an MFC MDI application.</li> <li>Add a new class based on CMDIChildWnd. These steps assume the class is named CMyChildFrame.</li> <li>Choose the project's main .CPP file and replace the CMDIChildWnd class in the call to the AddDocTemplate function to the new class you created.</li> <li>Include the newly created .H file at the top of the project's main .CPP file.</li> <li> Choose the .H file from the project list for the newly created class and add the following line to the protected implementation section: virtual BOOL PreCreateWindow(CREATESTRUCT &cs); NOTE: With Visual C++ versions 4.x or 5.0, you may use ClassWizard to do the above. </li> <li> Choose the .CPP file from the project list for the newly created class and add the following to the end of the file: BOOL CMyChildFrame::PreCreateWindow(CREATESTRUCT &cs) {     // Do default processing. if(CMDIChildWnd::PreCreateWindow(cs)==0) return FALSE;

return TRUE; } where CMyChildFrame is the class name of your newly created class. This function calls the base class's version for the PreCreateWindow function. </li> <li> Add the following code line to the newly created PreCreateWindow function immediately following the default call to CMDIChildWnd::PreCreateWindow: cs.style&=~(LONG)FWS_ADDTOTITLE; </li> <li> Choose the .H file corresponding to your application's view window and add the following code to the public implementation section: virtual void OnInitialUpdate; NOTE: With Visual C++ versions 4.x or 5.0, you may use ClassWizard to do the above. </li> <li> Choose the .CPP file corresponding to your application's view window and add the following code to the end of the file: void CMyAppView::OnInitialUpdate {        // Do default processing. CView::OnInitialUpdate; } where CMyAppView is the view class for the application. </li> <li> Add your customization code. Remember to follow the rules of &quot;The Windows Interface: An Application Design Guide&quot; for child frame titles. Then add your customized information, in addition to that, immediately following the default call to CView::OnInitialUpdate. An example of this is: GetParent->SetWindowText(GetDocument->GetTitle+        &quot; - This is a test!&quot;); </li> <li>Build the program.</li> <li>Run it and you will see the change implemented. In this example, the first view will display &quot;Myapp1 - this is a test!&quot;. Additional views will be identical except for the document name (Myapp1).</li></ol>

NOTE: It is necessary to override OnInitialUpdate instead of OnCreate because the document has not been instantiated, and therefore calling GetTitle would return an empty string.

NOTE: It may also be necessary to override CDocument::CanCloseFrame if multiple views of the same document are open. This is because the CMDIChildWnd member m_nWindow is not updated if FWS_ADDTOTITLE is not set for the MDI Child frame. This prevents the &quot;Save File&quot; dialog from coming up when any view other than the last view open for the document is closed.

This does NOT apply to Visual C++ for Windows, version 1.52, and Visual C++ 32-bit Edition, versions 2.10 and above. The behavior of m_nWindows was modified in these versions to handle this scenario properly.

The overridden function would look something like this: BOOL CMyDoc::CanCloseFrame(CFrameWnd* pFrameArg) {        ASSERT_VALID(pFrameArg); UINT iCount = 0;

POSITION pos = GetFirstViewPosition; while (pos != NULL) {           CView* pView = GetNextView(pos); ASSERT_VALID(pView); CFrameWnd* pFrame = pView->GetParentFrame; // assume frameless views are ok to close if (pFrame != NULL) {              // assumes 1 document per frame ASSERT_VALID(pFrame); iCount++; }     }          if ( iCount > 1 ) return TRUE;

// otherwise only one frame that we know about return SaveModified; }

Keywords: kbhowto kbuidesign kbdocview kbmdi KB99182

-

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

© Microsoft Corporation. All rights reserved.