How to initialize child controls in a derived CDialogBar

原创 2006年06月05日 03:45:00

如何将CDialogBar的消息映射转到CMainFrame中去..原文地址:http://support.microsoft.com/default.aspx?scid=kb;en-us;185672

 

How to initialize child controls in a derived CDialogBar

SUMMARY
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.


MORE INFORMATION
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;
      }
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

How to initialize child controls in a derived CDialogBar

原帖地址:http://support.microsoft.com/kb/185672When creating a simple CDialogBar, such as one with only ...
  • guguarchieve
  • guguarchieve
  • 2010年05月18日 10:14
  • 371

错误“A child container failed during start”之处理(1)

Dynamic Web 项目转成Maven项目,部署时出现“ child container failed during start”异常的处理方式(1)....
  • williamanos
  • williamanos
  • 2017年05月15日 15:44
  • 591

A child container failed during start tomcat启动报错

严重: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catali...
  • zhoumengshun
  • zhoumengshun
  • 2017年06月28日 16:57
  • 2673

How to find child controls that are located in the template of a parent control

SUMMARYTo find child controls that are located in the template of a parent control, you can use the...
  • mdot
  • mdot
  • 2004年07月09日 21:12
  • 837

利用CDialogBar来实现类似工具栏的浮动条

     如果你想实现有工具条的浮动和定位功能,而且可以方便的摆放任何控件上去,请继续看这篇文章吧!那就使用CDialogBar就可以拥有和CDialog一样的方便和快捷。    步骤1:添加一个CD...
  • houen_study
  • houen_study
  • 2004年11月05日 10:29
  • 2675

致MFC新手,关于CDialogBar使用的一个小错误

最近,我根据CSDN上的一篇文章http://blog.csdn.net/franksunny/article/details/572357学习了如何在一个文档中停靠一个...
  • DOCAICSD
  • DOCAICSD
  • 2014年08月21日 11:08
  • 809

10分钟内完成 MFC对话条CDialogBar编程

关于CDialogBar的编程          今天编程过程中,涉及到了CDialogBar的编程,在网上看了一些资料,虽然看明白了,但是写的不是很透明,如果对于一个初学者来说,肯定会头疼,下面,...
  • kjxyzhaobo_01
  • kjxyzhaobo_01
  • 2014年07月06日 20:40
  • 708

将CDialog完美转换成CDialogBar

DialogBar相比Dialog有很多优势。因CDialogBar继承于CControlBar,因此其可以Docking在窗口一边,而把CView挤压开,但是 CDialogBar 的使用并不是很容...
  • zztoll
  • zztoll
  • 2012年05月03日 11:07
  • 2079

在打开 Office XP 或 Office 2003 文档时,会提示您为 ActiveX 控件授予权限

重要说明:本文包含有关如何修改注册表的信息。修改注册表之前,一定要先对其进行备份,并且一定要知道在出现问题时如何还原注册表。有关如何备份、还原和修改注册表的更多信息,请单击下面的文章编号,以查看 Mi...
  • snlei
  • snlei
  • 2008年08月06日 10:59
  • 3823

Unity3D 官方文档 UGUI总览 自动布局组件的介绍

版本:unity 5.6  语言:C#   总起: 自动布局组件可以根据元素的内容自动调节元素的布局,在和滚动列表配合显示动态元素时比较方便。   拥有Layout Element脚本的是子元素,放在...
  • u012632851
  • u012632851
  • 2017年08月09日 18:09
  • 768
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to initialize child controls in a derived CDialogBar
举报原因:
原因补充:

(最多只允许输入30个字)