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

Owner Drawn Menu with Icons, Titles and Shading

Rate me:
Please Sign up or sign in to vote.
4.95/5 (205 votes)
5 Dec 2003CPOL12 min read 2.8M   35K   522  
An easy use of owner drawn menu with variable styles like new Office products with titles, shading and icons.
#include "..\mdi\mainfrm.h"
#include "..\mdi\mainfrm.h"
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MDIFloating.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CNewFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CNewFrameWnd)
	ON_WM_CREATE()
	ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

static WORD ToolId[] = { IDR_NEWMENU256,
                        16, 15,
                        ID_FILE_NEW,
                        ID_FILE_OPEN,
                        ID_FILE_SAVE,
                        ID_EDIT_CUT,
                        ID_EDIT_COPY,
                        ID_EDIT_PASTE,
                        ID_FILE_PRINT,
                        ID_APP_ABOUT,
                        ID_WINDOW_NEW,
                        ID_WINDOW_CASCADE,
                        ID_WINDOW_TILE_HORZ,
                        ID_WINDOW_TILE_VERT,
                        ID_RECORD_FIRST,
                        ID_RECORD_LAST,
                        ID_FILE_PRINT_SETUP,
                        ID_FILE_SAVE_AS,
                        ID_EDIT_REDO,
                        ID_EDIT_UNDO,
                        ID_APP_EXIT,
                        ID_EDIT_FIND,
                        ID_FILE_CLOSE,
                        ID_FILE_SEND_MAIL,
                        ID_DEFAULT_HELP,NULL};


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CNewFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	// TODO: Delete these three lines if you don't want the toolbar to be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	m_DefaultNewMenu.LoadToolBar(ToolId,RGB(192,192,192));
	m_SystemNewMenu.SetMenuTitle(_T("Floating Sample"),MFT_ROUND|MFT_CENTER|MFT_SIDE_TITLE);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CNewFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CNewFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CNewFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame message handlers

BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle, CWnd* pParentWnd, CCreateContext* pContext) 
{
	// base class does the real work

	if (!CNewFrameWnd::LoadFrame(nIDResource, dwDefaultStyle, pParentWnd, pContext))
	{
		return FALSE;
	}

	CWinApp* pApp = AfxGetApp();
	if (pApp->m_pMainWnd == NULL)
		pApp->m_pMainWnd = this;

	// replace Exit option on File menu with Close for secondary windows
	if (AfxGetApp()->m_pMainWnd != this)
	{
		CMenu *pMenu = GetMenu();
		ASSERT(pMenu);
		pMenu->ModifyMenu(ID_APP_EXIT, MF_BYCOMMAND | MF_STRING, ID_FILE_CLOSE, _T("&Close"));
	}

	return TRUE;
}

void CMainFrame::OnFileClose()
{
   DestroyWindow();
}


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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions