Using ON_UPDATE_COMMAND_UI with menu items and controls

.

Warning: The class presented in this article uses the undocumented MFC
member function CCmdUI::DoUpdate


Example project with dialog using this technique



You will have used the ON_UPDATE_COMMAND_UI macro in MFC with your
various menu commands. This macros is used to implement the message handler for
the UPDATE_COMMAND_UI "message" sent by the MFC framework.
This message allows you to set the state (enabled/disabled, checked/unchecked
etc) of your menu and toolbar items by calling the various OnUpdateXXX functions
that you supply.


Quite often, it would be useful to use this same "auto-update"
facility with the controls of a dialog box. Unfortunately, MFC does not provide
this functionality for you. In this article, I present a dialog class, derived
from CDialog, that allows you to use the familiar ON_UPDATE_COMMAND_UI macros
with any control on a dialog box.


This approach has the advantage of centralising your dialog controls state
logic and is very useful for complicated dialogs where the state of many
controls is determined by the state of other controls on the dialog.


The class, CCmdUIDialog, implements the necessary code to call your
OnUpdateXXX functions. You simply derive your dialog class from CCmndUIDialog
instead of CDialog, and manually add the ON_UPDATE_COMMAND_UI macros to
your class.


Class declaration:

#if !defined(AFX_CMDUIDIALOG_H__7D35F4B8_7531_11D1_8FA7_000000000000__INCLUDED_)
#define AFX_CMDUIDIALOG_H__7D35F4B8_7531_11D1_8FA7_000000000000__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// CmdUIDialog.h : header file
// 

/////////////////////////////////////////////////////////////////////////////
// CCmdUIDialog dialog

class CCmdUIDialog : public CDialog
{
// Construction
public:
	CCmdUIDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL);
	CCmdUIDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL);
	CCmdUIDialog();
	BOOL ContinueModal();

// Implementation
protected:

	// Generated message map functions
	//{{AFX_MSG(CCmdUIDialog)
	// NOTE: the ClassWizard will add member functions here
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CMDUIDIALOG_H__7D35F4B8_7531_11D1_8FA7_000000000000__INCLUDED_)

Class Implementation:

// CmdUIDialog.cpp : implementation file
//

#include "stdafx.h"
#include "delme.h"
#include "CmdUIDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCmdUIDialog dialog


CCmdUIDialog::CCmdUIDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
	: CDialog(lpszTemplateName, pParentWnd)
{
}

CCmdUIDialog::CCmdUIDialog(UINT nIDTemplate, CWnd* pParentWnd)
	: CDialog(nIDTemplate, pParentWnd)
{
}

CCmdUIDialog::CCmdUIDialog()
{
}


BOOL CCmdUIDialog::ContinueModal()
{
	// Iterate all child windows and instruct to update themselves
	CWnd* pWndChild=GetWindow(GW_CHILD);
	int iIndex=0;
	while (NULL!=pWndChild)
	{

		CCmdUI state;
		state.m_nID=::GetWindowLong(*pWndChild, GWL_ID);
		state.m_nIndex=iIndex++;
		state.m_pOther=pWndChild;

		// ***CCmdUI::DoUpdate is undocumented MFC***
		state.DoUpdate(this, FALSE);

		pWndChild=pWndChild->GetWindow(GW_HWNDNEXT);
	}

	// Must call the base class
	return CDialog::ContinueModal();
}

BEGIN_MESSAGE_MAP(CCmdUIDialog, CDialog)
	//{{AFX_MSG_MAP(CCmdUIDialog)
		// NOTE: the ClassWizard will add message map macros here
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCmdUIDialog message handlers



To use the class and update handlers


First, derive your dialog class from CCmdUIDialog instead of CDialog. Next,
add update command handlers for each control you require this feature for, using
the following technique:


Enter the name of the update function in the protected section of your
dialog header file


Each update function has the signature void OnUpdateXXX(CCmdUI*
pCmdUI);
where XXX might describe the control for this handler. These
update functions should be places immediatley after the //}}AFX_MSG
comment in your header file. This will ensure that all of the command
handlers are grouped together.


Add a ON_UPDATE_COMMAND_UI macro in your dialog source (.cpp) file.


This macro takes the resource ID of the dialog control and the name if the
update function to call. For a dialog control called IDC_CHECK1 with a
handler function called OnUpdateCheck1 , the macro entry would read

	ON_UPDATE_COMMAND_UI(IDC_CHECK1, OnUpdateCheck1)

This handler should appear between the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP
macros and after the //}}AFX_MSG_MAP entry.


Implement the update function


Implement the logic you require in your update function, using the passed
CCmdUI pointer to update your dialog control. The passed CCmdUI pointer
is based on the dialog control that this habdler is called for.


The attached example application displays a dialog that used the technique
described here and also shows how to update more than one control using the same
handler function.

Update

Thanks go to Jeff Marino for pointing out this better
approach.

Jeff Marino has pointed out an MFC function that handles
ON_UPDATE_COMMAND_UI with dialogs and does not require the derived dialog
class to call any undocumented MFC stuff. The function is
CWnd::UpdateDialogControls.

This just goes to show how large MFC is and that there is a wealth of
functionality if only you can find it and how useful your resource is.

To use the UpdateControls function, overload ONE of the PreTranslateMessage
or ContinueModal functions like this:

BOOL ::PreTranslateMessage( MSG* pMsg )
{
UpdateDialogControls( this, TRUE );
return CDialog::PreTranslateMessage( pMsg );
}

or

BOOL ::ContinueModal( )
{
UpdateDialogControls( this, TRUE );
return CDialog::ContinueModal( );
}

One should bear in mind that ContinueModal is called about four to five
times as often as PreTranslateMessage.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read