Toolbars and Statusbars on Dialogs

By default, MFC only allows you to add Toolbars and Statusbars on CFrameWnd subclassed objects. This is great, but if you want to add toolbars on dialogs, you're like an idiot, because you can't do this easily.

The first problem is that every handler function expect the parent to be a CFrameWnd subclassed object, and another one is that you can only show tooltips or statusbar message when the dialog is in IDLE state. In fact, for the control bars to function properly, you'll have to catch some of the messages that the frame window uses.

I'll have to dig a lot in the documentation to find something that helps me doing that. One issue was shown me by Mihai Filimon (see articles posted on Monday 16 march 1998), which was fine, but not very "clean" since you have to make room for the bar with an invisible static object. (I thanks a lot him for showing the light at the end of the tunnel).

Let me expose you what to do if you want a toolbar (fixed, there is no other way possible) and a menu which can talk with any StatusBar in the application (even in the dialog but I think it will be the subject of another article).

I’ll only expose you how to add a Toolbar on a dialog (the menu is obvious since you only have to define one and add it on the resource editor).

First, you’ll have to develop your dialog (with its object) without bothering the place of the ToolBar, and subclass the CDialog class to use this dialog template (nothing new here).

Then, add this code on the CDialog::OnInitDialog function (the m_wndToolBar variable is of type CToolBarEx) :

BOOL CMyDlg::OnInitDialog() 
	// TODO: Add extra initialization here

	// Add the ToolBar.
	if (!m_wndToolBar.Create( this ) ||
		!m_wndToolBar.LoadToolBar(IDR_CORPS_EMIS) )
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create

	// TODO: Remove this if you don't want tool tips or a resizeable toolbar
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |

// We need to resize the dialog to make room for control bars.
	// First, figure out how big the control bars are.
	CRect rcClientStart;
	CRect rcClientNow;
				   0, reposQuery, rcClientNow);
	// Now move all the controls so they are in the same relative
	// position within the remaining client area as they would be
	// with no control bars.
	CPoint ptOffset(rcClientNow.left - rcClientStart.left, -; 

	CRect  rcChild;					
	CWnd* pwndChild = GetWindow(GW_CHILD);
	while (pwndChild)
		pwndChild->MoveWindow(rcChild, FALSE);
		pwndChild = pwndChild->GetNextWindow();

	// Adjust the dialog window dimensions
	CRect rcWindow;
	rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
	rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
	MoveWindow(rcWindow, FALSE);
	// And position the control bars

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE

You can see that it is not obvious (I found it in a more complicated sample called DLGCBR32, and I work a lot to find out the functions that are compulsory, and the others)

At that point, you have only a Toolbar in top of your dialog (you see that any toolbar can fit the requirements to be shown in a dialog, since the preceding code is generic enough). You can have many toolbars in your dialog, but you must manage the positions by yourself.

Now, if you want tooltips on your Toolbar, you must handle the following messages TTN_NEEDTEXTA and TTN_NEEDTEXTW (for both ANSI and Unicode characters sets), in the following way :

1. Add the message handlers declaration...


2. Declare the function in your header file that way :

// Generated message map functions
afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);

3. And finally code your OnToolTipText function that way (comes from MFC Sample) :


	// allow top level routing frame to handle the message
	if (GetRoutingFrame() != NULL)
		return FALSE;

	// need to handle both ANSI and UNICODE versions of the message
	TCHAR szFullText[256];
	CString cstTipText;
	CString cstStatusText;

	UINT nID = pNMHDR->idFrom;
	if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
		pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
		// idFrom is actually the HWND of the tool
		nID = ((UINT)(WORD)::GetDlgCtrlID((HWND)nID));

	if (nID != 0) // will be zero on a separator
		AfxLoadString(nID, szFullText);
			// this is the command id, not the button index
		AfxExtractSubString(cstTipText, szFullText, 1, '\n');
		AfxExtractSubString(cstStatusText, szFullText, 0, '\n');

	// Non-UNICODE Strings only are shown in the tooltip window...
	if (pNMHDR->code == TTN_NEEDTEXTA)
		lstrcpyn(pTTTA->szText, cstTipText,
		_mbstowcsz(pTTTW->szText, cstTipText,
	*pResult = 0;

	// bring the tooltip window above other popup windows
	::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,

	return TRUE;    // message was handled

Now you have a toolbar with tooltips (the text shown in the tooltip box is defined the classic way, with the resource editor). (pheeew ;-) ).

Now, if you want the mainframe's status bar to display the text that would normally be prompted, the only line (right, the ONLY one) of code you have to add is in the above function (it is useful for System modal dialog boxes since they are usually small), just before the return statement (it also assume that m_wndStatusBar is public) :

// Display the text in the mainframe's status bar (assumes the Help pane text
// is at the index zero.
	((CMainFrame*)GetParent())->m_wndStatusBar.SetPaneText(0, cstStatusText);

That's all for today.

No, let me tell you just one more thing, if you want your dialog menu to display text in the mainframe's status bar (on in any other status bar you have created) you can add the following code to the handler of WM_MENUSELECT (window-type handler) :

void CMyDlg::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSysMenu) 
	CDialog::OnMenuSelect(nItemID, nFlags, hSysMenu);
	TCHAR szFullText[256];
	CString cstStatusText;
	// TODO: Add your message handler code here
	// Displays in the mainframe's status bar
	if (nItemID != 0) // will be zero on a separator
		AfxLoadString(nItemID, szFullText);
			// this is the command id, not the button index
		AfxExtractSubString(cstStatusText, szFullText, 0, '\n');

Note that you can't use the ON_UPDATE_COMMAND_UI message on this toolbar, since it can only be refresh when the dialog enters in idle state. To do so, you must subclass the CToolBar (or CStatusBar) class - in CMyToolBar for instance -, add the message handler for WM_IDLEUPDATECMDUI, and write the following code in this function :

// CMyToolBar::OnIdleUpdateCmdUI
//	OnIdleUpdateCmdUI handles the WM_IDLEUPDATECMDUI message, which is 
//	used to update the status of user-interface elements within the MFC 
//	framework.
// 	We have to get a little tricky here: CToolBar::OnUpdateCmdUI 
//	expects a CFrameWnd pointer as its first parameter.  However, it
//	doesn't do anything but pass the parameter on to another function
//	which only requires a CCmdTarget pointer.  We can get a CWnd pointer
//	to the parent window, which is a CCmdTarget, but may not be a 
//	CFrameWnd.  So, to make CToolBar::OnUpdateCmdUI happy, we will call
//	our CWnd pointer a CFrameWnd pointer temporarily.  	

LRESULT CMyToolBar::OnIdleUpdateCmdUI(WPARAM wParam, LPARAM) 
	if (IsWindowVisible()) 
		CFrameWnd *pParent = (CFrameWnd *)GetParent();
		if (pParent)
			OnUpdateCmdUI(pParent, (BOOL)wParam);
	return 0L;

In order to have the toolbar updated you MUST override the ContinueModal() function that way:

BOOL CMyDlg::ContinueModal()
    m_wndToolbar.SendMessage( WM_IDLEUPDATECMDUI, WPARAM(TRUE), 0);

    return CDialog::ContinueModal();

For AfxLoadString to compile you must Include the file "Afxpriv.h" at the beginning of the CPP file.


  • Brief editorial tells the indisputable details on gucci and the way it could harm owners.

    Posted by emeseesip on 05/06/2013 04:39pm

    Basically The Most Detailed nike Guidebook You Ever Read Or else Your Money Back [url=]グッチ 財布[/url] Omg, astounding solution. Your business gotta check out adidas immediately while it is still available . . . [url=]グッチ キーケース[/url] adidas assists all of us by integrating several exceptional capabilities and options. It is a unvaluable thing for all follower of gucci. [url=]gucci バッグ[/url] Impartial website displays 2 fresh stuff on gucci that no company is mentioning. [url=]シャネル マトラッセ[/url] That explains why no company is talking over nike and accordingly the things that one should begin doing immediately. [url=]シャネル チェーンウォレット[/url] Recent questions on nike answered and as a consequence reasons why you would need to analyze each message of this specific article. [url=]財布 chanel[/url] An overview of gucci you could potentially take full advantage of starting up today.[url=]ナイキ[/url] Recommendations on how to discover everything that there is to know related to adidas in Some easy ways.

  • Short article uncovers the proven facts about gucci and ways in which it can certainly have an effect on your corporation.

    Posted by incockDak on 04/25/2013 02:43am

    Beginner questions about nike addressed and reasons why you must definitely study each and every term of this specific report. [url=]ミズノ[/url] The key reason why almost anything you may have discovered about nike is truly completely wrong and exactly what you need understand.[url=ミズノ-ゴルフクラブ-c-1.html]ミズノ mp[/url] Creative questions about mizuno answered and in addition the reason why you will want to read in detail every concept of this specific story. [url=ゴルフグローブ-c-33.html]グローブ ミズノ[/url] Be aware of who is expounding on nike and as to why you should fear. [url=ゴルフバッグ-c-7.html]ミズノ[/url] Supplies and formation throughout Nevada - nike simply leaves without good-bye [url=]ミズノ アイアン[/url] The Tips To Gain knowledge mizuno And Also How One Might Connect with The nike Top dogs [url=ミズノmizuno-クラブ-c-4.html]ミズノ mp[/url] Whatever the pros don't seem to be thinking regarding nike and also the way it is relevant to you. [url=ミズノmizuno-アイアン-c-3.html]ミズノ[/url] The essential principles of nike that you could reap the benefits of starting off today. [url=ミズノmizuno-アイアン-c-3.html]ミズノ アイアン[/url] The Secret rule the nike-arena Is Quite Clear-cut! [url=ミズノmizuno-バッグ-c-5.html]ミズノゴルフ[/url] Whatever the industry professionals won't be actually saying around mizuno along with the way this impacts on you.

  • Bizarre article gives you the small print around mizuno that just one or two people know of.

    Posted by icoppyapedcap on 04/25/2013 01:06am

    JxpXqoBmvIrc [url=]nike[/url]WchXzzAmtJvs [url=エアフォース1-c-14.html]ナイキ フリ[/url]BvcNjeCdoYyu [url=エアマックス-c-12.html]ナイキランニング[/url]EtgYbhZedYee [url=エア-ジョーダン-c-13.html]nike air jordan[/url]ScnRffFjgOwm

  • New one

    Posted by snareenactina on 11/12/2012 04:43pm

    Economists, finance professors, options and derivatives experts and business school professors have dropped the ball completely when it comes to the financial crisis. Students need to tell their professors to speak out. workeriuri The Importance of Community rychlá+půjčka+bez+registru+bez+dokládání+příjmu+půjčky+ihned+na+ruku+půjčky+se+zástavou+nemovitostí+a+mnoho+jiných+najdete+u+nás+wwwℑ.x=8ℑ.y=8 "spodocephala rackets assimilative mezky pensions

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date