Tuesday, February 24, 2009

[VC++] Removing 'Untitled' from Doc/View App Titlebar

Most of you may get wearied of seeing the 'Untitled' caption on your title bar when you create a Document/View Architecture application, say SDI application. What we actually seeing on an SDI title bar is Document Name - Application name, by default your document name will be 'Untitled' and application name will be IDR_MAINFRAME's value in the string table. The reason why you are seeing this is because of a style set by default FWS_ADDTOTITLE to the CREATESTRUCT data structure. So inorder to remove the 'Untitled'(or document name) from your mainframe title bar, remove the FWS_ADDTOTITLE style from the create structure of the mainframe window. There is one more style which worth a mention at this point, FWS_PREFIXTITLE this is the one responsible showing the document name followed by the application name. If we remove this style and if FWS_ADDTOTITLE is not removed then window will show title as Application name - Document Name.

Suppose if my Application name is TestGlobalObjects then see the default title bar with Untitled.


Changing Document Prefix Title to Postfix

See the below code modification in CMainFrame::PreCreateWindow() removing the FWS_PREFIXTITLE and see the change in window title bar caption.
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;

// Removing Pre Fix style
cs.style &= ~FWS_PREFIXTITLE;
return TRUE;
}



Removing document from title

See the below code and see the Untitled removed from window title bar on executing the below code.

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;

// Removing the Add To Title style
cs.style &= ~FWS_ADDTOTITLE;
return TRUE;
}

No comments: