Click here to Skip to main content
15,881,455 members
Articles / Desktop Programming / MFC

Applying an Update UI Notification Interface to User-defined Controls

Rate me:
Please Sign up or sign in to vote.
4.50/5 (10 votes)
30 Jul 20003 min read 131.9K   2.5K   56   10
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 notification. Unfortunately, MFC does not support this mechanism by default for user-defined controls, e.g., like a push button. Well, there's still a way to manage it. Follow the step by step procedure below to understand the principles of what to do. You may want to study the complete context in the source code of the sample.

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 CDialog and once for a view derived from CFormView. The sample code demonstrates it by using a dialog window. However, there's not much difference between the two ways. Preparing a CFormView instance is described here. 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.

    C++
    #include <afxpriv.h>
  2. In the corresponding header file where your dialog window class is defined, declare the prototypes of the WM_KICKIDLE message handler and the update UI handler as well. The WM_KICKIDLE message is always sent by the system if your dialog instance is idle. This is a good time to do some framework job.

    C++
    //{{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 message and the update notification message for the desired resource (the push button in our sample).

    C++
    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:

    C++
    void CCmdUIDemoDlg::OnKickIdle()
    {
    	UpdateDialogControls(this, FALSE);
    }
    
    void CCmdUIDemoDlg::OnUpdateUserButton(CCmdUI *pCmdUI)
    {
    	pCmdUI->Enable(m_bToggle);
    }
  5. Done! By the pCmdUI pointer, you've now have access to the methods of the CCmdUI instance associated with the object, e.g., the value of the Enable() method's parameter (BOOL) decides if the control is enabled or disabled as known from a menuitem or tool bar button.

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

To use the update notification mechanism for a CFormView instance instead of a 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.

    C++
    #include <afxpriv.h>
  2. In the corresponding header file where your view class is defined, declare the prototypes of the WM_IDLEUPDATECMDUI message handler and the update UI handler as well. The WM_IDLEUPDATECMDUI message is always sent by the system if your view instance is idle.

    C++
    //{{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 message and the update notification message for the desired resource.

    C++
    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:

    C++
    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.


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Maximilien26-Apr-13 4:29
Maximilien26-Apr-13 4:29 
QuestionThanks! Pin
Maximilien26-Apr-13 4:28
Maximilien26-Apr-13 4:28 
GeneralWorks fine in CDialogBar classes Pin
jon_a25-May-04 9:23
jon_a25-May-04 9:23 
Generalit doesn't work in modaless Dialog! Pin
yuzhou15-May-02 0:05
yuzhou15-May-02 0:05 
General...or, even better... Pin
Sardaukar5-Aug-00 0:17
Sardaukar5-Aug-00 0:17 
GeneralRe: ...or, even better... Pin
31-Jan-02 17:46
suss31-Jan-02 17:46 
GeneralRe: ...or, even better... Pin
Alexandre B19-Feb-04 4:05
professionalAlexandre B19-Feb-04 4:05 
GeneralRe: ...or, even better... Pin
mretondo13-Apr-04 6:57
mretondo13-Apr-04 6:57 
GeneralOk, but MFC is required... Pin
Sardaukar5-Aug-00 0:14
Sardaukar5-Aug-00 0:14 
GeneralRe: Ok, but MFC is required... Pin
James R. Twine7-Aug-00 6:56
James R. Twine7-Aug-00 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.