Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / MFC

Radial Context Menu

Rate me:
Please Sign up or sign in to vote.
4.85/5 (26 votes)
20 Jul 20053 min read 84.9K   2.6K   52  
Mouse gesture with a graphical user interface.
// Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "ContextGestureDemo.h"
#include "Dlg.h"
#include "RadialContextMenu.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlg dialog

CDlg::CDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlg)
	m_strEditText = _T("Try to press the right mouse button inside this edit control.\r\nYou might then wait a few seconds to get a hint.");
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlg)
	DDX_Control(pDX, IDC_ContextEdit, m_wndEdit);
	DDX_Text(pDX, IDC_ContextEdit, m_strEditText);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDlg, CDialog)
	//{{AFX_MSG_MAP(CDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_RBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlg message handlers

BOOL CDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	VERIFY(m_ctrlToolbar.Create(this));
	VERIFY(m_ctrlToolbar.LoadToolBar(IDR_DEFAULT));

	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

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

		// Center icon in client rectangle
		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;

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


HCURSOR CDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CRadialContextMenu menu, submenuConfig, submenuFile;

	submenuConfig.SetBkColor(RGB(204, 204, 255))
		.SetTipColors(RGB(204, 204, 255), RGB(153, 153, 255))
		.SetIconSize(CSize(32, 32))
		.SetMenuItem(2001, AfxGetApp()->LoadIcon(IDI_ConfigKeyboard), CRadialContextMenu::ICON_TOP, _T("Configure Keyboard"))
		.SetMenuItem(2002, AfxGetApp()->LoadIcon(IDI_ConfigPrinter), CRadialContextMenu::ICON_RIGHT, _T("Configure Printer"))
		.SetMenuItem(2003, AfxGetApp()->LoadIcon(IDI_ConfigSearch), CRadialContextMenu::ICON_BOTTOM, _T("Configure Search"))
		.SetMenuItem(2004, AfxGetApp()->LoadIcon(IDI_ConfigToolbar), CRadialContextMenu::ICON_LEFT, _T("Configure Toolbar"));

	submenuFile.SetBkColor(RGB(204, 204, 255))
		.SetTipColors(RGB(204, 204, 255), RGB(153, 153, 255))
		.SetIconSize(CSize(32, 32))
		.SetMenuItem(3001, AfxGetApp()->LoadIcon(IDI_Open), CRadialContextMenu::ICON_TOP, _T("Open"))
		.SetMenuItem(3002, AfxGetApp()->LoadIcon(IDI_Print), CRadialContextMenu::ICON_RIGHT, _T("Print"))
		.SetMenuItem(3003, AfxGetApp()->LoadIcon(IDI_Save), CRadialContextMenu::ICON_BOTTOM, _T("Save"));

	menu.SetBkColor(RGB(204, 204, 255))
		.SetTipColors(RGB(204, 204, 255), RGB(153, 153, 255))
		.SetIconSize(CSize(32, 32))
		.SetMenuItem(1001, AfxGetApp()->LoadIcon(IDI_Info), CRadialContextMenu::ICON_TOP, _T("Info"))
		.SetMenuItem(&submenuConfig, AfxGetApp()->LoadIcon(IDI_Config), CRadialContextMenu::ICON_TOPRIGHT, _T("Configure"))
		.SetMenuItem(1002, AfxGetApp()->LoadIcon(IDI_SortAsc), CRadialContextMenu::ICON_RIGHT, _T("Sort Ascending"))
		.SetMenuItem(&submenuFile, AfxGetApp()->LoadIcon(IDI_File), CRadialContextMenu::ICON_BOTTOMRIGHT, _T("File Operations"))
		.SetMenuItem(1003, AfxGetApp()->LoadIcon(IDI_Search), CRadialContextMenu::ICON_BOTTOM, _T("Search"))
		.SetMenuItem(1004, AfxGetApp()->LoadIcon(IDI_SortDesc), CRadialContextMenu::ICON_LEFT, _T("Sort Descending"));

	if(menu.TrackRadialMenu(this))
		TRACE0("User made a selection\n");
	else
		TRACE0("Menu closed without a selection\n");
}

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.

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
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