MFC - How to set the toolbar title?

1.2k Views Asked by At

The title of the toolbar and also the menu item ID_VIEW_TOOLBAR remains empty. I have tried to set it within CMainFrame::OnCreate:

CString strToolbar;
strToolbar.LoadStringA(IDS_TOOLBAR_STANDARD); // IDS_TOOLBAR_STANDARD = "Standard"
m_wndToolBar.SetWindowText(strToolbar);
DockPane(&m_wndToolBar);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
EnablePaneMenu(TRUE, ID_ANSICHT_ANPASSEN, "Anpassen", ID_VIEW_TOOLBAR);

The menu Looks like following:

Empty ID_VIEW_TOOLBAR title

Do you know how to set it properly?

2

There are 2 best solutions below

0
On BEST ANSWER

To solve this problem, I had create the Toolbar by explicitly specifying the ID of the Toolbar (instead of using the default argument of the function).

    m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP |
        CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC,
        CRect(1,1,1,1),
        IDR_MAINFRAME) // <-- Custom ID

After this I could set the title of the toolbar to what I want:

m_wndToolBar.SetWindowText(_T("My Toolbar Name");

I got it working after reading this thread.

0
On

Generally speaking people do not change the display text of standard commands at runtime. At compile time you can set the ID's menu text in in the string table section of the resource editor.

If you want to change the menu text at runtime, you can add an ON_UPDATE_COMMAND_UI handler for ID_VIEW_TOOLBAR and call CCmdUI::SetText inside the handler. When the user click a top level menu to display a drop-down menu, a WM_INITMENUPOPUP is sent to the owner of the menu, in this case your CFrameWnd-derived class. CFrameWnd::OnInitMenuPopup then iterates through each menu item and calls the update command UI handlers if exists.

Reference MFC TN021: Command and Message Routing