Click here to Skip to main content
15,881,715 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  
A C++/MFC sample how to implement UI notifications for user-defined controls
#include "stdafx.h"

#include <afxpriv.h>		// This was manually added for MFC's command UI support

#include "CmdUIDemo.h"
#include "CmdUIDemoDlg.h"

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

//----------------------------------------------------------------------------//
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV-Unterst�tzung
	//}}AFX_VIRTUAL

protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

//----------------------------------------------------------------------------//
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

//----------------------------------------------------------------------------//
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

//----------------------------------------------------------------------------//
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// Keine Nachrichten-Handler
	//}}AFX_MSG_MAP
	
END_MESSAGE_MAP()

//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//

CCmdUIDemoDlg::CCmdUIDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCmdUIDemoDlg::IDD, pParent),
	m_bToggle(FALSE),
	m_nTimer(0)
{
	//{{AFX_DATA_INIT(CCmdUIDemoDlg)
		// HINWEIS: Der Klassenassistent f�gt hier Member-Initialisierung ein
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

//----------------------------------------------------------------------------//
void CCmdUIDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCmdUIDemoDlg)
		// HINWEIS: Der Klassenassistent f�gt an dieser Stelle DDX- und DDV-Aufrufe ein
	//}}AFX_DATA_MAP
}

//----------------------------------------------------------------------------//
BEGIN_MESSAGE_MAP(CCmdUIDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CCmdUIDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE_VOID(WM_KICKIDLE, OnKickIdle)
	ON_UPDATE_COMMAND_UI(IDC_BUTTON1, OnUpdateUserButton)
END_MESSAGE_MAP()

//----------------------------------------------------------------------------//
BOOL CCmdUIDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{	
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);			
	SetIcon(m_hIcon, FALSE);		
	
	// Our toggle timer
	m_nTimer = SetTimer(1, 1000, NULL);

	return TRUE;
}

//----------------------------------------------------------------------------//
void CCmdUIDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}


//----------------------------------------------------------------------------//
void CCmdUIDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); 

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//----------------------------------------------------------------------------//
HCURSOR CCmdUIDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//----------------------------------------------------------------------------//
void CCmdUIDemoDlg::OnKickIdle()
{
	UpdateDialogControls(this, FALSE);
}

//----------------------------------------------------------------------------//
void CCmdUIDemoDlg::OnUpdateUserButton(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_bToggle);
}

//----------------------------------------------------------------------------//
void CCmdUIDemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	KillTimer(m_nTimer);	
}

//----------------------------------------------------------------------------//
void CCmdUIDemoDlg::OnTimer(UINT nIDEvent) 
{
	m_bToggle = !m_bToggle;	
	CDialog::OnTimer(nIDEvent);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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