Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / C++

Bitmap Menu

Rate me:
Please Sign up or sign in to vote.
4.96/5 (13 votes)
8 Mar 2013CPOL1 min read 44.5K   2.1K   33  
A simple way to have a bitmap menu without any bitmap resource or ownerdraw
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "TestMenu.h"

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_INITMENUPOPUP()
	ON_WM_CONTEXTMENU()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

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

}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if(CMDIFrameWnd::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);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if(! CMDIFrameWnd::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
{
	CMDIFrameWnd::AssertValid();
}

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

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
	CMDIFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);

	// TODO: Add your message handler code here

	HICON hIcon = AfxGetApp()->LoadIcon(IDR_TESTMETYPE);
	pPopupMenu->SetMenuItemBitmaps(ID_FILE_NEW,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_SMALL);
	pPopupMenu->SetMenuItemBitmaps(ID_FILE_OPEN,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_SAVE);
	pPopupMenu->SetMenuItemBitmaps(ID_FILE_SAVE,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_CLOSE);
	pPopupMenu->SetMenuItemBitmaps(ID_FILE_CLOSE,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_PRINT);
	pPopupMenu->SetMenuItemBitmaps(ID_FILE_PRINT,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_UNDO);
	pPopupMenu->SetMenuItemBitmaps(ID_EDIT_UNDO,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_CUT);
	pPopupMenu->SetMenuItemBitmaps(ID_EDIT_CUT,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_COPY);
	pPopupMenu->SetMenuItemBitmaps(ID_EDIT_COPY,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_PASTE);
	pPopupMenu->SetMenuItemBitmaps(ID_EDIT_PASTE,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_ABOUT);
	pPopupMenu->SetMenuItemBitmaps(ID_APP_ABOUT,MF_BYCOMMAND,ConvertIconToBitmap(hIcon),NULL);
}

void CMainFrame::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// TODO: Add your message handler code here

	CMenu* pMenu = GetMenu();
	CMenu* pSubMenu = pMenu->GetSubMenu(0);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,point.x,point.y,AfxGetMainWnd());
}

CBitmap* CMainFrame::ConvertIconToBitmap(HICON hIcon)
{
	const int cx = GetSystemMetrics(SM_CXSMICON);
	const int cy = GetSystemMetrics(SM_CYSMICON);

	CDC dc;
	CBitmap bmp;
	CClientDC bmpDC(this);
	dc.CreateCompatibleDC(&bmpDC);
	bmp.CreateCompatibleBitmap(&bmpDC, cx, cy);
	CBitmap* pOldBmp = (CBitmap*)dc.SelectObject(&bmp);
	::DrawIconEx(dc.GetSafeHdc(), 0, 0, hIcon, cx, cy, 0, (HBRUSH)GetSysColor(COLOR_MENU), DI_NORMAL);
	dc.SelectObject(pOldBmp);
	dc.DeleteDC();
	HBITMAP hBitmap = (HBITMAP)::CopyImage((HANDLE)((HBITMAP)bmp), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE);
	if(NULL != hBitmap)
		return CBitmap::FromHandle(hBitmap);

	return NULL;
}

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
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions