HomeC&C++WainTutorialsSamplesTip&TrickTools


Then we will add a toolbar to our application.
It will be a simple toolbar with buttons, with text and bitmaps.
First we must create the window itself
   ToolBarWindow = CreateWindowEx(0,
                                  TOOLBARCLASSNAME,
                                  0,
                                  WS_CHILD| WS_BORDER,
                                  0, 0, 0, 0,
                                  ParentWindow,
                                  (HMENU )ToolBarId,
                                  InstanceHandle,
                                  0);
No surprise here, except the wnd-class name, TOOLBARCLASSNAME, which is the name of a predefined wnd-class for toolbars.
ToolBarId is the ID for the toolbar, it's defined the same way as StatusBarId, in the same enumeration.
The next to do is to TB_BUTTONSTRUCTSIZE message to the window, I'm not sure why this is needed, but the documentation say we have to:
   SendMessage(ToolBarWindow,
               TB_BUTTONSTRUCTSIZE,
               (WPARAM )sizeof(TBBUTTON),
               0); 
Next we have to tell the toolbar which bitmap to use, in this simple case we will use a standard set, it has 14 images to choose from, it fits nicely to this demo purpose.
   TBADDBITMAP TBaddBitmap;
   TBaddBitmap.hInst = HINST_COMMCTRL;
   TBaddBitmap.nID = IDB_STD_SMALL_COLOR;
   SendMessage(ToolBarWindow,
               TB_ADDBITMAP,
               (WPARAM )NumButton,
               (LPARAM )&TBaddBitmap);
Notice that we use HINST_COMMCTRL as instance, as the bitmap is stored in the comctrl dll.
NumButton the number of buttons we will have on the toolbar, its defined as:
   const int NumButton = 3;
Next we have to specify what text to show on the buttons and which bitmap to use:
   TBBUTTON ButtonInfo[NumButton];
   memset(ButtonInfo, 0, sizeof(ButtonInfo));
   int idx;
   idx = SendMessage(ToolBarWindow,
                     TB_ADDSTRING,
                     0,
                     (LPARAM )"Open");
   ButtonInfo[0].iString = idx;
   ButtonInfo[0].iBitmap = STD_FILEOPEN;
   ButtonInfo[0].idCommand = FileOpenCmd;
   ButtonInfo[0].fsState = TBSTATE_ENABLED;
   ButtonInfo[0].fsStyle = TBSTYLE_BUTTON;
This has to be repeated for each button.
Then we sends this information to the toolbar:
   SendMessage(ToolBarWindow,
               TB_ADDBUTTONS,
               NumButton,
               (LPARAM)&ButtonInfo);
And at the end we show the toolbar:
   ShowWindow(ToolBarWindow, SW_SHOW);
As for the statusbar, we must tell the toolbar to resize when the mainwindow is resized, so we add this line to the WM_SIZE handling:
         SendMessage(ToolBarWindow, msg, wParam, lParam);
The complete code for the application can be found here