求一个给工具栏贴上自己的图标的demo

combobox2013 2013-02-26 05:13:06
求一个给工具栏贴上自己的图标的demo. 条件,用以下函数CreateSimpleToolbar实现.\


说明:这个函数是msdn上的, 给工具栏上添加3个图标,这些图标均是windows预定义的。

代码是对的, 毫无问题,但是我希望在这个基础上贴自己的图标,非系统的, 搞了很久了,一直不成功


亲们,自己可以验证代码,没有验证过的别乱回帖,谢谢。

注意出现有汉字的那行代码,我在那里做了个小小的改动.


HIMAGELIST g_hImageList = NULL;

HWND CreateSimpleToolbar(HWND hWndParent)
{
// Declare and initialize local constants.
const int ImageListID = 0;
const int numButtons = 3;
const int bitmapSize = 16;

const DWORD buttonStyles = BTNS_AUTOSIZE;

// Create the toolbar.
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
hWndParent, NULL, g_hInst, NULL);

if (hWndToolbar == NULL)
return NULL;

// Create the image list.
g_hImageList = ImageList_Create(bitmapSize, bitmapSize, // Dimensions of individual bitmaps.
ILC_COLOR16 | ILC_MASK, // Ensures transparent background.
numButtons, 0);

// Set the image list.
SendMessage(hWndToolbar, TB_SETIMAGELIST,
(WPARAM)ImageListID,
(LPARAM)g_hImageList);

// Load the button images.
SendMessage(hWndToolbar, TB_LOADIMAGES,
(WPARAM)IDB_STD_SMALL_COLOR,
(LPARAM)HINST_COMMCTRL);

// Initialize button info.
// IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants.

TBBUTTON tbButtons[numButtons] =
{
{ MAKELONG(STD_FILENEW, ImageListID), IDM_NEW, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
// 我想修改按钮“New"这里的图标,所以代码为: { IDI_ICON1, ImageListID), IDM_NEW, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
{ MAKELONG(STD_FILEOPEN, ImageListID), IDM_OPEN, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Open"},
{ MAKELONG(STD_FILESAVE, ImageListID), IDM_SAVE, 0, buttonStyles, {0}, 0, (INT_PTR)L"Save"}
};

// Add buttons.
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons);

// Resize the toolbar, and then show it.
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar, TRUE);

return hWndToolbar;
}


修改图标不成功。 不知道什么原因!

后来又发现一个问题,用mfc 创建工具栏,贴上自己的图标的时候,也会出现有的图标也看不到

于是我猜测:图标大小及位深度和代码无法一一对应导致的。


所以,希望成功贴上自己图标的人,提供你的代码和你的icon,多谢。
我邮箱:

combobox2013@163.com
...全文
306 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ticomi 2014-04-14
  • 打赏
  • 举报
回复
学习了,很感谢,解决了一个很大的问题!!
schlafenhamster 2013-03-01
  • 打赏
  • 举报
回复
“操作起来繁琐的很” 还可以吧。 主要问题在于 只能 ADD 不能 insert add 加在后面,又不能 移动。
combobox2013 2013-03-01
  • 打赏
  • 举报
回复
引用 14 楼 schlafenhamster 的回复:
tb.hInst = YourInstance; tb.nID = IDB_YOUR_TOOLBAR; 就可以把你 图标 加上去。
说白了tb_addbitmap的消息的相关使用 操作起来繁琐的很。 多谢
schlafenhamster 2013-02-28
  • 打赏
  • 举报
回复
TBADDBITMAP 结构可以 指定 instance 。 使用 AddBitmap 函数。 可以 装载 不同 instance 里 的 bmp。 在 帮助文件 里 搜索 “CreateTheToolBar”。看看 它 是 怎么 装载 IDB_STD_SMALL_COLOR 和 IDB_VIEW_SMALL_COLOR 的。 这段代码 都用 HINST_COMMCTRL, 但 TBADDBITMAP 一个是 IDB_STD_SMALL_COLOR 另一个 是 IDB_VIEW_SMALL_COLOR。 要 装载 app 的 TBADDBITMAP 结构要 输入 hInst 和 IDB_APPTB // 你的 exe 定义的 bmp 。 你的 IDI_ICON1 是 IDB_APPTB 中的 索引 如果 IDB_APPTB 图标就一个 那就是 0.
schlafenhamster 2013-02-28
  • 打赏
  • 举报
回复
tb.hInst = YourInstance; tb.nID = IDB_YOUR_TOOLBAR; 就可以把你 图标 加上去。
schlafenhamster 2013-02-28
  • 打赏
  • 举报
回复
我把 代码 给你:

// Toolbar buttons
TBBUTTON tbButtons [] = 
{
{STD_FILENEW, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{STD_FILEOPEN, IDM_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{STD_FILESAVE, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
{0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0L, 0},
{VIEW_LARGEICONS, IDM_LARGEICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 
0L, 0},
{VIEW_SMALLICONS, IDM_SMALLICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, 
0L, 0},
{VIEW_LIST, IDM_LISTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 
0L, 0},
{VIEW_DETAILS, IDM_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 
0L, 0},
}; 
//
HWND CreateTheToolbar (HWND hWndParent)
{
HWND hWndToolbar;
TBADDBITMAP tb;
int index, stdidx;

hWndToolbar = CreateToolbarEx (hWndParent, 
WS_CHILD | WS_BORDER | WS_VISIBLE | WS_CHILD | TBSTYLE_TOOLTIPS, 
ID_TOOLBAR, 11, (HINSTANCE)HINST_COMMCTRL, IDB_STD_SMALL_COLOR, 
(LPCTBBUTTON)&tbButtons, 4, 0, 0, 100, 30, sizeof (TBBUTTON));

// Add the system-defined view bitmaps.
// The hInst == HINST_COMMCTRL
// The nID == IDB_VIEW_SMALL_COLOR
tb.hInst = HINST_COMMCTRL;
tb.nID = IDB_VIEW_SMALL_COLOR;
stdidx = SendMessage (hWndToolbar, TB_ADDBITMAP, 12, (LPARAM)&tb);

// Update the indexes to the bitmaps.
for (index = 4; index < NUM_BUTTONS; index++)
tbButtons[index].iBitmap += stdidx;

// Add the view buttons.
SendMessage (hWndToolbar, TB_ADDBUTTONS, 4, (LONG) &tbButtons[4]);

return hWndToolbar;
} 
bsnry 2013-02-27
  • 打赏
  • 举报
回复
引用 7 楼 stjay 的回复:
看里面的注释 C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061HIMAGELIST g_hImageList = NULL; HWND CreateSimpleToolba……
++ 图标没有关联到imagelist中导致的。 在TB_SETIMAGELSIT之前必须创建icon,否则就是系统自带的了. TBUTTON的第0个字段的意思是index in imagelist iBitmap Type: int 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. 当没有添加自定义的图标的时候, 这个值就是系统的一些常数 http://msdn.microsoft.com/en-us/library/windows/desktop/bb787433(v=vs.85).aspx
combobox2013 2013-02-27
  • 打赏
  • 举报
回复
引用 10 楼 schlafenhamster 的回复:
#define HINST_COMMCTRL ((HINSTANCE)-1) #define IDB_STD_SMALL_COLOR 0 #define IDB_STD_LARGE_COLOR 1 #define IDB_VIEW_SMALL_COLOR 4 #define IDB_VIEW_LARGE_COLOR 5……
是的, 没有装上 本想结贴,但是你的回答有些不对, 所以只好纠正 lParam Instance handle. This parameter must be set to HINST_COMMCTRL 说明这个消息的lparam只能是这个参数,其他不行的。 而wparma则是标记了系统已经定义好的button image list. 也就说,有不同的imagelist,选一个而已, 然后在tbbuttons这个结构体里设计好索引即可到这些imagelist里寻找图标 不对,请跟正, 如果没有异议, 该结贴散分了
schlafenhamster 2013-02-27
  • 打赏
  • 举报
回复
#define HINST_COMMCTRL ((HINSTANCE)-1) #define IDB_STD_SMALL_COLOR 0 #define IDB_STD_LARGE_COLOR 1 #define IDB_VIEW_SMALL_COLOR 4 #define IDB_VIEW_LARGE_COLOR 5 上面 是 资源的 instance 每个 都是不同的, 表示 资源在 不同的 instance 里 HINST_COMMCTRL 在 从comctrl32.dll 中。 而: // Load the button images. SendMessage(hWndToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL); 只有一个,那个在 你的exe中的(就是 你 exe 的 instance 里) { IDI_ICON1, IDM_NEW, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" }, 却没装入!
Kaile 2013-02-26
  • 打赏
  • 举报
回复
工具栏的定制还是bcg牛B,名字就叫bcgcontrolbar
stjay 2013-02-26
  • 打赏
  • 举报
回复
看里面的注释
HIMAGELIST g_hImageList = NULL;

HWND CreateSimpleToolbar(HWND hWndParent)
{
    // Declare and initialize local constants.
    const int ImageListID    = 0;
    const int numButtons     = 3;
    const int bitmapSize     = 16; // 注意这个大小
    
    const DWORD buttonStyles = BTNS_AUTOSIZE;

    // Create the toolbar.
    HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, 
                                      WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0, 
                                      hWndParent, NULL, hInst, NULL);
        
    if (hWndToolbar == NULL)
        return NULL;

    // Create the image list.
    g_hImageList = ImageList_Create(bitmapSize, bitmapSize,   // Dimensions of individual bitmaps.
                                    ILC_COLOR32 | ILC_MASK,   // Ensures transparent background.
                                    numButtons, 0); // 注意这个ILC_COLOR32参数,现在的图一般是32位的

	int i1 = ImageList_AddIcon(g_hImageList, LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1))); // ImageList_ReplaceIcon
	// 下面自己加
	// 可以加ImageList_LoadBitmap位图
	// ImageList_LoadImage

    // Set the image list.
    SendMessage(hWndToolbar, TB_SETIMAGELIST, 
                (WPARAM)ImageListID, 
                (LPARAM)g_hImageList);

    // 这个不要了,加载的是Toolbar自带的图标
    // Load the button images.
    //SendMessage(hWndToolbar, TB_LOADIMAGES, 
    //            (WPARAM)IDB_STD_SMALL_COLOR, 
    //            (LPARAM)HINST_COMMCTRL);

    // Initialize button info.
    // IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants.
    
    TBBUTTON tbButtons[numButtons] = 
    {
        { MAKELONG(STD_FILENEW,  ImageListID), 0,  TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
// 我想修改按钮“New"这里的图标,所以代码为:  { IDI_ICON1,  ImageListID), IDM_NEW,  TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
        { MAKELONG(i1, ImageListID), 0, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Open"},
        { MAKELONG(i1, ImageListID), 0, 0,               buttonStyles, {0}, 0, (INT_PTR)L"Save"}
    };

    // Add buttons.
    SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
    SendMessage(hWndToolbar, TB_ADDBUTTONS,       (WPARAM)numButtons,       (LPARAM)&tbButtons);

    // Resize the toolbar, and then show it.
    SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0); 
    ShowWindow(hWndToolbar,  TRUE);
    
    return hWndToolbar;
}
sdchenchen2012 2013-02-26
  • 打赏
  • 举报
回复
引用 5 楼 schlafenhamster 的回复:
“'BTNS_AUTOSIZE' : undeclared identifier”
放心这个我乱写的,有这个风格的 BTNS_AUTOSIZE include commctrl.h里,顺便加上其相应的库: comctl32.lib 还有请在合适的的地方加上 initcommoncontrols始化公共控件库
schlafenhamster 2013-02-26
  • 打赏
  • 举报
回复
“'BTNS_AUTOSIZE' : undeclared identifier”
combobox2013 2013-02-26
  • 打赏
  • 举报
回复
引用 3 楼 chuachua66 的回复:
http://www.codeproject.com/Articles/3948/WTL-for-MFC-Programmers-Part-III-Toolbars-and-Stat 这个是不是你要得?
多谢你提供,真不是 很巧的是文章里的那个函数 和我说的一样,呵呵 这个函数是自定义的,你却用其当搜索关键字眼 这个函数是msdn提供的一个例子里的自定义的
combobox2013 2013-02-26
  • 打赏
  • 举报
回复
引用 1 楼 chuachua66 的回复:
我木有现成的,但是推荐你到codeproject上搜搜,应该不少。
用以下函数CreateSimpleToolbar实现
chuachua66 2013-02-26
  • 打赏
  • 举报
回复
我木有现成的,但是推荐你到codeproject上搜搜,应该不少。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧