How to get rid of “Untitled – MyApp” in MFC

Microsoft outdid themselves when they designed MFC such that
applications are automatically named according to the document
view paradigm. When you use MFC to create either an SDI or MDI
application, your main window is probably called something like
“Untitled – MyApp.” If you don’t particularly care for the
document view paradigm, other than perhaps wishing to benefit
from the separation of user interface and program core,
then you’re left in the dark…

To get rid of the “Untitled” alone, you can override the
CDocument virtual function “SetTitle” as shown below.

   void CMyDoc::SetTitle(LPCTSTR lpszTitle)
     {
       CDocument::SetTitle("MyTitle");
     }

This will produce a main window title of the type
“MyTitle – MyApp”. But perhaps all you want is “MyApp”.

You can set the main window title from just about anywhere
in your application using the statement shown below.

   (AfxGetMainWnd( ))->SetWindowText("MyApp");

The problem with this approach is that MFC in its
infinite wisdom will reset your window title to
the “Document – App” default as soon as a
document object is constructed. If you care to change
MFC’s default behavior ( not advised ) look up WINMDI.CPP;
it’s the culprit.

Finally, you can overwrite the CFrameWnd virtual function
“OnUpdateFrameTitle” in your apps CMainFrame class. Bud
Milwood, a friend of mine, pointed out the function’s very
existence when I was loosing my sanity browsing the MFC
online help. Don’t try to look up “OnUpdateFrameTitle” in
the Microsoft Developer Studio online help. It’s not there.
So use it merrily, but use it wisely, subsequent versions
of MFC may not support it. The following code snippet shows
how…

   void CMainFrame::OnUpdateFrameTitle(BOOL Nada)
    {
      // get app name from string table resource
      //----------------------------------------
      CString csAppName;
      csAppName.Format(AFX_IDS_APP_TITLE);

      // Set caption of main frame window
      //---------------------------------
      SetWindowText(csAppName);
    }

Another and probably safer method has been
brought to my attention by Stephen Michael Schimpf at CyberSky.Simplenet.Com.
You can modify the window style in ‘PreCreateWindow’ as follows:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    cs.style &= ~(LONG) FWS_ADDTOTITLE;

    return CFrameWnd::PreCreateWindow(cs);
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read