Introduction
This is a sizeable GradientMenu with settings for Menu -Textcolor,
-Backgroundcolor and support for the MFC-Framework. Messages it handles are
CCmdUI, WM_...MESSAGESTRING etc.
Background
The CGradientMenu class of Jim
Koornneef don’t automatic update the states of the menu entries. I decided
to write a class based on his adding some new features to it.
Using the code
Because Jim Koornneef described all necessary things to use his GradientMenu class I give here only some additional
comments about the changes and enhancements:
Changes:
CGradientMenu::CGradientMenu -> set Menu-Fontsize
CGradientMenu::OnPaint -> uses Menu-Fontsize, removing
Ampersand's
CGradientMenu::PopupSubMenu -> uses
SetMenuColors()
CGradientMenu :: TrackPopupMenu -> only
WM_INITMENUPOPUP to get update infos for the menu
(CCmdUI)
MenuToolBar::OnCreate -> uses Menu-Fontsize
MenuToolBar::OnCustomDrawNotify -> uses Menu- and
HighLight-Colors, handling of TAB's
MenuToolBar::OnMouseMove -> send MessageString
New:
CGradientMenu::ChangeState -> set state (gray / check) of
MenuItem
CGradientMenu::SetMenuColors -> set colors for Menu-Text,
-GrayText, -BackGround, -Highlights
MenuToolBar::OnEraseBkgnd -> uses Menu-Background
Sample
MainFrame.h
[otected:
CToolBar m_wndMenuBar;
CFont dfont;
int iFontHight;
int iMenuFontHight;
int iGradientWidth;
]
MainFrame.cpp
[
.
.
.
#include "afxadv.h" #include "GradientMenu.h"
CMainFrame::~CMainFrame()
{
dfont.DeleteObject ();
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
int _scx, _scy;
HDC hdc;
hDC = ::GetDC (NULL);
_scx = GetDeviceCaps(hDC, HORZRES);
_scy = GetDeviceCaps(hDC, VERTRES);
_scb = GetDeviceCaps(hDC, BITSPIXEL);
::ReleaseDC (NULL, hDC);
iFontHight = 16 * _scy / 768;
iMenuFontHight = 14 * _scy / 768;
iGradientWidth = iMenuFontHight + 8;
dfont.CreateFont (
iFontHight, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
if (!m_wndMenuBar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_WRAPABLE,
WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP
| CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC,
CRect(0,0,_scx, 0), CG_ID_VIEW_MENUBAR))
{
AfxMessageBox ("Failed to create menubar");
return -1; }
m_wndMenuBar.GetToolBarCtrl ().SetBitmapSize(CSize (0, 0));
m_wndMenuBar.GetToolBarCtrl ().SetButtonSize (
CSize (90, 8 + iFontHight));
m_wndMenuBar.GetToolBarCtrl ().SetFont (&dfont, TRUE);
TBBUTTON tbb;
int nStr;
memset(&tbb, 0, sizeof(TBBUTTON));
tbb.fsState = TBSTATE_ENABLED;
tbb.fsStyle = TBSTYLE_BUTTON|TBSTYLE_AUTOSIZE;
tbb.iBitmap = -1;
tbb.dwData = NULL;
nStr = m_wndMenuBar.GetToolBarCtrl ().AddStrings(IDString (IDS_MB_FILE));
tbb.iString = nStr;
tbb.idCommand = ID_MB_FILE;
m_wndMenuBar.GetToolBarCtrl ().AddButtons(1, &tbb);
.
.
.
nStr = m_wndMenuBar.GetToolBarCtrl ().AddStrings(IDString (IDS_MB_WINDOW));
tbb.iString = nStr;
tbb.idCommand = ID_MB_WINDOW;
m_wndMenuBar.GetToolBarCtrl ().AddButtons(1, &tbb);
EnableDocking(CBRS_ALIGN_ANY);
m_wndMenuBar.EnableDocking(CBRS_ALIGN_TOP);
DockControlBar(&m_wndMenuBar);
.
.
}
.
.
.
void CMainFrame::OnFile()
{
RECT rec;
POINT pnt;
m_wndMenuBar.GetToolBarCtrl ().GetRect (ID_MB_FILE, &rec);
ClientToScreen(&rec);
pnt.y = rec.bottom - 1;
pnt.x = rec.left;
COLORREF rgb1(RGB(128, 255, 128));
COLORREF rgb2(RGB(0, 0, 0));
COLORREF rgbText(RGB(255, 255, 255));
COLORREF rgbMText(RGB(0, 0, 90));
COLORREF rgbMGray = GetSysColor(COLOR_GRAYTEXT);
COLORREF rgbMBack (RGB(255, 255, 230));
CGradientMenu oMenu(iGradientWidth, TRUE, iMenuFontHight);
oMenu.LoadMenu(IDR_BEARBEITEN);
oMenu.SetGradientColors(rgb1, rgb2, rgbText);
oMenu.SetMenuColors(rgbMText, rgbMGray, rgbMBack);
oMenu.SetTitle((CString)IDString (IDS_MB_FILE));
oMenu.TrackPopupMenu(0, pnt.x, pnt.y, this);
}
.
.
.
void CMainFrame::OnWindow()
{
RECT rec;
POINT pnt;
int i;
UINT flags;
CMedGrf32Doc* pDoc;
POSITION pos;
CWnd* pFrame;
CMyView* pView;
char buf[1024], bff[_MAX_PATH];
m_wndMenuBar.GetToolBarCtrl ().GetRect (ID_MB_FENSTER, &rec);
ClientToScreen(&rec);
pnt.y = rec.bottom - 1;
pnt.x = rec.left;
COLORREF rgb1(RGB(128, 255, 128));
COLORREF rgb2(RGB(0, 0, 0));
COLORREF rgbText(RGB(255, 255, 255));
COLORREF rgbMText(RGB(0, 0, 90));
COLORREF rgbMGray = GetSysColor(COLOR_GRAYTEXT);
COLORREF rgbMBack (RGB(255, 255, 230));
CGradientMenu oMenu(iGradientWidth, TRUE, iMenuFontHight);
oMenu.LoadMenu(IDR_FENSTER);
oMenu.AppendMenu (MFT_SEPARATOR, 0, "");
for (i = 0; i < iImgCnt; i++)
{
flags = MF_STRING | MF_ENABLED;
if (i == iImgAct)
flags |= MF_CHECKED;
*bff = 0;
if (pImgDoc[i] != NULL)
{
pDoc = (CMyDoc*)pImgDoc[i];
pos = pDoc->GetFirstViewPosition();
pView = (CMyView*)pDoc->GetNextView(pos);
pFrame = pView->GetParentFrame ();
pFrame->GetWindowText (bff, sizeof (bff));
}
sprintf (buf, "%d %s", i + 1, bff);
oMenu.AppendMenu (flags, AFX_IDM_FIRST_MDICHILD + i, buf);
}
oMenu.SetGradientColors(rgb1, rgb2, rgbText);
oMenu.SetMenuColors(rgbMText, rgbMGray, rgbMBack);
oMenu.SetTitle((CString)IDString (IDS_MB_FENSTER));
oMenu.TrackPopupMenu(0, pnt.x, pnt.y, this);
}
]