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

Programming Windows API

Toolbar Control

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 a bitmap is used to hold the toolbar images. Buttons are added to the toolbar via the sendmessage() function using the TB_ADDBUTTONS or TB_INSERTBUTTON message parameters. Each time a button is clicked a WM command message will be generated with the id of each button be 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 being 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 function ImageList_Create and this imagelist is then added into 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 toolbar bitmap was created from the following image

toolbar image

// toolbar control demo
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib") //adds link to control control DLL
HINSTANCE ghInstance;
#define TB_TEST1 1003
#define TB_TEST2 1004
static HWND hWndToolBar;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("myWindowClass");
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
InitCommonControls();
CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("myWindowClass"),TEXT("Simple Toolbar Window"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
TBBUTTON tbb[3];
HBITMAP toolbarBitMap;
//local toolbar bitmap tbbitmap.bmp from root of c drive
toolbarBitMap = (HBITMAP)LoadImage(NULL, TEXT("tbbitmap.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
//create toolbar
hWndToolBar = CreateWindowEx(0,TOOLBARCLASSNAME, NULL,TBSTYLE_TRANSPARENT | WS_CHILD| WS_VISIBLE,0,0,0,0,hwnd,NULL , (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
HIMAGELIST imageList;
// Creates image list
imageList = ImageList_Create(30, 30, ILC_COLOR8 | ILC_MASK, 4, 0);
//add bitmap to image list
ImageList_Add(imageList, toolbarBitMap, NULL);
//add imagelist to toolbar
SendMessage(hWndToolBar, TB_SETIMAGELIST, 0, (LPARAM)imageList);
ZeroMemory(tbb, sizeof(tbb));
//TBBUTON variable defines characteristics of each toolbar button.
tbb[0].iBitmap = 0;
tbb[0].idCommand = TB_TEST1;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_SEP;
tbb[2].iBitmap = 1;
tbb[2].idCommand = TB_TEST2;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
//message TB_ADDBUTTONS add three buttons as defined in TBBUTTON structure tbb
SendMessage(hWndToolBar, TB_ADDBUTTONS, 3, (LPARAM)&tbb);
//message TB_AUTOSIZE causes a toolbar to be resized so that icons and buttons are aligined in toolbar
SendMessage(hWndToolBar, TB_AUTOSIZE , 0, 0);
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
//responds to button 1 click
case TB_TEST1:
{
MessageBox(NULL, TEXT("Toolbar Button One"),TEXT("Success"), MB_OK | MB_ICONINFORMATION);
}
break;
//responds to button 2 click
case TB_TEST2:
{
MessageBox(NULL, TEXT("Toolbar Button Two"), TEXT("Success"), MB_OK | MB_ICONINFORMATION);
}
break;
}
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

Download


Home | API | MFC | 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 | ScrollBar Control | 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