但是,在一个更复杂的 CDialogBar,可能有一个下拉组合框、 树视图或 ActiveX 控件的情况下可能有用来提供子控件的初始化 CDialogBar 从派生。
因为类向导不支持从 CDialogBar 派生类,这篇文章将显示该步骤了为所需从 CDialog 创建一个类,然后将"转换"类为 CDialogBar。
回到顶端
更多信息若要开始时,创建与您要使用的子控件的 CDialog 类。您可以将 CDialog 类转换 CDialogBar 类使用以下的 9 个步骤: 在类声明中更改从...若要开始时,创建与您要使用的子控件的 CDialog 类。您可以将 CDialog 类转换 CDialogBar 类使用以下的 9 个步骤:
在类声明中更改从 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);
...
转换的关键是到 WM_INITDIALOG 消息映射方法虚拟 OnInitDialog() 成员函数的转换,通过更改 OnInitDialog 方法以及添加 ON_MESSAGE() 处理程序。您可能没有 OnInitDialog() 的重写。如果不是,添加一个在继续操作之前。
删除"虚拟 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() 转换为消息映射 OnInitDialog()。
如下所示进行 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;
的 CDialogBar 类不具有虚拟 OnInitDialog(),并因此调用某个不起作用。UpdateData 调用以子类或初始化任何子控件。
确保对话框框中的资源样式所示:
样式: 子
boarder: 无
可见: 未检查
重新此位置的所有内容已被连接到进行转换从 CDialog 类到 CDialogBar 类正常工作。现在,创建并使用它。
将派生 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;
}