Click here to Skip to main content
15,891,864 members
Articles / Desktop Programming / MFC

A Revolutionary New Approach to Custom Drawn Menus

Rate me:
Please Sign up or sign in to vote.
4.95/5 (119 votes)
26 May 200311 min read 588.2K   13.2K   234  
A new and unique approach to the perennial problem of how to change the default appearance of Windows menus
// SkinMenuTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SkinMenuTest.h"
#include "SkinMenuTestDlg.h"

#ifndef NO_SKIN_INI
#include "..\skinwindows\skininiglobalsfile.h"
#endif

#include "..\skinwindows\skinmenumgr.h"
#include "..\skinwindows\skinbase.h"

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

enum { COLOR_DEF, COLOR_COFFEE, COLOR_BLUE };

/////////////////////////////////////////////////////////////////////////////
// CSkinMenuTestDlg dialog

CSkinMenuTestDlg::CSkinMenuTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSkinMenuTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSkinMenuTestDlg)
	m_sSkinPath = _T("");
	m_nColorScheme = COLOR_BLUE;
	m_bGradient = TRUE;
	m_bSidebar = TRUE;
	m_sEdit = _T("Edit Control");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSkinMenuTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSkinMenuTestDlg)
	DDX_Control(pDX, IDC_EXPLORER1, m_browser);
	DDX_Text(pDX, IDC_SKINPATH, m_sSkinPath);
	DDX_CBIndex(pDX, IDC_COLORSCHEME, m_nColorScheme);
	DDX_Check(pDX, IDC_GRADIENT, m_bGradient);
	DDX_Check(pDX, IDC_SIDEBAR, m_bSidebar);
	DDX_Text(pDX, IDC_EDIT, m_sEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSkinMenuTestDlg, CDialog)
	//{{AFX_MSG_MAP(CSkinMenuTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_CBN_SELCHANGE(IDC_COLORSCHEME, OnSelchangeColorscheme)
	ON_WM_ERASEBKGND()
	ON_BN_CLICKED(IDC_GRADIENT, OnGradientChange)
	ON_BN_CLICKED(IDC_SIDEBAR, OnSidebarChange)
	ON_COMMAND(ID_FILE_BROWSE, OnBrowse)
	ON_WM_CONTEXTMENU()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSkinMenuTestDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_browser.Navigate("about:blank", NULL, NULL, NULL, NULL);

	ResetMenuMgr();
	
	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 CSkinMenuTestDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSkinMenuTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSkinMenuTestDlg::OnBrowse() 
{
#ifndef NO_SKIN_INI
	CFileDialog dialog(TRUE, NULL, m_sSkinPath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		"Skins (*.xml)|*.xml||");
	
	if (dialog.DoModal() == IDOK)
	{
		CSkinIniGlobalsFile sif;
		CString sSkinPath = dialog.GetPathName();

		if (sif.LoadSkin(sSkinPath))
		{
			CSkinMenuMgr::LoadSkin(&sif);

			m_sSkinPath = sSkinPath;
			UpdateData(FALSE);
		}
		else
		{
			CString sMessage;
			sMessage.Format("'%s' is not a skin definition file or does not contain all the necessary resources", sSkinPath);
			AfxMessageBox(sMessage);
		}
	}
#else
	// generic browse to display file open
	CFileDialog dialog(TRUE, NULL, "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		"All Files (*.*)|*.*||");
	
	dialog.DoModal();
#endif
}

void CSkinMenuTestDlg::OnSelchangeColorscheme() 
{
	UpdateData();

	ResetMenuMgr();
}

void CSkinMenuTestDlg::ResetMenuMgr()
{
	CSkinMenuMgr::Initialize((m_bSidebar ? SKMS_SIDEBAR : 0) | SKMS_FLAT, 8);
	CSkinMenu::SetRenderer(this);
	
	switch (m_nColorScheme)
	{
	case COLOR_DEF:
		CSkinMenuMgr::ClearColors();
		break;

	case COLOR_COFFEE:
		CSkinMenuMgr::SetColor(COLOR_MENU, RGB(248, 242, 224));
		CSkinMenuMgr::SetColor(COLOR_WINDOWTEXT, RGB(110, 90, 25));
		CSkinMenuMgr::SetColor(COLOR_HIGHLIGHTTEXT, RGB(255, 255, 255));
		CSkinMenuMgr::SetColor(COLOR_3DSHADOW, RGB(150, 130, 70));
		break;

	case COLOR_BLUE:
		CSkinMenuMgr::SetColor(COLOR_MENU, RGB(200, 235, 255));
		CSkinMenuMgr::SetColor(COLOR_WINDOWTEXT, RGB(0, 70, 100));
		CSkinMenuMgr::SetColor(COLOR_HIGHLIGHTTEXT, RGB(255, 255, 255));
		CSkinMenuMgr::SetColor(COLOR_3DSHADOW, RGB(70, 100, 130));
		break;
	}
}

BOOL CSkinMenuTestDlg::DrawMenuNonClientBkgnd(CDC* pDC, LPRECT pRect)
{
	if (m_bGradient)
	{
		DrawGradientBkgnd(pDC, pRect, NULL);
		return TRUE;
	}
	else
		return FALSE;
}

BOOL CSkinMenuTestDlg::DrawMenuClientBkgnd(CDC* pDC, LPRECT pRect, LPRECT pClip)
{
	if (m_bGradient)
	{
		DrawGradientBkgnd(pDC, pRect, pClip);
		return TRUE;
	}
	else
		return FALSE;
}

void CSkinMenuTestDlg::DrawGradientBkgnd(CDC* pDC, LPRECT pRect, LPRECT pClip)
{
	COLORREF crFrom = CSkinMenuMgr::GetColor(COLOR_MENU);
	COLORREF crTo = RGB(255, 255, 255);

	if (pClip)
	{
		// ensure that pClip is at least 100 pixels high else the 
		// gradient has artifacts
		CRect rClip(pClip), rRect(pRect);

		if (rClip.Height() < 100 && rRect.Height() > 100)
		{
			rClip.InflateRect(0, (min(rRect.Height(), 100) - rClip.Height()) / 2);

			if (rClip.top < rRect.top)
				rClip.OffsetRect(0, rRect.top - rClip.top);

			else if (rClip.bottom > rRect.bottom)
				rClip.OffsetRect(0, rRect.bottom - rClip.bottom);
		}

		float fHeight = (float)rRect.Height();

		float fFromFactor = (pRect->bottom - rClip.top) / fHeight;
		float fToFactor = (pRect->bottom - rClip.bottom) / fHeight;

		crFrom = CSkinBase::BlendColors(crFrom, crTo, fFromFactor);
		crTo = CSkinBase::BlendColors(crFrom, crTo, fToFactor);

		CSkinBase::GradientFill(pDC, rClip, crFrom, crTo, FALSE);
	}
	else
		CSkinBase::GradientFill(pDC, pRect, crFrom, crTo, FALSE);
}

BOOL CSkinMenuTestDlg::DrawMenuSidebar(CDC* pDC, LPRECT pRect, LPCTSTR szTitle)
{
	if (m_bGradient)
	{
		COLORREF crColor = CSkinMenuMgr::GetColor(COLOR_3DSHADOW);

		COLORREF crFrom = crColor;
		COLORREF crTo = CSkinBase::VaryColor(crFrom, 2.0f);

		CSkinBase::GradientFill(pDC, pRect, crFrom, crTo, FALSE);
		return TRUE;
	}
	else 
	{
		COLORREF crColor = CSkinMenuMgr::GetColor(COLOR_3DSHADOW);

		pDC->FillSolidRect(pRect, crColor);
		return TRUE;
	}

	return FALSE;
}

BOOL CSkinMenuTestDlg::DrawMenuBorder(CDC* pDC, LPRECT pRect)
{
	return FALSE;
}

BOOL CSkinMenuTestDlg::OnEraseBkgnd(CDC* pDC) 
{
	return CDialog::OnEraseBkgnd(pDC);
}

void CSkinMenuTestDlg::OnGradientChange() 
{
	UpdateData();
}

void CSkinMenuTestDlg::OnSidebarChange() 
{
	UpdateData();

	ResetMenuMgr();
}

void CSkinMenuTestDlg::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	if (pWnd != this)
		return;

	CMenu menu;
	
	if (menu.LoadMenu(IDR_DIALOG_CONTEXT))
	{
		CMenu* pSub = menu.GetSubMenu(0);

		if (pSub)
			pSub->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
	}
}

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
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

Comments and Discussions