CToolBar m_wndToolBar step3: 在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 traw 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)引文来源
第一个工具栏:
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT,
WS_CHILD | WS_VISIBLE | CBRS_TOP |
CBRS_FLOAT_MULTI | CBRS_GRIPPER | CBRS_TOOLTIPS |
CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar")
return -1 // fail to create
}
m_wndToolBar.SetWindowText(_T("标准工具栏"))
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY)
EnableDocking(CBRS_ALIGN_ANY)
DockControlBar(&m_wndToolBar)
第二个工具栏:
if (!m_wndExtendBar.CreateEx(this, TBSTYLE_FLAT,
WS_CHILD | WS_VISIBLE | CBRS_TOP |
CBRS_FLOAT_MULTI | CBRS_GRIPPER | CBRS_TOOLTIPS |
CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndExtendBar.LoadToolBar(IDR_EXTEND_BAR))
{
TRACE0(_T("Failed to create extend toolbar"))
return -1
}
m_wndExtendBar.SetWindowText(_T("扩展工具栏"))
m_wndExtendBar.EnableDocking(CBRS_ALIGN_ANY)
CRect rect
m_wndExtendBar.GetWindowRect(&rect)
rect.OffsetRect(1, 0)
this->RecalcLayout() //关键的一步,重新排列
DockControlBar(&m_wndExtendBar, AFX_IDW_DOCKBAR_TOP, &rect)
注:第二个工具栏CreateEx必须写在第一个工具栏CreateEx之后,否则还是两行。
MFC并列显示多个工具栏(Toolbar)
评论列表(0条)