Win32 SDK的Toolbar使用标准位图

最终效果如图所示:


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

 

DefineValue
HIST_BACK0
HIST_FORWARD1
HIST_FAVORITES2
HIST_ADDTOFAVORITES3
HIST_VIEWTREE4

 

IDB_STD_LARGE_COLOR and IDB_STD_SMALL_COLOR:

 

Standard images

Standard images

 

 

Standard images

Standard images

 

DefineValue
STD_CUT0
STD_COPY1
STD_PASTE2
STD_UNDO3
STD_REDOW4
STD_DELETE5
STD_FILENEW6
STD_FILEOPEN7
STD_FILESAVE8
STD_PRINTPRE9
STD_PROPERTIES10
STD_HELP11
STD_FIND12
STD_REPLACE13
STD_PRINT14

 

IDB_VIEW_LARGE_COLOR and IDB_VIEW_SMALL_COLOR:

 

Standard images

Standard images

 

 

Standard images

Standard images

 

DefineValue
VIEW_LARGEICONS0
VIEW_SMALLICONS1
VIEW_LIST2
VIEW_DETAILS3
VIEW_SORTNAME4
VIEW_SORTSIZE5
VIEW_SORTDATE6
VIEW_SORTTYPE7
VIEW_PARENTFOLDER8
VIEW_NETCONNECT9
VIEW_NETDISCONNECT10
VIEW_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);使得窗口改变时工具栏大小依旧可以自适应。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值