Professional User Interface Suite, Copyright FOSS Software Inc. Help Published with Permission.

You can insert one or more additional buttons to the toolbar by inserting the following code snippet immediately after you have created the toolbar in the OnCreate() method of the mainframe:

int nButtonsCount =  m_wndToolbar.GetButtonsCount();
 
   // Let's append the separator to the end
m_wndToolbar.InsertButton(
   nButtonsCount++,
   ID_SEPARATOR
);
 
   // Let's append the button specified with ID_YOUR_COMMAND
m_wndToolbar.InsertButton(
   nButtonsCount ++,
   ID_YOUR_COMMAND
);

Please note that any command identifier such as ID_YOUR_COMMAND that you are going to use in toolbars must be registered in the command manager. The Prof-UIS toolbar control stores only command identifiers and gets the additional information related to them (corresponding icons, tooltips, and etc.) from the command manager. The following code may be used to allocate a new dynamic command identifier in the command manager:

// Set it to zero to allow the command manager
// to find the dynamic identifier atomatically
UINT nCmdID = ID_YOUR_COMMAND;
LPCTSTR strProfileName =
   g_CmdManager->ProfileNameFromWnd(
   m_wndToolbar.GetSafeHwnd()
);
ASSERT( strProfileName != NULL );
CExtCmdItem * pCmdItem =
   g_CmdManager->CmdAllocPtr(
   strProfileName,
   nCmdID
);
ASSERT( pCmdItem != NULL );
   pCmdItem->m_sTipTool = _T("tooltip text");
   pCmdItem->m_sTipStatus = _T("statusbar text");
   pCmdItem->m_sMenuText = _T("text in menu");
   pCmdItem->m_sToolbarText = _T("text in toolbar");
// you may also load an 16x16 icon
// and assign it to the command
HICON hIcon = (HICON)::LoadImage(
   ::AfxGetResourceHandle(),
   MAKEINTRESOURCE(IDI_YOUR_ICON),
   IMAGE_ICON,
   16,
   16,
   0
);
ASSERT( hIcon != NULL );
g_CmdManager->CmdSetIcon(
   strProfileName,
   pCmdItem->m_nCmdID,
   hIcon,
   false // Use the hIcon handle "as is"
);

You can also allocate new commands and insert buttons into toolbars dynamically at run-time, after the execution of CMainFrame::OnCreate() is completed. For this purpose, the same code lines may be used but then you need to recalculate the layout of the toolbar's parent window:

CFrameWnd * pFrame =
   m_wndToolbar.GetParentFrame();
ASSERT_VALID( pFrame );
pFrame->RecalcLayout();

Finally, if you want to remove a button from a toolbar, you need to know the button's index first.

int nButtonIndex =
   wndToolbar.CommandToIndex( nCmdID );
if( nButtonIndex >= 0 )
   wndToolbar.RemoveButton( nButtonIndex );