Programming Windows - Creating a Toolbar


Home | API | MFC | C++ | C | Previous | Next

Toolbars can be created using the CreateWindowEx function, specifying TOOLBARCLASSNAME as the window class name, or by using the depreciated API CreateToolbarEx function. A TBBUTTON structure contains information about the individual buttons and this is added to the toolbar via the sendmessage() function using the TB_ADDBUTTONS or TB_INSERTBUTTON message parameters. A bitmap is used to hold the toolbar image. Each time a button is clicked a WM command message will be generated with the ID of each button being held in the low order word of the wparam.

In the following example, a simple 3-button toolbar is defined and created with two standard buttons divided by a separator. A separator provides a small gap between button groups and does not receive user input. The API function LoadImage loads the toolbar bitmap from the local c drive into a handle of type HBITMAP. The toolbar image bitmap is then created by a call to the function ImageList_Create() and the imagelist is then added to the toolbar control by the function ImageList_Add(). The individual image associated with the toolbar button is set in the TBBUTTON structure. Clicking either of these buttons will generate a message box indicating the button clicked

For in-depth reading on the creation of toolbars
https://docs.microsoft.com/en-us/windows/win32/controls/toolbar-control-reference

The following short program creates a simple toolbar with two buttons using the following two images. Clicking either button will produce a message box.


Home | API | MFC | C++ | C | C | Previous | Next

Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions

Last Updated: 15 September 2022