最终效果如图所示:
1.定义CreateSimpleToolbar方法:
//新建工具栏
HWND CreateSimpleToolbar(HWND hwndParent){
HWND hwndToolbar;
//工具栏上按钮的数目
const int BUTTONNUMS = 2;
//按钮
TBBUTTON tbb[2];
ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[1].iBitmap = STD_REDOW;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
//位图,commctl中的标准位图
TBADDBITMAP tbBitmap1;
tbBitmap1.hInst = HINST_COMMCTRL;
tbBitmap1.nID = IDB_STD_SMALL_COLOR;
RECT windowRect;
GetWindowRect(hwndParent,&windowRect);
hwndToolbar = CreateWindowEx(0L,TOOLBARCLASSNAME,NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,0,0,0,0,
hwndParent,(HMENU)ID_TOOLBAR,hInst,NULL);
//将位图添加到工具栏
SendMessage(hwndToolbar,TB_ADDBITMAP,0,(LPARAM)&tbBitmap1);
//计算工具栏大小
SendMessage(hwndToolbar,TB_BUTTONSTRUCTSIZE,(WPARAM)sizeof(TBBUTTON),0);
//添加按钮到工具栏
SendMessage(hwndToolbar,TB_ADDBUTTONS,(WPARAM)BUTTONNUMS,(LPARAM)&tbb);
return hwndToolbar;
}
2.在WndProc窗口过程中,处理WM_CREATE,即窗口新建时即调用生成工具栏的方法:
switch (message)
{
case WM_CREATE:
CreateSimpleToolbar(hWnd);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
附上MSDN对于标准位图的说明:
| Toolbar Standard Button Image Index Values | ![]() ![]() |
This section specifies index values of images within standard bitmaps.
Index values for IDB_HIST_LARGE_COLOR and IDB_HIST_SMALL_COLOR:
| Constant | Description |
|---|---|
| Add to favorites. |
| Move back. |
| Open favorites folder. |
| Move forward. |
| View tree. |
Index values for IDB_STD_LARGE_COLOR and IDB_STD_SMALL_COLOR:
| Constant | Description |
|---|---|
| Copy operation. |
| Cut operation. |
| Delete operation. |
| New file operation. |
| Open file operation. |
| Save file operation. |
| Find operation. |
| Help operation. |
| Paste operation. |
| Print operation. |
| Print preview operation. |
| Properties operation. |
| Redo operation. |
| Replace operation. |
| Undo operation. |
Index values for IDB_VIEW_LARGE_COLOR and IDB_VIEW_SMALL_COLOR:
| Constant | Description |
|---|---|
| Details view. |
| Large icons view. |
| List view. |
| Connect to network drive. |
| Disconnect from network drive. |
| New folder. |
| Go to parent folder. |
| Small icon view. |
| Sort by date. |
| Sort by name. |
| Sort by size. |
| Sort by type. |
Remarks
You use these values to specify an image index within a standard image list that was loaded with theTB_LOADIMAGES message. The index values correspond to images within the standard bitmaps used by common controls.
The following tables list the constants by value so that they can be mapped to the icons in the bitmaps.
IDB_HIST_LARGE_COLOR and IDB_HIST_SMALL_COLOR:
Standard images
Standard images
| Define | Value |
|---|---|
| HIST_BACK | 0 |
| HIST_FORWARD | 1 |
| HIST_FAVORITES | 2 |
| HIST_ADDTOFAVORITES | 3 |
| HIST_VIEWTREE | 4 |
IDB_STD_LARGE_COLOR and IDB_STD_SMALL_COLOR:
Standard images
Standard images
| Define | Value |
|---|---|
| STD_CUT | 0 |
| STD_COPY | 1 |
| STD_PASTE | 2 |
| STD_UNDO | 3 |
| STD_REDOW | 4 |
| STD_DELETE | 5 |
| STD_FILENEW | 6 |
| STD_FILEOPEN | 7 |
| STD_FILESAVE | 8 |
| STD_PRINTPRE | 9 |
| STD_PROPERTIES | 10 |
| STD_HELP | 11 |
| STD_FIND | 12 |
| STD_REPLACE | 13 |
| STD_PRINT | 14 |
IDB_VIEW_LARGE_COLOR and IDB_VIEW_SMALL_COLOR:
Standard images
Standard images
| Define | Value |
|---|---|
| VIEW_LARGEICONS | 0 |
| VIEW_SMALLICONS | 1 |
| VIEW_LIST | 2 |
| VIEW_DETAILS | 3 |
| VIEW_SORTNAME | 4 |
| VIEW_SORTSIZE | 5 |
| VIEW_SORTDATE | 6 |
| VIEW_SORTTYPE | 7 |
| VIEW_PARENTFOLDER | 8 |
| VIEW_NETCONNECT | 9 |
| VIEW_NETDISCONNECT | 10 |
| VIEW_NEWFOLDER | 11 |
如果要使用自己的位图,则需要改写一下CreateSimpleToolbar方法:
//创建工具栏
HWND CreateSimpleToolbar(HWND parentHwnd)
{
const int ImageListID = 0;
const int ImageButtonNums = 2;
const int bitmapSize = 40;
const DWORD buttonStyle = BTNS_BUTTON;
toolbar = CreateWindowEx(0,TOOLBARCLASSNAME,NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER,0,0,0,0,
parentHwnd,NULL,hInst,NULL);
g_imagelist = ImageList_Create(bitmapSize,bitmapSize, ILC_COLOR24,ImageButtonNums,0);
//加载自己的位图
int iBitmap = ImageList_Add(g_imagelist,LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BITMAP1)),NULL);
SendMessage(toolbar,TB_SETIMAGELIST,(WPARAM)ImageListID,(LPARAM)g_imagelist);
TBBUTTON tbButtons[ImageButtonNums];
tbButtons[0].iBitmap = MAKELONG(iBitmap,ImageListID);
tbButtons[0].fsState = TBSTATE_ENABLED;
tbButtons[0].fsStyle = buttonStyle;
tbButtons[1].iBitmap = MAKELONG(iBitmap+1,ImageListID);
tbButtons[1].fsState = TBSTATE_ENABLED;
tbButtons[1].fsStyle = buttonStyle;
tbButtons[0].iString = 0;
tbButtons[1].iString = 0;
//添加按钮
SendMessage(toolbar,TB_BUTTONSTRUCTSIZE,(WPARAM)sizeof(TBBUTTON),0);
SendMessage(toolbar,TB_ADDBUTTONS,(WPARAM)ImageButtonNums,(LPARAM)&tbButtons);
//调整工具栏大小,并展示它
SendMessage(toolbar,TB_AUTOSIZE,0,0);
ShowWindow(toolbar,TRUE);
return toolbar;
}
同时,需要在WM_PAINT中调用:SendMessage(toolbar,TB_AUTOSIZE,0,0);使得窗口改变时工具栏大小依旧可以自适应。
使用自己定义的图片,效果如下图所示:
本文详细介绍了如何在窗口中创建自定义工具栏,包括使用标准位图和添加自定义图片,以及如何在不同消息处理函数中进行操作。通过实例展示了最终效果,并提供了关键代码段,帮助开发者实现个性化工具栏设计。


5753

被折叠的 条评论
为什么被折叠?



