1 . 新建一个对话框 ,style选择child
2. 用Class Wizard创建一个以CDialog为基类的新的类
3. 注释掉cpp文件中构造函数中的//: CDialog(CMYDIalogBar::IDD, pParent),并将其中出现的CDialog全部更换为CDialogBar
3. 自定义消息ON_MESSAGE(WM_INITDIALOG, OnInitDialog ),(WM_INITDIALOG可以使用系统的消息ID),h文件中:
afx_msg LONG OnInitDialog(UINT,LONG);
cpp文件中:
LONG CMYDIalogBar::OnInitDialog(UINT wParam, LONG lParam)
{
BOOL bRet = HandleInitDialog(wParam, lParam);
if (!UpdateData(FALSE))
{
TRACE0("Warning: UpdateData failed during dialog init./n");
}
return bRet;
}
4.自定义成员函数
virtual BOOL Create(CWnd* pParent, UINT nIDTemplate, UINT nStyle, UINT nID);
BOOL CMYDIalogBar::Create(CWnd* pParent, UINT nIDTemplate, UINT nStyle, UINT nID)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bReturn = CDialogBar::Create(pParent, nIDTemplate, nStyle, nID);
if(bReturn)
{
//m_bmButton.AutoLoad(IDC_BUTTON_BITMAP, this);
//这里可以完成一些初始化的工作,如果在动态创建此CDialogBar的时候。
}
return bReturn;
}
5. 在CMainFrm.h,cpp中的使用:
在h文件中定义一个实体的对象dlgBar,在cpp文件中用Create()函数创建:
if (!dlgBar.Create(this, IDD_MY_DIALOGBAR,
CBRS_TOP| CBRS_GRIPPER |CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_HIDE_INPLACE,
IDD_VIEW_DIALOGBAR))
{//IDD_VIEW_DIALOGBAR为自己定义的消息ID
TRACE0("Failed to create dialog bar m_wndDlgBar/n");
return -1; // fail to create
}
dlgBar.EnableDocking( CBRS_ALIGN_RIGHT|CBRS_FLOAT_MULTI|CBRS_ALIGN_TOP);
EnableDocking(CBRS_ALIGN_ANY);
CRect rect;//工具栏的窗口区域
DWORD dw;//工具栏的风格
UINT n;//工具栏的风格
// get MFC to adjust the dimensions of all docked ToolBars
// so that GetWindowRect will be accurate
RecalcLayout(TRUE);
m_wndToolBar.GetWindowRect(&rect);//获取工具栏窗口区域
rect.OffsetRect(1,0);
dw=m_wndToolBar.GetBarStyle();//取工具栏的风格
n = 0;
n = (dw&CBRS_ALIGN_TOP)?AFX_IDW_DOCKBAR_TOP : n;//顶端
n = (dw&CBRS_ALIGN_BOTTOM && n==0) ? AFX_IDW_DOCKBAR_BOTTOM : n;//底端
n = (dw&CBRS_ALIGN_LEFT && n==0) ? AFX_IDW_DOCKBAR_LEFT : n;//左边
n = (dw&CBRS_ALIGN_RIGHT && n==0)?AFX_IDW_DOCKBAR_RIGHT : n;//右边
// When we take the default parameters on rect, DockControlBar will dock
// each Toolbar on a separate line.By calculating a rectangle, we
// are simulating a Toolbar being dragged to that location and docked.
DockControlBar(&m_wndToolBar,n,&rect);
rect.OffsetRect(200,0);
DockControlBar(&dlgBar,n,&rect);
在消息映射中手动添加:
ON_COMMAND_EX(IDD_VIEW_DIALOGBAR, OnBarCheck)
ON_UPDATE_COMMAND_UI(IDD_VIEW_DIALOGBAR, OnUpdateControlBarMenu)
6. 对于CDialogBar上面的其他控件的响应,在View中完成