CDialogBar的具体使用过程二
2011-04-13 22:25
369 查看
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B185672
例子工程代码: http://download.csdn.net/source/3188852
How to initialize child controls in a derived CDialogBar
View products that this article applies to.
This article was previously published under Q185672
Expand all | Collapse all
When creating a simple CDialogBar, such as one with only CButtons similar to MFC's print preview, it is not necessary to derive from CDialogBar because the parent of CControlBar receives the notification messages from any child controls.
However, in the case of a more complex CDialogBar, which might have a drop- down combo box, a treeview, or ActiveX control, it might be useful to derive from CDialogBar to provide initialization for the child controls.
Because ClassWizard does not support deriving a class from CDialogBar, this article shows the steps necessary to create a class from CDialog and then "convert" the class to CDialogBar.
To start out, create a CDialog class with the child controls you want to use. You can transform the CDialog class into a CDialogBar class using the following nine steps:
1. Change the base class from CDialog to CDialogBar in the class declaration. Don't forget to also change the base class in BEGIN_MESSAGE_MAP in the .cpp file.
2. Change the constructor in both the .h and the .cpp files. Also make the change to the DoDataExchange(). Below are three items to change.
Change the following from
CMyDlgBar (CWnd* pParent = NULL); // standard constructor
CMyDlgBar:: CMyDlgBar (CWnd* pParent /*=NULL*/)
: CDialog(CMyDlgBar::IDD, pParent)
{
...
void CMyDlgBar::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
...
to the following:
CMyDlgBar (); // standard constructor
CMyDlgBar:: CMyDlgBar ()
{
...
void CMyDlgBar::DoDataExchange(CDataExchange* pDX)
{
CDialogBar::DoDataExchange(pDX);
...
The key to the transformation is the conversion of the virtual OnInitDialog() member function to the WM_INITDIALOG message mapped method by changing the OnInitDialog method and by adding the ON_MESSAGE() handler. You may not have an override of OnInitDialog(). If not, add one before proceeding.
3. Remove "virtual BOOL OnInitDialog();" from the class header and add "afx_msg LONG OnInitDialog ( UINT, LONG );" in its place. For example:
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()
};
Now, in the class implementation section, make the corresponding changes.
4. Add "ON_MESSAGE(WM_INITDIALOG, OnInitDialog );" to the message map in the .CPP implementation file. For example:
BEGIN_MESSAGE_MAP(CMyDlgBar, CDialogBar)
//{{AFX_MSG_MAP(CMyDlgBar)
...
//}}AFX_MSG_MAP
ON_MESSAGE(WM_INITDIALOG, OnInitDialog ) // <-- Add this line.
END_MESSAGE_MAP()
Now, convert the virtual OnInitDialog() to the message-mapped OnInitDialog().
5. Make the OnInitDialog() conversion as follows:
Change the following:
BOOL CMyDlgBar::OnInitDialog()
{
CDialog::OnInitDialog(); // <-- Replace this line:
...
to the following:
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;
The CDialogBar class doesn't have a virtual OnInitDialog(), and therefore calling one does not work. UpdateData is called to subclass or initialize any child controls.
6. Make sure the dialog box resource styles to the following:
Style: Child
Boarder: None
Visible: Unchecked
At this point, everything has been reconnected to make the transformation from a CDialog class to a CDialogBar class work correctly. Now, create and use it.
7. Add an instance of the derived CDialogBar to the CframeWnd-derived class (normally called CMainFrame). For example:
class CMainFrame : public CFrameWnd
{
...
CMyDlgBar m_myDlgBar;
...
};
8. Call the create method for the m_myDlgBar variable in the CFrameWnd::OnCreate() method similar to the following:
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
}
...
}
9. Finally, if you want to support dynamic docking and resizing of the CDialogBar, add the following lines to the end of 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;
}
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
99161 How to derive from classes not listed in ClassWizard
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B185672
例子工程代码: http://download.csdn.net/source/3188852
How to initialize child controls in a derived CDialogBar
View products that this article applies to.
This article was previously published under Q185672
Expand all | Collapse all
When creating a simple CDialogBar, such as one with only CButtons similar to MFC's print preview, it is not necessary to derive from CDialogBar because the parent of CControlBar receives the notification messages from any child controls.
However, in the case of a more complex CDialogBar, which might have a drop- down combo box, a treeview, or ActiveX control, it might be useful to derive from CDialogBar to provide initialization for the child controls.
Because ClassWizard does not support deriving a class from CDialogBar, this article shows the steps necessary to create a class from CDialog and then "convert" the class to CDialogBar.
To start out, create a CDialog class with the child controls you want to use. You can transform the CDialog class into a CDialogBar class using the following nine steps:
1. Change the base class from CDialog to CDialogBar in the class declaration. Don't forget to also change the base class in BEGIN_MESSAGE_MAP in the .cpp file.
2. Change the constructor in both the .h and the .cpp files. Also make the change to the DoDataExchange(). Below are three items to change.
Change the following from
CMyDlgBar (CWnd* pParent = NULL); // standard constructor
CMyDlgBar:: CMyDlgBar (CWnd* pParent /*=NULL*/)
: CDialog(CMyDlgBar::IDD, pParent)
{
...
void CMyDlgBar::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
...
to the following:
CMyDlgBar (); // standard constructor
CMyDlgBar:: CMyDlgBar ()
{
...
void CMyDlgBar::DoDataExchange(CDataExchange* pDX)
{
CDialogBar::DoDataExchange(pDX);
...
The key to the transformation is the conversion of the virtual OnInitDialog() member function to the WM_INITDIALOG message mapped method by changing the OnInitDialog method and by adding the ON_MESSAGE() handler. You may not have an override of OnInitDialog(). If not, add one before proceeding.
3. Remove "virtual BOOL OnInitDialog();" from the class header and add "afx_msg LONG OnInitDialog ( UINT, LONG );" in its place. For example:
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()
};
Now, in the class implementation section, make the corresponding changes.
4. Add "ON_MESSAGE(WM_INITDIALOG, OnInitDialog );" to the message map in the .CPP implementation file. For example:
BEGIN_MESSAGE_MAP(CMyDlgBar, CDialogBar)
//{{AFX_MSG_MAP(CMyDlgBar)
...
//}}AFX_MSG_MAP
ON_MESSAGE(WM_INITDIALOG, OnInitDialog ) // <-- Add this line.
END_MESSAGE_MAP()
Now, convert the virtual OnInitDialog() to the message-mapped OnInitDialog().
5. Make the OnInitDialog() conversion as follows:
Change the following:
BOOL CMyDlgBar::OnInitDialog()
{
CDialog::OnInitDialog(); // <-- Replace this line:
...
to the following:
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;
The CDialogBar class doesn't have a virtual OnInitDialog(), and therefore calling one does not work. UpdateData is called to subclass or initialize any child controls.
6. Make sure the dialog box resource styles to the following:
Style: Child
Boarder: None
Visible: Unchecked
At this point, everything has been reconnected to make the transformation from a CDialog class to a CDialogBar class work correctly. Now, create and use it.
7. Add an instance of the derived CDialogBar to the CframeWnd-derived class (normally called CMainFrame). For example:
class CMainFrame : public CFrameWnd
{
...
CMyDlgBar m_myDlgBar;
...
};
8. Call the create method for the m_myDlgBar variable in the CFrameWnd::OnCreate() method similar to the following:
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
}
...
}
9. Finally, if you want to support dynamic docking and resizing of the CDialogBar, add the following lines to the end of 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;
}
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
99161 How to derive from classes not listed in ClassWizard
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B185672
相关文章推荐
- CDialogBar的具体使用过程一
- Filter过滤器的使用过程,结合具体示例
- 大型机学习之具体技术之-JCL练习(五)-过程的定义与使用
- 在kali下使用openVas扫描的具体过程
- 使用grunt进行批量处理的具体过程
- 使用序列化实现对象的拷贝的具体实现过程
- 【Git使用具体解释】Egit使用过程中遇到的问题及解决的方法
- SVN的具体使用过程2【资料+操作+截图】客户端
- SVN的具体使用过程1【资料+操作+截图】仅服务器端
- Oracle导出程序Exp的使用具体过程
- Oracle导出程序Exp的使用具体过程
- MySQL 使用自增ID主键和UUID 作为主键的优劣比較具体过程(从百万到千万表记录測试)
- Oracle导出程序Exp/imp的使用具体过程(转)
- 使用递归调用求数组的最大值,了解递归的栈调用以及递归函数的具体执行过程
- fb61使用axel加速port具体过程,效果太明显了
- Oracle导出程序Exp的使用具体过程
- 存储过程中使用RETURN语句返回数值,.Net里用ExecuteScalar方法结果将会导致一个未引用到具体对象的异常
- 通讯:使用浏览器访问一个网站的具体过程是什么样的?
- Oracle导出程序Exp的使用具体过程
- 公钥与密钥的生成与具体的机器没有关系,生成过程没有使用与具体机器相关的信息