利用crebar和cdialogbar编写可浮动的dialog类型的工具栏.
这个crebar用起来参数真奇怪, 算了, 直接上代码吧:先弄一个基于cdialog的类CMyDlgBar,将基类改为cdialogbar, 需要注意必须修改 constructor(没有参数), OnInitDialog(里面需要初始化)
class CMyDlgBar : public CDialogBar
{
DECLARE_DYNAMIC(CMyDlgBar)
public:
CMyDlgBar(); // standard constructor
virtual ~CMyDlgBar();
// Dialog Data
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
virtual BOOL Create(CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle, UINT nID);
int m_nData;
afx_msg void OnBnClickedOk();
// afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton1();
afx_msg LRESULT OnInitDialog(UINT wParam,LONG lParam);
afx_msg void OnEnChangeEdit1();
};
{
DECLARE_DYNAMIC(CMyDlgBar)
public:
CMyDlgBar(); // standard constructor
virtual ~CMyDlgBar();
// Dialog Data
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
virtual BOOL Create(CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle, UINT nID);
int m_nData;
afx_msg void OnBnClickedOk();
// afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton1();
afx_msg LRESULT OnInitDialog(UINT wParam,LONG lParam);
afx_msg void OnEnChangeEdit1();
};
// CMyDlgBar dialog
IMPLEMENT_DYNAMIC(CMyDlgBar, CDialogBar)
CMyDlgBar::CMyDlgBar()
: CDialogBar()
, m_nData(0)
{
}
CMyDlgBar::~CMyDlgBar()
{
}
void CMyDlgBar::DoDataExchange(CDataExchange* pDX)
{
CDialogBar::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMyDlgBar, CDialogBar)
ON_BN_CLICKED(IDOK, &CMyDlgBar::OnBnClickedOk)
// ON_BN_CLICKED(IDC_BUTTON1, &CMyDlgBar::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON1, &CMyDlgBar::OnBnClickedButton1)
ON_EN_CHANGE(IDC_EDIT1, &CMyDlgBar::OnEnChangeEdit1)
ON_MESSAGE(WM_INITDIALOG,OnInitDialog)
END_MESSAGE_MAP()
LRESULT CMyDlgBar::OnInitDialog(UINT wParam,LONG lParam)
{
// TODO: Add extra initialization here
BOOL bRet = HandleInitDialog(wParam,lParam);
if (!UpdateData(FALSE))
{
TRACE("InitCDataStatus Failed!");
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
// CMyDlgBar message handlers
BOOL CMyDlgBar::Create(CWnd* pParentWnd, UINT nIDTemplate, UINT nStyle, UINT nID)
{
// TODO: Add your specialized code here and/or call the base class
CDialogBar::Create(pParentWnd, nIDTemplate, nStyle, nID);
UpdateData(FALSE);
return TRUE;
}
void CMyDlgBar::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
}
//void CMyDlgBar::OnBnClickedButton1()
//{
// // TODO: Add your control notification handler code here
//}
void CMyDlgBar::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
}
void CMyDlgBar::OnEnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialogBar::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
MessageBox(L"Changed");
// TODO: Add your control notification handler code here
}
在cmainframe里面定义两个变量
CMyDlgBar m_barAttrib;
CReBar m_wndReBar;
必须实现oncreate和oncmdmsg
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to be dockable
if (!m_barAttrib.Create(this,m_barAttrib.IDD, CBRS_ALIGN_RIGHT|CBRS_GRIPPER, m_barAttrib.IDD))
{
TRACE0("Failed to create dialogbar\n");
return -1;
}
m_barAttrib.SetWindowText(L"部件属性");
/*XXX是一个资源id手工直接在资源的.h文件中添加一条,不会,这里就不教了。
工具条的显示和隐藏代码如下,自己慢慢理解吧:*/
// ShowControlBar(&m_barAttrib, (m_barAttrib.GetStyle() & WS_VISIBLE) == 0, FALSE);
//m_wndReBar.Create(this);
m_wndReBar.Create(this, RBS_AUTOSIZE | RBS_BANDBORDERS,
/*WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | */ CBRS_RIGHT) ;
m_wndReBar.AddBar(&m_barAttrib,NULL,NULL, RBBS_GRIPPERALWAYS|RBBS_FIXEDBMP);
//m_wndReBar.AddBar(&m_wndToolBar,NULL,NULL, RBBS_GRIPPERALWAYS|RBBS_FIXEDBMP);
m_barAttrib.EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_barAttrib);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// TODO: Add your specialized code here and/or call the base class
if(m_barAttrib.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE;
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}