CDialogBar应用

若要开始时,创建与您要使用的子控件的 CDialog 类。您可以将 CDialog 类转换 CDialogBar 类使用以下的 9 个步骤: 

  1. 在类声明中更改从 CDialog 为 CDialogBar 的基类。不要忘记还更改基类 BEGIN_MESSAGE_MAP 在.cpp 文件中。
  2. 更改该.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() 的重写。如果不是,添加一个在继续操作之前。
  3. 删除"虚拟 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()
          };
    						
    立即,在类的实现部分进行相应更改。
  4. 添加 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()。
  5. 如下所示进行 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 调用以子类或初始化任何子控件。
  6. 确保对话框框中的资源样式所示:
    样式: 子
    boarder: 无
    可见: 未检查
    重新此位置的所有内容已被连接到进行转换从 CDialog 类到 CDialogBar 类正常工作。现在,创建并使用它。
  7. 将派生 CDialogBar 的实例添加到 (通常称为 CMainFrame) CframeWnd 派生类中。例如:
          class CMainFrame : public CFrameWnd
          {
              ...
              CMyDlgBar m_myDlgBar;
              ...
          };
    					
  8. 调用在方法中 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
             }
             ...
          }
    					
  9. 最后,如果您想要支持动态插接和调整大小的该 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;
          }

//添加按钮无效
原来只需要重载CDialogBar::OnUpdateCmdUI这个方法就可以了,代码如下:

void CToolBarDlg::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHandler)
{
    bDisableIfNoHandler = FALSE;
    CDialogBar::OnUpdateCmdUI(pTarget, FALSE);
}
# 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.


出处: 微软官方
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值