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 CDialog
CFormViewCFormViewhere. But first, let's take a look at the implementation details.
-
Include the MFC header file declaring private Windows messages and macros in the source file (.cpp) of your dialog class implementation.
#include <afxpriv.h>
-
In the corresponding header file where your dialog window class is defined, declare the prototypes of the WM_KICKIDLE
WM_KICKIDLE
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
[...]
afx_msg void OnKickIdle();
afx_msg void OnUpdateUserButton(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
-
Update your message map in .cpp file to map the WM_KICKIDLE
BEGIN_MESSAGE_MAP(CCmdUIDemoDlg, CDialog)
ON_WM_PAINT()
[...]
ON_MESSAGE_VOID(WM_KICKIDLE, OnKickIdle)
ON_UPDATE_COMMAND_UI(IDC_BUTTON1, OnUpdateUserButton)
END_MESSAGE_MAP()
-
At last, implement the bodies of both message handlers:
void CCmdUIDemoDlg::OnKickIdle()
{
UpdateDialogControls(this, FALSE);
}
void CCmdUIDemoDlg::OnUpdateUserButton(CCmdUI *pCmdUI)
{
pCmdUI->Enable(m_bToggle);
}
- Done! By the
pCmdUI
CCmdUIEnable()
To use the update notification mechanism for a CFormView
CDialog
instance, perform the following steps by analogy:
-
Include the MFC header file declaring private Windows messages and macros in the source file (.cpp) of your dialog class implementation.
#include <afxpriv.h>
-
In the corresponding header file where your view class is defined, declare the prototypes of the WM_IDLEUPDATECMDUI
WM_IDLEUPDATECMDUI
[...]
afx_msg void OnIdleUpdateCmdUI();
afx_msg void OnUpdateUserControl(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
-
Update your message map in .cpp file to map the WM_IDLEUPDATECMDUI
BEGIN_MESSAGE_MAP(CMyFormView, CFormView)
[...]
ON_MESSAGE_VOID(WM_IDLEUPDATECMDUI, OnIdleUpdateCmdUI)
ON_UPDATE_COMMAND_UI(IDC_MY_CONTROL, OnUpdateUserControl)
END_MESSAGE_MAP()
-
At last, implement the bodies of both message handlers:
void CMyFormView::OnIdleUpdateCmdUI()
{
UpdateDialogControls(this, FALSE);
}
void CMyFormView::OnUpdateUserControl(CCmdUI *pCmdUI)
{
pCmdUI->Enable(...);
}
- 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.