如果你的DialogBar要与某个类关联的话,则必须从CDialogBar派生。从CDialog类派生是不行的。 对DialogBar创建类的方法是这样的: 先从CDialog类派生,再将所有的CDialog改成CDialogBar 其中CDialogBar类中没有的函数需要更改,主要是OnInitDialog不能调用 CDialogBar::OnInitDialog(...),因为该函数不存在。 改成这样:在头文件中定义为: afx_msg LONG OnInitDialog ( UINT, LONG );.cpp文件中 LONG CYourDlgBar::OnInitDialog(UINT wParam, LONG lParam) { if ( !HandleInitDialog(wParam, lParam) || !UpdateData(FALSE)) { TRACE0("Warning: UpdateData failed during dialog init.\n"); return FALSE; }
// TODO: Add extra initialization here return TRUE; }
那个OnInitDialog 去掉不行吗?
给你个例子 消息处理各过程如下所示: 1、添加一个DialogBar资源!IDD_DIALOGBAR 2、在Dialogbar上放一个按钮(IDC_TEST_BUTTON),和一个editbox(IDC_TEST_EDIT) 3、在CMainFrame中添加变量CDialogBar m_TestDialogBar; 改写OnCreate()函数! int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { ... //Add the following codes to build a dialogbar in mainframe if (!m_TestDialogBar.Create(this,IDD_DIALOGBAR, CBRS_SIZE_DYNAMIC| CBRS_FLYBY|CBRS_GRIPPER| WS_CHILD|WS_VISIBLE|CBRS_BOTTOM,IDD_DIALOGBAR)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create }... } 4、在按钮(IDC_TEST_BUTTON)上单击右键 ,选择Add classwizard(建立类向导),在随后弹出的Add a class 对话框中选择Select an exsiting class,接着选择CMainFrame,对于vc的警告信息置之不理,最终就在CMainFrame 里响应消息BN_CLICKED void CMainFrame::OnTestButton() { // TODO: Add your control notification handler code here MessageBox("Hi,I am psusong(人类失去指针,世界将会怎样?) from csdn!"); //access the control in the dialogbar! CEdit *pEdit=(CEdit*)this->m_TestDialogBar.GetDlgItem(IDC_TEST_EDIT); pEdit->SetWindowText("My mail is [email protected]"); }-------------------------- Are you OK?
如果你添加了一个cdialogbar类 class CTemplateDialogBar : public CDialogBar { // Construction DECLARE_DYNCREATE(CTemplateDialogBar) public: CTemplateDialogBar(CWnd* pParent = NULL); // standard constructor// Dialog Data //{{AFX_DATA(CTemplateDialogBar) CScrollBar m_ScrollBarCtrl; //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTemplateDialogBar) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: // Generated message map functions //{{AFX_MSG(CTemplateDialogBar) afx_msg void OnTestButton(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; --------------------- void CTemplateDialogBar::OnTestButton() { // TODO: Add your control notification handler code here MessageBox("I am from CTemplateDialogBar"); } ------------------ in CMainFrame public: CTemplateDialogBar m_TestDialogBar; ----------------- 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 } if (!m_TestDialogBar.Create(this,IDD_DIALOGBAR, CBRS_SIZE_DYNAMIC| CBRS_FLYBY|CBRS_GRIPPER| WS_CHILD|WS_VISIBLE|CBRS_BOTTOM,IDD_DIALOGBAR)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } this->m_bAutoMenuEnable=true;
// TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); CMainFrame *pFrame=(CMainFrame*)::AfxGetMainWnd(); CDialogBar &pDialogBar=pFrame->m_TestDialogBar; CWnd *pWnd=pDialogBar.GetDlgItem(IDC_STATIC_TEST); m_CircleStatic.SubclassWindow(pWnd->m_hWnd); return 0; }然后为button 添加一个什么事也不做的WM_COMMAND onTestbutton() { } 就ok
classwizard有时候在你已经正确关联到自己创建的类以后也会出现这个问题,这时候我一般是从workspace窗口中新建消息映射。如果谁有更好的方法,欢迎指教。
对DialogBar创建类的方法是这样的:
先从CDialog类派生,再将所有的CDialog改成CDialogBar
其中CDialogBar类中没有的函数需要更改,主要是OnInitDialog不能调用
CDialogBar::OnInitDialog(...),因为该函数不存在。
改成这样:在头文件中定义为:
afx_msg LONG OnInitDialog ( UINT, LONG );.cpp文件中
LONG CYourDlgBar::OnInitDialog(UINT wParam, LONG lParam)
{
if ( !HandleInitDialog(wParam, lParam) || !UpdateData(FALSE))
{
TRACE0("Warning: UpdateData failed during dialog init.\n");
return FALSE;
}
// TODO: Add extra initialization here
return TRUE;
}
消息处理各过程如下所示:
1、添加一个DialogBar资源!IDD_DIALOGBAR
2、在Dialogbar上放一个按钮(IDC_TEST_BUTTON),和一个editbox(IDC_TEST_EDIT)
3、在CMainFrame中添加变量CDialogBar m_TestDialogBar;
改写OnCreate()函数!
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
...
//Add the following codes to build a dialogbar in mainframe
if (!m_TestDialogBar.Create(this,IDD_DIALOGBAR,
CBRS_SIZE_DYNAMIC|
CBRS_FLYBY|CBRS_GRIPPER|
WS_CHILD|WS_VISIBLE|CBRS_BOTTOM,IDD_DIALOGBAR))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}...
}
4、在按钮(IDC_TEST_BUTTON)上单击右键 ,选择Add classwizard(建立类向导),在随后弹出的Add a class 对话框中选择Select an exsiting class,接着选择CMainFrame,对于vc的警告信息置之不理,最终就在CMainFrame 里响应消息BN_CLICKED
void CMainFrame::OnTestButton()
{
// TODO: Add your control notification handler code here
MessageBox("Hi,I am psusong(人类失去指针,世界将会怎样?) from csdn!");
//access the control in the dialogbar!
CEdit *pEdit=(CEdit*)this->m_TestDialogBar.GetDlgItem(IDC_TEST_EDIT);
pEdit->SetWindowText("My mail is [email protected]");
}--------------------------
Are you OK?
class CTemplateDialogBar : public CDialogBar
{
// Construction
DECLARE_DYNCREATE(CTemplateDialogBar)
public:
CTemplateDialogBar(CWnd* pParent = NULL); // standard constructor// Dialog Data
//{{AFX_DATA(CTemplateDialogBar)
CScrollBar m_ScrollBarCtrl;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTemplateDialogBar)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected: // Generated message map functions
//{{AFX_MSG(CTemplateDialogBar)
afx_msg void OnTestButton();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
---------------------
void CTemplateDialogBar::OnTestButton()
{
// TODO: Add your control notification handler code here
MessageBox("I am from CTemplateDialogBar");
}
------------------
in CMainFrame
public:
CTemplateDialogBar m_TestDialogBar;
-----------------
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
} if (!m_TestDialogBar.Create(this,IDD_DIALOGBAR,
CBRS_SIZE_DYNAMIC|
CBRS_FLYBY|CBRS_GRIPPER|
WS_CHILD|WS_VISIBLE|CBRS_BOTTOM,IDD_DIALOGBAR))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
} this->m_bAutoMenuEnable=true;
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar); CMainFrame *pFrame=(CMainFrame*)::AfxGetMainWnd();
CDialogBar &pDialogBar=pFrame->m_TestDialogBar;
CWnd *pWnd=pDialogBar.GetDlgItem(IDC_STATIC_TEST);
m_CircleStatic.SubclassWindow(pWnd->m_hWnd); return 0;
}然后为button 添加一个什么事也不做的WM_COMMAND
onTestbutton()
{
}
就ok
DialogBar为基类的类
应该没问题
怎么最近老有人问cdialogbar消息的问题?
给我你的信箱,我给你发个demo!
[email protected]