📄 cdrawmenu.cpp
字号:
#include "stdafx.h"
#include "cDrawMenu.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CDrawMenu::CDrawMenu()
{
m_clrText = GetSysColor (COLOR_MENUTEXT); // Default menu text colour
m_clrBack = GetSysColor (COLOR_MENU);
m_clrIconBack = m_clrBack;
m_brBackground.CreateSolidBrush (m_clrBack);
m_brIconBack.CreateSolidBrush (m_clrIconBack);
m_penBack.CreatePen (PS_SOLID,0,m_clrBack);
m_crText = m_clrText;
m_bLBtnDown = FALSE;
m_SubMenus = NULL;
m_iconX = 16; // Icon sizes default to 16 x 16
m_iconY = 16; // ...
m_clrHilight = GetSysColor (COLOR_HIGHLIGHT); // User Colours...
m_brSelect.CreateSolidBrush (m_clrHilight); // Create a brush
m_clrHilightText = GetSysColor (COLOR_HIGHLIGHTTEXT); // User Colours...
/*
ZeroMemory ((PVOID) &m_lf,sizeof (LOGFONT));
NONCLIENTMETRICS nm;
nm.cbSize = sizeof (NONCLIENTMETRICS);
//Get the system metrics for the Captionfromhere
VERIFY (SystemParametersInfo (SPI_GETNONCLIENTMETRICS,0,&nm,0));
m_lf = nm.lfMenuFont;
*/
m_iMenuHeight = GetSystemMetrics(SM_CYMENU);
//m_iMenuHeight = nm.iMenuHeight;
// m_fontMenu.CreateFontIndirect (&m_lf);
// m_fontMenu = (CFont *)CWnd::GetFont();
VERIFY(m_fontMenu.CreateFont(
16,
0 ,
0 ,
0 ,
FW_NORMAL,
FALSE,
FALSE,
0 ,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_SWISS,
L"宋体")
);
m_fontMenu.GetLogFont(&m_lf);
/*
m_lf.lfHeight = 12 ;
m_lf.lfWidth = 0 ;
m_lf.lfEscapement = 0;
m_lf.lfOrientation = 0 ;
m_lf.lfWeight = FW_NORMAL;
m_lf.lfItalic = FALSE;
m_lf.lfUnderline = FALSE;
m_lf.lfStrikeOut = 0;
m_lf.lfCharSet = ANSI_CHARSET;
m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
m_lf.lfQuality = DEFAULT_QUALITY;
m_lf.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
m_lf.lfFaceName = L"宋体";
m_iMenuHeight = GetSystemMetrics(SM_CYMENU);
VERIFY(m_fontMenu.CreateFontIndirect(&m_lf));
*/
}
CDrawMenu::~CDrawMenu()
{
DestroyMenu();
}
BOOL CDrawMenu::DestroyMenu()
{
// Destroy Sub menus:
/*
int m;
int numSubMenus = m_SubMenus.GetUpperBound();
for(m = numSubMenus; m >= 0; m--)
delete(m_SubMenus[m]);
m_SubMenus.RemoveAll();
*/
delete(m_SubMenus);
// Destroy brushes:
if ((HBRUSH) m_brBackground != NULL) // Background brush
m_brBackground.DeleteObject ();
if ((HFONT)m_fontMenu !=NULL) // Font for the menu
m_fontMenu.DeleteObject ();
if ((HBRUSH)m_brSelect != NULL) // Selected background brush
m_brSelect.DeleteObject ();
// Destroy menu data
int numItems = m_MenuList.GetUpperBound();
for(int m = 0; m <= numItems; m++)
delete(m_MenuList[m]);
m_MenuList.RemoveAll();
// Call base-class implementation last:
return(CMenu::DestroyMenu());
};
BOOL CDrawMenu::OnIniMenu()
{
if(m_SubMenus != NULL)
{
delete m_SubMenus;
m_SubMenus = NULL;
}
m_SubMenus = new CDrawMenu;
m_SubMenus->CreatePopupMenu();
//set default settings
m_SubMenus->SetIconBgColor(RGB(172,198,244));
m_SubMenus->SetBackColor(RGB(137,173,239));
m_SubMenus->SetHighlightColor(RGB(0,0,255));
//m_SubMenus->SetHighlightStyle(Normal);
//m_SubMenus->SetHighlightTextColor(RGB(0,0,255));
m_SubMenus->SetTextColor(RGB(0,0,128));
return TRUE;
}
void CDrawMenu::DrawItem (LPDRAWITEMSTRUCT lpDIS)
{
ASSERT(lpDIS != NULL);
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
CRect rect;
HICON hIcon;
COLORREF crText = m_crText;
// draw the colored rectangle portion
rect.CopyRect(&lpDIS->rcItem);
// draw the up/down/focused/disabled state
UINT action = lpDIS->itemAction;
UINT state = lpDIS->itemState;
CString strText;
LOGFONT lf;
lf = m_lf;
CFont dispFont;
CFont *pFont;
//GetWindowText(strText);
if (lpDIS->itemData != NULL)
{
UINT nIconNormal = (((CDrawMenuData*)(lpDIS->itemData))->menuIconNormal);
UINT nIconSelected = (((CDrawMenuData*)(lpDIS->itemData))->menuIconSelected);
UINT nIconDisabled = (((CDrawMenuData*)(lpDIS->itemData))->menuIconDisabled);
strText = (((CDrawMenuData*) (lpDIS->itemData))->menuText);
if(nIconNormal == -1)
hIcon = NULL;
else
{
hIcon = NULL;
// Obtain the IDs for the appropriate Icons:
if (state & ODS_SELECTED && !(state & ODS_GRAYED)) // Selected (but not disabled)
{
if(nIconSelected != -1)
hIcon = AfxGetApp ()->LoadIcon(nIconSelected);
}
else
{
if(state & ODS_GRAYED) // Disabled (selected or not)
if(nIconDisabled != -1)
hIcon = AfxGetApp()->LoadIcon(nIconDisabled);
};
// If we didn't manage to select a specific icon, we'll use the
// default (normal) one...
if(hIcon == NULL)
hIcon = AfxGetApp()->LoadIcon(nIconNormal);
}
}
else
{
strText.Empty();
hIcon = NULL;
}
if ( (state & ODS_SELECTED) )
{
// draw the down edges
CPen *pOldPen = pDC->SelectObject (&m_penBack);
// You need only Text highlight and thats what you get
if (m_hilightStyle != Normal)
{
pDC->FillRect (rect,&m_brBackground);
CRect rectTemp;
rectTemp.CopyRect(rect);
rectTemp.right = rectTemp.left + ((m_iconX)?m_iconX:32) + 2;
pDC->FillRect (rectTemp,&m_brIconBack);
}
else
pDC->FillRect (rect,&m_brSelect);
pDC->SelectObject (pOldPen);
pDC->Draw3dRect (rect,GetSysColor (COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW));
// This version provides a black-border:
pDC->Draw3dRect (rect,GetSysColor (COLOR_3DHILIGHT),RGB(0,0,0));
//lf.lfWeight = FW_BOLD;
if ((HFONT)dispFont != NULL)
dispFont.DeleteObject ();
dispFont.CreateFontIndirect (&lf);
crText = m_clrHilightText;
}
else
{
CPen *pOldPen = pDC->SelectObject (&m_penBack);
pDC->FillRect (rect,&m_brBackground);
CRect rectTemp;
rectTemp.CopyRect(rect);
rectTemp.right = rectTemp.left + ((m_iconX)?m_iconX:32) + 2;
pDC->FillRect (rectTemp,&m_brIconBack);
pDC->SelectObject (pOldPen);
// draw the up edges
pDC->Draw3dRect (rect,m_clrBack,m_clrBack);
if ((HFONT)dispFont != NULL)
dispFont.DeleteObject ();
dispFont.CreateFontIndirect (&lf); //Normal
}
// draw the text if there is any
//We have to paint the text only if the image is nonexistant
if (hIcon != NULL)
{
DrawIconEx (pDC->GetSafeHdc(),rect.left,rect.top,hIcon,
m_iconX,m_iconY,0,NULL,DI_NORMAL);
DestroyIcon(hIcon);
};
//This is needed always so that we can have the space for check marks
//AfxMessageBox(strText);
rect.left = rect.left +((m_iconX)?m_iconX:32) + 2;
if ( !strText.IsEmpty())
{
//pFont->GetLogFont (&lf);
pFont = pDC->SelectObject (&dispFont);
int iOldMode = pDC->GetBkMode();
pDC->SetBkMode( TRANSPARENT);
// Draw the text in the correct colour:
//AfxMessageBox(strText);
//CString s;
UINT nFormat = DT_LEFT|DT_SINGLELINE|DT_EXPANDTABS|DT_VCENTER;
if(!(lpDIS->itemState & ODS_GRAYED))
{
pDC->SetTextColor(crText);
//int i = pDC->DrawText (strText,rect,nFormat);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -