Win32 SDK的Toolbar使用标准位图

来源:互联网 发布:linq实战源码 编辑:程序博客网 时间:2024/05/29 15:13

最终效果如图所示:


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 ValuesVisual Studio 2010

This section specifies index values of images within standard bitmaps.

Index values for IDB_HIST_LARGE_COLOR and IDB_HIST_SMALL_COLOR:

ConstantDescription
HIST_ADDTOFAVORITES

Add to favorites.

HIST_BACK

Move back.

HIST_FAVORITES

Open favorites folder.

HIST_FORWARD

Move forward.

HIST_VIEWTREE

View tree.

Index values for IDB_STD_LARGE_COLOR and IDB_STD_SMALL_COLOR:

ConstantDescription
STD_COPY

Copy operation.

STD_CUT

Cut operation.

STD_DELETE

Delete operation.

STD_FILENEW

New file operation.

STD_FILEOPEN

Open file operation.

STD_FILESAVE

Save file operation.

STD_FIND

Find operation.

STD_HELP

Help operation.

STD_PASTE

Paste operation.

STD_PRINT

Print operation.

STD_PRINTPRE

Print preview operation.

STD_PROPERTIES

Properties operation.

STD_REDOW

Redo operation.

STD_REPLACE

Replace operation.

STD_UNDO

Undo operation.

Index values for IDB_VIEW_LARGE_COLOR and IDB_VIEW_SMALL_COLOR:

ConstantDescription
VIEW_DETAILS

Details view.

VIEW_LARGEICONS

Large icons view.

VIEW_LIST

List view.

VIEW_NETCONNECT

Connect to network drive.

VIEW_NETDISCONNECT

Disconnect from network drive.

VIEW_NEWFOLDER

New folder.

VIEW_PARENTFOLDER

Go to parent folder.

VIEW_SMALLICONS

Small icon view.

VIEW_SORTDATE

Sort by date.

VIEW_SORTNAME

Sort by name.

VIEW_SORTSIZE

Sort by size.

VIEW_SORTTYPE

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

 

 

Standard images

Standard images

 

DefineValueHIST_BACK0HIST_FORWARD1HIST_FAVORITES2HIST_ADDTOFAVORITES3HIST_VIEWTREE4

 

IDB_STD_LARGE_COLOR and IDB_STD_SMALL_COLOR:

 

Standard images

Standard images

 

 

Standard images

Standard images

 

DefineValueSTD_CUT0STD_COPY1STD_PASTE2STD_UNDO3STD_REDOW4STD_DELETE5STD_FILENEW6STD_FILEOPEN7STD_FILESAVE8STD_PRINTPRE9STD_PROPERTIES10STD_HELP11STD_FIND12STD_REPLACE13STD_PRINT14

 

IDB_VIEW_LARGE_COLOR and IDB_VIEW_SMALL_COLOR:

 

Standard images

Standard images

 

 

Standard images

Standard images

 

DefineValueVIEW_LARGEICONS0VIEW_SMALLICONS1VIEW_LIST2VIEW_DETAILS3VIEW_SORTNAME4VIEW_SORTSIZE5VIEW_SORTDATE6VIEW_SORTTYPE7VIEW_PARENTFOLDER8VIEW_NETCONNECT9VIEW_NETDISCONNECT10VIEW_NEWFOLDER11

如果要使用自己的位图,则需要改写一下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);使得窗口改变时工具栏大小依旧可以自适应。

使用自己定义的图片,效果如下图所示:

0 0
原创粉丝点击