- 在类声明中更改从 CDialog 为 CDialogBar 的基类。不要忘记还更改基类 BEGIN_MESSAGE_MAP 在.cpp 文件中。
- 更改该.h 和.cpp 文件中的构造函数。此外在 DoDataExchange() 进行的更改。以下是要更改的三个项目。
更改以下CMyDlgBar (CWnd* pParent = NULL); // standard constructor CMyDlgBar:: CMyDlgBar (CWnd* pParent /*=NULL*/) : CDialog(CMyDlgBar::IDD, pParent) { ... void CMyDlgBar::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); ...
CMyDlgBar (); // standard constructor CMyDlgBar:: CMyDlgBar () { ... void CMyDlgBar::DoDataExchange(CDataExchange* pDX) { CDialogBar::DoDataExchange(pDX); ...
- 删除"虚拟 OnInitDialog() BOOL ; 将从类标头,并添加"afx_msg LONG OnInitDialog (UINT,LONG) ;"在其位置。例如:
class CMyDlgBar : public CDialogBar { ... // Implementation protected: // Generated message map functions //{{AFX_MSG(CMyDlgBar) virtual BOOL OnInitDialog(); // <-Remove this line. //}}AFX_MSG afx_msg LONG OnInitDialog ( UINT, LONG ); // <-Add this line. DECLARE_MESSAGE_MAP() };
- 添加 ON_MESSAGE WM_INITDIALOG OnInitDialog) ; 到消息映射在.cpp 实现文件中。例如:
BEGIN_MESSAGE_MAP(CMyDlgBar, CDialogBar) //{{AFX_MSG_MAP(CMyDlgBar) ... //}}AFX_MSG_MAP ON_MESSAGE(WM_INITDIALOG, OnInitDialog ) // <-- Add this line. END_MESSAGE_MAP()
- 如下所示进行 OnInitDialog() 转换:
Change the following: BOOL CMyDlgBar::OnInitDialog() { CDialog::OnInitDialog(); // <-- Replace this line: ...
LONG CMyDlgBar::OnInitDialog ( UINT wParam, LONG lParam) { // <-- with these lines. --> BOOL bRet = HandleInitDialog(wParam, lParam); if (!UpdateData(FALSE)) { TRACE0("Warning: UpdateData failed during dialog init.\n"); } ... return bRet;
- 确保对话框框中的资源样式所示:
样式: 子重新此位置的所有内容已被连接到进行转换从 CDialog 类到 CDialogBar 类正常工作。现在,创建并使用它。
boarder: 无
可见: 未检查 - 将派生 CDialogBar 的实例添加到 (通常称为 CMainFrame) CframeWnd 派生类中。例如:
class CMainFrame : public CFrameWnd { ... CMyDlgBar m_myDlgBar; ... };
- 调用在方法中 CFrameWnd::OnCreate() m_myDlgBar 变量的创建方法类似于以下内容:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { ... if (!m_myDlgBar.Create(this, IDD_DLGBAR1, CBRS_LEFT, IDD_DLGBAR1)) { TRACE0("Failed to create dialog bar\n"); return -1; // fail to create } ... }
- 最后,如果您想要支持动态插接和调整大小的该 CDialogBar,添加到 CMainFrame::OnCreate() 末尾的以下行:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { ... m_myDlgBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); m_myDlgBar.EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_myDlgBar); return 0; }





# re: CDialogBar中按钮为灰色不可用的解决办法 2008-06-03 16:10 Sunny
刚放进去的按钮是灰色的,但是当你为他添加了响应函数后,他就可以用了,不必重载OnUpdateCmdUI吧 回复 更多评论
# re: CDialogBar中按钮为灰色不可用的解决办法 2008-06-03 16:16 Sunny
哦,我知道了,如果按钮的消息处理是在CMainFrame中的就不用OnUpdateCmdUI,在其他类的话还是要重载OnUpdateCmdUI的。3Q呵呵 回复 更多评论
# re: CDialogBar中按钮为灰色不可用的解决办法[未登录] 2008-06-03 19:49 杨粼波
具体我没有去深究个缘由,等有时间会去看看究竟内部的。
俺就晓得OnUpdateCmdUI是处理的WM_UI_UPDATE()这个消息的。
等我搞清楚了,就在后面把原理加上去。 回复 更多评论
# re: CDialogBar中按钮为灰色不可用的解决办法 2009-02-17 23:36 hxy
另外请教一个问题,在VC中的CDialogBar中加入一个VB控件,该控件含有OnClick()事件,当点击该控件时,在CMainFrame中如何实现OnClick()事件响应。如果是VC自己的一个按钮,双击它,然后就可以添加事件代码,框架便会响应该事件。 回复 更多评论
# re: CDialogBar中按钮为灰色不可用的解决办法 2009-04-21 10:10 Sunshine Alike
刚刚遇上了这个问题,下面是找到的答案
A button in a CDialogBar object is disabled automatically if the command routing does not contain a command handler function for the button.
To enable a button in a CDialogBar object, the command routing must include a command handler for the button.
NOTE: Because the CDialogBar object is not included in the routing scheme, the ON_BN_CLICKED() or ON_COMMAND() handler must be placed in one of the objects mentioned above that is in the routing scheme and not in the CDialogBar class.