如何在WinAPI编程中为工具栏使用自定义图标 [英] How to use custom icons for toolbars in winapi programming

查看:247
本文介绍了如何在WinAPI编程中为工具栏使用自定义图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自己的自定义图像在win32 winapi程序中创建工具栏.这就是我所拥有的(在我的WM_CREATE情况下):

I'm trying to use my own custom images for creating a toolbar in a win32 winapi program. This is what I have ( in my WM_CREATE case ):

#define IDT_MAIN_TOOL     101
TBBUTTON tbb[ 1 ];
TBADDBITMAP tbab;

HWND hToolbar = CreateWindowEx( 0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT, 0, 0, 0, 0, hwnd, ( HMENU )IDT_MAIN_TOL, NULL, NULL );

SendMessage( hToolbar, TB_BUTTONSTRUCTSIZE, ( WPARAM )sizeof( TBBUTTON ), 0 );

tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_HIST_LARGE_COLOR;
SendMessage( hToolbar, TB_ADDBITMAP, 0, ( LPARAM )&tbab );

ZeroMemory( tbb, sizeof( tbb ) );

tbb[ 0 ].iBitmap = HIST_BACK;
// I've also tried tbb[ 0 ].iBitmap = LoadIcon( NULL, "browse_back.ico" );
// However, iBitmap must be an integer and can't figure out how to use my 'browse_back.ico' image
tbb[ 0 ].fsState = TBSTATE_ENABLED;

SendMessage( hToolbar, TB_ADDBUTTONS, sizeof( tbb ) / sizeof( TBBUTTON ), ( LPARAM )&tbb );

我想使用我自己的图标图像:'browse_back.ico'作为该工具栏按钮的图像.这是如何完成的?不知道这是否必要,但我使用的是Microsoft Visual C ++ 2010 Express.

I would like to use my own icon image: 'browse_back.ico' as the image for that toolbar button. How is this accomplished? Not sure if this is necessary but I'm using Microsoft Visual C++ 2010 Express.

推荐答案

iBitmap 类型:int

iBitmap Type: int

按钮图像的基于零的索引.将此成员设置为I_IMAGECALLBACK,工具栏将在需要时发送TBN_GETDISPINFO通知代码以检索图像索引.

Zero-based index of the button image. Set this member to I_IMAGECALLBACK, and the toolbar will send the TBN_GETDISPINFO notification code to retrieve the image index when it is needed.

版本5.81.将此成员设置为I_IMAGENONE,以指示该按钮没有图像.按钮布局将不包含位图的任何空间,仅包含文本.

Version 5.81. Set this member to I_IMAGENONE to indicate that the button does not have an image. The button layout will not include any space for a bitmap, only text.

如果按钮是分隔符,即fsStyle设置为BTNS_SEP,则iBitmap确定分隔符的宽度(以像素为单位). 有关从图像列表中选择按钮图像的信息,请参见TB_SETIMAGELIST消息.

If the button is a separator, that is, if fsStyle is set to BTNS_SEP, iBitmap determines the width of the separator, in pixels. For information on selecting button images from image lists, see TB_SETIMAGELIST message.

因此,您需要使用 ImageList_Create() ,使用 ImageList_ReplaceIcon() ,使用

So you need to create an image list using ImageList_Create(), add your ICO image to it using ImageList_Add() or ImageList_ReplaceIcon(), associate it with the toolbar using TB_SETIMAGELIST, and then you can set tbb[0].iBitmap to the ICO's index within the image list.

这篇关于如何在WinAPI编程中为工具栏使用自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆