vs2010单文档创建带有图标的自定义工具栏,工具栏没法显示按钮和图片

chenyancan 2015-04-23 10:31:29
在vs2010的MFC单文档中,创建带有图标的自定义工具栏,按钮和图片都没法显示。操作步骤如下:
1、头文件新增定义:
(1)
CMFCToolBarImages m_myImages; //图片列表
CMFCToolBar m_mywndToolBar;//自定义工具栏

(2)
afx_msg void OnToolBarButton(UINT nID);

2、源文件新增内容:
(1)
enum {
ID_LOGIN = 5000,
ID_LOGOUT,
ID_VIEW,
};

typedef struct SCustomizeButton
{
unsigned int id;
char name[64];
char tip[64];
}SCustomizeButton;

static SCustomizeButton CustomizeButton[]=
{
{ID_LOGIN, "login.bmp", "登陆"},
{ID_LOGOUT, "logout.bmp", "退出"},
{ID_VIEW, "view.bmp", "视图"}
};

(2)
ON_COMMAND_RANGE(ID_LOGIN, ID_VIEW, &CMainFrame::OnToolBarButton)

(3)将OnCreate修改如下:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
return -1;

if (!m_wndStatusBar.Create(this))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));

if (!m_mywndToolBar.CreateEx(this))
{
TRACE0("Failed to create tool bar\n");
return -1; // fail to create
}

m_mywndToolBar.SetSizes(CSize(31,30),CSize(24,24));

m_mywndToolBar.SetWindowText("mytoolbar");

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
DockPane(&m_mywndToolBar);

//设置图像大小
m_myImages.SetImageSize(CSize(24,24),FALSE);

int i = 0;
char tmp[32] = {0};
char strPath[MAX_PATH] = {0}, strFull[MAX_PATH] = {0};
int buttonNum = sizeof(CustomizeButton) / sizeof(CustomizeButton[0]);
HBITMAP hBitmap = NULL;

//获取文件路径并加载
GetModuleFileName(NULL, strPath, MAX_PATH);
(_tcsrchr(strPath,'\\'))[1] = 0;
for (i = 0; i < buttonNum; i++)
{
memset(strFull, 0, sizeof(strFull));
StrCpy(strFull, strPath);
StrCat(strFull, "\\");
StrCat(strFull, CustomizeButton[i].name);
hBitmap = (HBITMAP)LoadImage(AfxGetResourceHandle(), strFull, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR|LR_LOADFROMFILE);
m_myImages.AddImage(hBitmap);
}
//加载位图
m_mywndToolBar.SetUserImages(&m_myImages);

//创建工具条按钮及提示
for (i = 0; i < buttonNum; i++)
{
int index = 0;
//创建工具条按钮
m_mywndToolBar.InsertButton(CMFCToolBarButton(CustomizeButton[i].id, i, NULL, TRUE, 0));

//设置按钮提示
m_mywndToolBar.SetButtonText(i, CustomizeButton[i].tip);
}

return 0;
}

(4)
void CMainFrame::OnToolBarButton(UINT nID)
{
switch (nID)
{
case ID_LOGIN:
AfxMessageBox("login");
break;
case ID_LOGOUT:
AfxMessageBox("logout");
break;
case ID_VIEW:
AfxMessageBox("view");
break;
default:
AfxMessageBox("other");
break;
}
}

运行结果如下:


请各路大神帮忙指定啊。
...全文
373 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyancan 2015-04-30
  • 打赏
  • 举报
回复
引用 5 楼 cyc701 的回复:
我使用的也是用loadtoolbar替换的方式做的,等后续后新的方法了,大家再来一起学习啊。
恩,也就只能这样子啦
cyc701 2015-04-30
  • 打赏
  • 举报
回复
我使用的也是用loadtoolbar替换的方式做的,等后续后新的方法了,大家再来一起学习啊。
chenyancan 2015-04-30
  • 打赏
  • 举报
回复
哎呀,没人肯出来帮帮忙,就暂时用loadtoolbar的方式解决了。
chenyancan 2015-04-28
  • 打赏
  • 举报
回复
不可能没人遇到这样子的问题吧?
chenyancan 2015-04-23
  • 打赏
  • 举报
回复
引用 1 楼 sunnyloves 的回复:
看我的帖子http://blog.csdn.net/sunnyloves/article/details/42027999 估计是资源载入的问题 直接导入到工程里去吧
不想导入到工程里啊,这种做法太受限制了。
洗洗睡去 2015-04-23
  • 打赏
  • 举报
回复
看我的帖子http://blog.csdn.net/sunnyloves/article/details/42027999 估计是资源载入的问题 直接导入到工程里去吧

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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