MFC 单文档添加自定义位图工具栏图标

MFC 单文档添加自定义位图工具栏图标,第1张

MFC单文档程序,在资源编辑器中只有一个已有的toolbar资源,你只需要在资源编辑器里面,通过“空位”建立一个按钮图标(通过绘制或者复制)并加工好,然后选中这个
工具条
按钮,右键属性,给予它一个ID就可以使用了。
ID最好是先建立菜单项的方式给予,这样方便建立提示说明等文字。
工具条按钮,不需要SetButton方法,这个是对话框按钮控件的方式,而不是工具条按钮。

下列函数为添加工具栏的代码:
BOOL CTestDlg::CreatToolBar( void )
{
// 创建工具栏并绑定资源
if (!m_toolBarCreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_toolBarLoadToolBar(IDR_TOOLBAR))
{
TRACE0("Failed to Create Dialog Toolbar");
EndDialog(IDCANCEL);
return FALSE;
}
// 2 - 得出控件条大小
CRect rcClientStart;
CRect rcClientNow;
GetClientRect(rcClientStart);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,
0, reposQuery, rcClientNow);
// 3 放置控件条位置
CPoint ptOffset(rcClientNowleft - rcClientStartleft,
rcClientNowtop - rcClientStarttop);
CRect rcChild;
CWnd pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChildOffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
// 4 - 调整对话框尺寸
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindowright += rcClientStartWidth() - rcClientNowWidth();
rcWindowbottom += rcClientStartHeight() - rcClientNowHeight();
MoveWindow(rcWindow, FALSE);
//m_webRect = rcWindow;

// 5 - 控件条定位
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
//GetClientRect( &m_webRect );
m_webRectleft = rcWindowleft;
m_webRectbottom = rcWindowbottom;
m_webRectright = rcWindowright;
m_webRecttop = rcChildtop;

// 6 - 对框居中
CenterWindow();
return TRUE;
}
头文件中添加:
CToolBar m_toolBar;
在资源中自定义工具栏资源:IDR_TOOLBAR(该名字可自定义,主要与程序中的匹配)
在OnInitDialog()
中调用CreatToolBar()即能实现添加工具栏

下列的这段代码是我从网上找的,希望对你有帮助!
先找到自己想要的Ico图标,并将这些图标依次加到程序资源中,声明一个CToolBar m_wndToolBar工具栏变量和
CImageList img变量;
在程序中创建工具栏如下,其中各项参数可以参考MSDN:
if(m_wndToolBarCreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC )
||m_wndToolBarLoadToolBar(IDR_TOOLBAR1))
{
TRACE0(_T("创建工具条失败\n"));
}
m_wndToolBarGetToolBarCtrl()SetButtonWidth(43, 70);
在ImageList中加上自己想要的图标
ImgCreate(22, 22, ILC_COLOR8|ILC_MASK,2,2);
ImgSetBkColor(::GetSysColor(COLOR_BTNFACE));
imgAdd(AfxGetApp()->LoadIcon(IDI_ICON1));
imgAdd(AfxGetApp()->LoadIcon(IDI_ICON2));
imgAdd(AfxGetApp()->LoadIcon(IDI_ICON3));
imgAdd(AfxGetApp()->LoadIcon(IDI_ICON4));
m_wndToolBarGetToolBarCtrl()SetHotImageList(&img);
imgDetach();
得到按钮的大小,设置按钮的大小
CRect rectToolBar;
m_wndToolBarGetItemRect(0, &rectToolBar);
m_wndToolBarSetSizes(rectToolBarSize(), CSize(20,20));


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12717206.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-27
下一篇 2023-05-27

发表评论

登录后才能评论

评论列表(0条)

保存