📄 popupmenu.cpp
字号:
// PopupMenu.cpp: implementation of the CPopupMenu class.
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PopupMenu.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
#ifndef OBM_CHECK
#define OBM_CHECK 32760 // from winuser.h
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPopupMenu::CPopupMenu()
{
crMenuText = GetSysColor(COLOR_MENUTEXT);
crMenuTextSel = GetSysColor(COLOR_HIGHLIGHTTEXT);
cr3dFace = GetSysColor(COLOR_3DFACE);
crMenu = GetSysColor(COLOR_MENU);
crHighlight = GetSysColor(COLOR_HIGHLIGHT);
cr3dHilight = GetSysColor(COLOR_3DHILIGHT);
cr3dShadow = GetSysColor(COLOR_3DSHADOW);
crGrayText = GetSysColor(COLOR_GRAYTEXT);
m_clrBtnFace = GetSysColor(COLOR_BTNFACE);
m_clrBtnHilight = GetSysColor(COLOR_BTNHILIGHT);
m_clrBtnShadow = GetSysColor(COLOR_BTNSHADOW);
iSpawnItem = 0;
pSpawnItem = NULL;
iImageItem = 0;
pImageItem = NULL;
szImage = CSize(20, 20);
hMenuFont = NULL;
NONCLIENTMETRICS ncm;
memset(&ncm, 0, sizeof(ncm));
ncm.cbSize = sizeof(ncm);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, static_cast<PVOID>(&ncm), 0);
hGuiFont = ::CreateFontIndirect(&ncm.lfMenuFont);
// David 08/04/98 - start - bold font handling
hMenuBoldFont = NULL;
CreateBoldFont();
// David 08/04/98 - end - bold font handling
}
CPopupMenu::~CPopupMenu()
{
if (iSpawnItem > 0)
{
for (int t = 0; t < iSpawnItem; t++)
{
if (pSpawnItem[t])
delete pSpawnItem[t];
}
GlobalFree(static_cast<HGLOBAL>(pSpawnItem));
}
if (iImageItem > 0)
{
GlobalFree(static_cast<HGLOBAL>(pImageItem));
}
if (hMenuFont)
::DeleteObject(static_cast<HGDIOBJ>(hMenuFont));
if (hMenuBoldFont)
::DeleteObject(static_cast<HGDIOBJ>(hMenuBoldFont));
}
void CPopupMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
// CRect rcItem(lpDrawItemStruct->rcItem);
// pDC->FillSolidRect(rcItem, RGB(255,0,0));
if (lpDrawItemStruct->CtlType == ODT_MENU)
{
UINT state = lpDrawItemStruct->itemState;
BOOL bEnab = !(state & ODS_DISABLED);
BOOL bSelect = (state & ODS_SELECTED) ? TRUE : FALSE;
BOOL bChecked = (state & ODS_CHECKED) ? TRUE : FALSE;
// David 08/04/98 - start - bold font handling
BOOL bBold = (state & ODS_DEFAULT) ? TRUE : FALSE;
// David 08/04/98 - end - bold font handling
SpawnItem* pItem = reinterpret_cast<SpawnItem*>(lpDrawItemStruct->itemData);
if (pItem)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CFont* pft;
// David 08/04/98 - start - bold font handling
if (!bBold)
pft = CFont::FromHandle(static_cast<HFONT>(hMenuFont) ? hMenuFont : hGuiFont);
else
pft = CFont::FromHandle(static_cast<HFONT>(hMenuBoldFont) ? hMenuBoldFont : hGuiFont);
// David 08/04/98 - end - bold font handling
CFont* of = pDC -> SelectObject(pft);
CRect rc(lpDrawItemStruct->rcItem);
CRect rcImage(rc), rcText(rc);
rcImage.right = rcImage.left + rc.Height();
rcImage.bottom = rc.bottom;
if (pItem -> iCmd == -3) // is a separator
{
CPen pnDk(PS_SOLID, 1, cr3dShadow);
CPen pnLt(PS_SOLID, 1, cr3dHilight);
CPen * opn = pDC->SelectObject(&pnDk);
pDC -> MoveTo(rc.left + 2, rc.top + 2);
pDC -> LineTo(rc.right - 2, rc.top + 2);
pDC -> SelectObject(&pnLt);
pDC -> MoveTo(rc.left + 2, rc.top + 3);
pDC -> LineTo(rc.right - 2, rc.top + 3);
pDC -> SelectObject(opn);
}
else if (pItem -> iCmd == -4) // is a title item
{
CString cs(pItem -> cText), cs1;
CRect rcBdr(rcText);
if (bSelect && bEnab)
{
rcText.top ++;
rcText.left += 2;
}
pDC -> FillSolidRect(rcText, crMenu);
pDC -> DrawText(cs, rcText, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
if (bSelect && bEnab)
pDC -> Draw3dRect(rcBdr, cr3dShadow, cr3dHilight);
}
else
{
rcText.left += rcImage.right + 1;
int obk = pDC -> SetBkMode(TRANSPARENT);
COLORREF ocr;
if (bSelect)
{
if (pItem -> iImageIdx >= 0 || (state & ODS_CHECKED))
pDC -> FillSolidRect(rcText, crHighlight);
else
pDC -> FillSolidRect(rc, crHighlight);
ocr = pDC -> SetTextColor(crMenuTextSel);
}
else
{
if (pItem -> iImageIdx >= 0 || (state & ODS_CHECKED))
pDC -> FillSolidRect(rcText, crMenu);
else
pDC -> FillSolidRect(rc/*rcText*/, crMenu);
ocr = pDC -> SetTextColor(crMenuText);
}
if (pItem->iImageIdx >= 0)
{
int ay = (rcImage.Height() - szImage.cy) / 2;
int ax = (rcImage.Width() - szImage.cx) / 2;
if (bSelect && bEnab)
pDC -> Draw3dRect(rcImage, cr3dHilight, cr3dShadow);
else
{
pDC -> Draw3dRect(rcImage, crMenu, crMenu);
}
if (bEnab)
{
ilList.Draw(pDC, pItem -> iImageIdx, CPoint(rcImage.left + ax,
rcImage.top +ay), ILD_NORMAL);
}
else
{
HICON hIcon = ilList.ExtractIcon(pItem -> iImageIdx);
pDC -> DrawState(CPoint(rcImage.left + ax, rcImage.top + ay), szImage,
static_cast<HICON>(hIcon), DST_ICON | DSS_DISABLED,
static_cast<CBrush*>(NULL));
}
}
else
{
/*
if (bChecked)
{
int ay = (rcImage.Height() - szImage.cy) / 2;
int ax = (rcImage.Width() - szImage.cx) / 2;
ilOther.Draw(pDC, 0, CPoint(rcImage.left + ax, rcImage.top + ay - 2), ILD_NORMAL);
}
*/
if (bChecked)
{
CRect rcTemp = rcImage;
rcTemp.DeflateRect(2, 2);
DrawCheckmark(*pDC, rcTemp, bSelect);
}
if (bSelect || lpDrawItemStruct -> itemAction == ODA_SELECT)
{
COLORREF colorBG = GetSysColor(bSelect ? COLOR_HIGHLIGHT : COLOR_MENU);
// selected or selection state changed: paint text background
CRect rcBG(lpDrawItemStruct -> rcItem); // the whole rectangle
rcBG.left += rcImage.Width(); // do not paint over it!
CBrush brush(colorBG);
CBrush* pOldBrush = pDC -> SelectObject(&brush);
pDC -> PatBlt(rcBG.left, rcBG.top, rcBG.Width(), rcBG.Height(), PATCOPY);
pDC -> SelectObject(pOldBrush);
}
}
CString cs(pItem -> cText), cs1;
CSize sz;
sz = pDC -> GetTextExtent(cs);
int ay1 = (rcText.Height() - sz.cy) / 2;
rcText.top += ay1;
rcText.left += 2;
rcText.right -= 15;
int tf = cs.Find('\t');
if (tf >= 0)
{
cs1 = cs.Right(cs.GetLength() - tf - 1);
cs = cs.Left(tf);
if (!bEnab)
{
if (!bSelect)
{
CRect rcText1(rcText);
rcText1.InflateRect(-1, - 1);
pDC -> SetTextColor(cr3dHilight);
pDC -> DrawText(cs, rcText1, DT_VCENTER | DT_LEFT);
pDC -> DrawText(cs1, rcText1, DT_VCENTER | DT_RIGHT);
pDC -> SetTextColor(crGrayText);
pDC -> DrawText(cs, rcText, DT_VCENTER | DT_LEFT);
pDC -> DrawText(cs1, rcText, DT_VCENTER | DT_RIGHT);
}
else
{
pDC -> SetTextColor(crMenu);
pDC -> DrawText(cs, rcText, DT_VCENTER | DT_LEFT);
pDC -> DrawText(cs1, rcText, DT_VCENTER | DT_RIGHT);
}
}
else
{
pDC -> DrawText(cs, rcText, DT_VCENTER | DT_LEFT);
pDC -> DrawText(cs1, rcText, DT_VCENTER | DT_RIGHT);
}
}
else
{
if (!bEnab)
{
if (!bSelect)
{
CRect rcText1(rcText);
rcText1.InflateRect(-1, - 1);
pDC -> SetTextColor(cr3dHilight);
pDC -> DrawText(cs, rcText1, DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
pDC -> SetTextColor(crGrayText);
pDC -> DrawText(cs, rcText, DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
}
else
{
pDC -> SetTextColor(crMenu);
pDC -> DrawText(cs, rcText, DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
}
}
else
pDC -> DrawText(cs, rcText, DT_VCENTER | DT_LEFT | DT_EXPANDTABS);
}
pDC -> SetTextColor(ocr);
pDC -> SetBkMode(obk);
}
pDC -> SelectObject(of);
}
}
}
void CPopupMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// lpMeasureItemStruct -> itemWidth = 200;
// lpMeasureItemStruct -> itemHeight = 25;
if (lpMeasureItemStruct -> CtlType == ODT_MENU)
{
SpawnItem* pItem = reinterpret_cast<SpawnItem*>(lpMeasureItemStruct -> itemData);
if (pItem)
{
if (pItem -> iCmd == -3) // is a separator
{
lpMeasureItemStruct -> itemWidth = 10;
lpMeasureItemStruct -> itemHeight = 6;
}
else
{
CString cs(pItem -> cText);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -