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 Values Visual Studio 2010

This section specifies index values of images within standard bitmaps.

Index values for IDB_HIST_LARGE_COLOR and IDB_HIST_SMALL_COLOR:

Constant Description
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:

Constant Description
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:

Constant Description
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

 

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);使得窗口改变时工具栏大小依旧可以自适应。

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

你可能感兴趣的:(Win32 SDK的Toolbar使用标准位图)