我在MainFrm.h中定义了:
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CToolBar m_wndmy;/////我的定义
然后在CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中这样写到:
//..... if (!m_wndmy.Create(this, WS_CHILD | WS_VISIBLE | CBRS_RIGHT
| CBRS_SIZE_DYNAMIC,IDD_DIALOG1)) //||
//!m_wndmy.LoadToolBar(IDD_DIALOG1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
m_wndmy.EnableDocking(CBRS_ALIGN_LEFT|CBRS_ALIGN_RIGHT);
DockControlBar(&m_wndmy);
EnableDocking(CBRS_ALIGN_ANY);
//.....
并且创建了对话框资源!ID为:IDD_DIALOG1,并创建了对话框类!
但运行后的程序中的对话框工具栏小的看不见,而且不能拖大!
请各位指教!
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CToolBar m_wndmy;/////我的定义
然后在CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中这样写到:
//..... if (!m_wndmy.Create(this, WS_CHILD | WS_VISIBLE | CBRS_RIGHT
| CBRS_SIZE_DYNAMIC,IDD_DIALOG1)) //||
//!m_wndmy.LoadToolBar(IDD_DIALOG1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
m_wndmy.EnableDocking(CBRS_ALIGN_LEFT|CBRS_ALIGN_RIGHT);
DockControlBar(&m_wndmy);
EnableDocking(CBRS_ALIGN_ANY);
//.....
并且创建了对话框资源!ID为:IDD_DIALOG1,并创建了对话框类!
但运行后的程序中的对话框工具栏小的看不见,而且不能拖大!
请各位指教!
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndmy.Create(this) ||
!m_wndmy.LoadToolBar(IDD_DIALOG1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
//Make the toolbar dockable
m_wndmy.EnableDocking(CBRS_ALIGN_LEFT|CBRS_ALIGN_RIGHT);
DockControlBar(&m_wndmy);
EnableDocking(CBRS_ALIGN_ANY);
return 0;
}还有,我想问,你到底是创建TOOLBAR还是DIALOGBAR ?
homepage: http://vchelp.zb169.net
MFC中没有提供供对话框使用的工具条类,而我们时常需要开发以对话框为框架的程序。下面我使用简单的代码说明这种方法。
step1:
在资源编辑器中插入工具条资源,并为每个按钮创建ID。将它命名为IDC_TOOLBAR1 step2:
在对话框变量中添加一个工具条变量。
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 WindowRepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
不过我的目的是在基于单文档的应用程序中创建具有对话框风格的工具栏,比如可以加button,等控件到工具栏上!