MFC 对话框添加ToolBar

本文详细介绍了在MFC中创建对话框工具条的过程,包括资源编辑器操作、初始化工具条、调整布局等步骤,以及工具条与普通按钮的相似性和使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MFC中没有提供供对话框使用的工具条类,而我们时常需要开发以对话框为框架的程序。下面简单的说明这种方法。

1:在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDR_TOOLBAR1

2:在对话框变量中添加一个工具条变量。

//.h  
CToolBar m_wndToolBar;
3:在CDialog::OnInitDialog中添加如下代码:

// 创建工具条并调入资源
if(!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to Create Dialog Toolbar\n");
EndDialog(IDCANCEL);
}

CRect rcClientOld; // 久客户区RECT
CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT
GetClientRect(rcClientOld); // 
// Called to reposition and resize control bars in the client area of a window
// The reposQuery FLAG does not really draw the Toolbar. It only does the calculations.
// And puts the new ClientRect values in rcClientNew so we can do the rest of the Math.
//重新计算RECT大小
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNew);

// All of the Child Windows (Controls) now need to be moved so the Tollbar does not cover them up.
//所有的子窗口将被移动,以免被TOOLBAR覆盖
// Offest to move all child controls after adding Tollbar
//计算移动的距离
CPoint ptOffset(rcClientNew.left-rcClientOld.left,
rcClientNew.top-rcClientOld.top);

CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD); //得到子窗口
while(pwndChild) // 处理所有子窗口
{//移动所有子窗口
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild); 
rcChild.OffsetRect(ptOffset); 
pwndChild->MoveWindow(rcChild,FALSE); 
pwndChild = pwndChild->GetNextWindow();
}

CRect rcWindow;
GetWindowRect(rcWindow); // 得到对话框RECT
rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height(); 
MoveWindow(rcWindow,FALSE); // Redraw Window

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
具栏创建好了以后自然就要用到了,那么又该做些什么呢?首先,我们要清楚一个TOOLBAR上的按钮和一个BUTTON是一样的,所以顾名思义,我们就模仿一个CButton来定义就好了

1:为每个TOOLBAR的子项添加ID;ID_BUTTON1

2:给它增加信息响应机制,在源文件中, ON_BN_CLICKED(ID_BUTTON1,OnButton1)

3:在头文件中定义 afx_msg void OnButton1();

4:OK;

转载于:https://my.oschina.net/chinesedragon/blog/86892

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值