Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Applying an Update UI Notification Interface to User-defined Controls

0.00/5 (No votes)
30 Jul 2000 2  
A C++/MFC sample how to implement UI notifications for user-defined controls

Sample Image

Introduction

I often run into a situation where I want to apply a UI update notification to a user-defined control in the way as it is provided by the MFC for menu items, tool bar buttons and status bar controls. In this case, a self-defined message handler is used to properly respond to an UPDATE_COMMAND_UI

Please note: There are two ways you have to prepare your source code for properly applying update UI notifications to user-defined controls. It depends on the type of window class your controls are located in; once for a dialog class derived from CDialogCFormViewCFormViewhere. But first, let's take a look at the implementation details.

Implementation Details to Perform with a CDialog Instance

  1. Include the MFC header file declaring private Windows messages and macros in the source file (.cpp) of your dialog class implementation.

    #include <afxpriv.h>
  2. In the corresponding header file where your dialog window class is defined, declare the prototypes of the WM_KICKIDLEWM_KICKIDLE

    //{{AFX_MSG(CCmdUIDemoDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnPaint();
    [...]
    //}}AFX_MSG
    afx_msg void OnKickIdle();
    afx_msg void OnUpdateUserButton(CCmdUI* pCmdUI);
    DECLARE_MESSAGE_MAP()
  3. Update your message map in .cpp file to map the WM_KICKIDLE

    BEGIN_MESSAGE_MAP(CCmdUIDemoDlg, CDialog)
    	//{{AFX_MSG_MAP(CCmdUIDemoDlg)
    	ON_WM_PAINT()
    	[...]
    	//}}AFX_MSG_MAP
    	ON_MESSAGE_VOID(WM_KICKIDLE, OnKickIdle)
    	ON_UPDATE_COMMAND_UI(IDC_BUTTON1, OnUpdateUserButton)
    END_MESSAGE_MAP()
  4. At last, implement the bodies of both message handlers:

    void CCmdUIDemoDlg::OnKickIdle()
    {
    	UpdateDialogControls(this, FALSE);
    }
    
    void CCmdUIDemoDlg::OnUpdateUserButton(CCmdUI *pCmdUI)
    {
    	pCmdUI->Enable(m_bToggle);
    }
  5. Done! By the pCmdUICCmdUIEnable()

Implementation Details to Perform With a CFormView Instance (Not Part of the Demo Code)

To use the update notification mechanism for a CFormView CDialog instance, perform the following steps by analogy:

  1. Include the MFC header file declaring private Windows messages and macros in the source file (.cpp) of your dialog class implementation.

    #include <afxpriv.h>
  2. In the corresponding header file where your view class is defined, declare the prototypes of the WM_IDLEUPDATECMDUIWM_IDLEUPDATECMDUI

    //{{AFX_MSG(CMyFormView)
    [...]
    //}}AFX_MSG
    afx_msg void OnIdleUpdateCmdUI();
    afx_msg void OnUpdateUserControl(CCmdUI* pCmdUI);
    DECLARE_MESSAGE_MAP()
  3. Update your message map in .cpp file to map the WM_IDLEUPDATECMDUI

    BEGIN_MESSAGE_MAP(CMyFormView, CFormView)
    	//{{AFX_MSG_MAP(CMyFormView)
    	[...]
    	//}}AFX_MSG_MAP
    	ON_MESSAGE_VOID(WM_IDLEUPDATECMDUI, OnIdleUpdateCmdUI)
    	ON_UPDATE_COMMAND_UI(IDC_MY_CONTROL, OnUpdateUserControl)
    END_MESSAGE_MAP()
  4. At last, implement the bodies of both message handlers:

    void CMyFormView::OnIdleUpdateCmdUI()
    {
    	UpdateDialogControls(this, FALSE);
    }
    
    void CMyFormView::OnUpdateUserControl(CCmdUI *pCmdUI)
    {
    	pCmdUI->Enable(...);
    }
  5. Done!

History

  • 31st July, 2000: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here