我想再对话框上加一个工具栏,
按msdn上说的作了一个toolbar resource,id=IDR_TOOLBAR1
在initdialog()中create,load toolbar:
if (!m_ToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_ToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
请问哪里不对了?为什么看不到工具栏?
http://www.vckbase.com/document/viewdoc.asp?id=265
后面加两句:
m_ToolBar.ShowWindow(SW_SHOW);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
MFC中没有提供供对话框使用的工具条类。
简单介绍
step1:
在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1
step2:
在对话框变量中添加一个工具条变量。
CToolBar m_wndToolBar;
step3:
在CDialog::OnInitDialog中添加如下代码:
//创建工具条并调入资源
if(!m_wndToolBar.Create(this)||!m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("FailedtoCreateDialogToolbar\n");
EndDialog(IDCANCEL);
}
CRect rcClientOld;//旧客户区RECT
CRect rcClientNew;//加入TOOLBAR后的CLIENTRECT
GetClientRect(rcClientOld);//Calledtorepositionandresizecontrolbars //intheclientareaofawindow
//ThereposQueryFLAGdoesnotreallytrawthe //Toolbar.Itonlydoesthecalculations.
//AndputsthenewClientRectvaluesin //rcClientNewsowecandotherestoftheMath.
//重新计算RECT大小
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNew);
//AlloftheChildWindows(Controls)nowneedtobemovedsotheTollbar //doesnotcoverthemup.
//所有的子窗口将被移动,以免被TOOLBAR覆盖
//OffesttomoveallchildcontrolsafteraddingTollbar
//计算移动的距离
CPointptOffset(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);//RedrawWindow
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);