菜单项的自绘
菜单项的自绘
步骤: 1.将需要自绘菜单项加上MF_OWNERDRAW,使用ModeifyMenu函数比较适合 2.使用WM_MEASUREITEM 设置菜单项宽度与高度 3.使用WM_DRAWITEM 绘制菜单项
CMenu*pMenu=pFrame->GetMenu();
ASSERT(pFrame!=NULL);
for(int i=0;i<5;i++)
pMenu->ModifyMenu(IDM_COLOR_RED+i,MF_OWNERDRAW,IDM_COLOR_RED+i);
return TRUE;
void CMainFrame::OnMeasureItem(int nIDCtl,LPMEASUREITEMSTRUCT lpmis)
{
lpmis->itemWidth=::GetSystemMetrics(SM_CYMENU)*4;
lpmis->itemHeight=::GetSystemMetrics(SM_CYMENU);
}
绘制菜单项步骤 1.实现高亮选中项pBrush->CreateSolidBrush(::GetSysColor((lpdis->itemState & ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW)); 2.绘制选中项边框dc.FrameRect(&(lpdis->rcItem), pBrush); 3.BitBlt绘制左边选中标志(√),右边矩形填充颜色
void CMainFrame::OnDrawItem(int nIDCtl,LPDRAWITEMSTRUCT lpdis)
{
BITMAP bm;
CBitmap bitmap;
bitmap.LoadOEMBitmap(OBM_CHECK);
bitmap.GetObject(sizeof(bm),&bm);
CDC dc;
dc.Attach(lpdis->hDC);
CBrush* pBrush = new CBrush;
pBrush->CreateSolidBrush(::GetSysColor((lpdis->itemState & ODS_SELECTED) ? COLOR_HIGHLIGHT : COLOR_WINDOW));
dc.FrameRect(&(lpdis->rcItem), pBrush);
if (lpdis->itemState & ODS_CHECKED)
{
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap* pOldBitMap = dcMem.SelectObject(&bitmap);
dc.BitBlt(lpdis->rcItem.left + 4, lpdis->rcItem.top + (((lpdis->rcItem.bottom - lpdis->rcItem.top) - bm.bmHeight) / 2),
bm.bmWidth, bm.bmHeight, &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject(pOldBitMap);
}
pBrush=new CBrush(col[lpdis->itemID-ID_COLOR_RED]);
CRect rect = lpdis->rcItem;
rect.DeflateRect(6,4);
rect.left += bm.bmWidth;
dc.FillRect(rect, pBrush);
delete pBrush;
dc.Detach();
CFrameWnd::OnDrawItem(nIDCtl, lpdis);
}
- 效果图
CListBox项的自绘
步骤: 1.覆盖** CListBox::PreCreateWindow** 函数,确保cs style 包含** LBS_OWNERDRAWFIXED** 或** LBS_OWNERDRAWVARIABLE** 2.覆盖** CListBox::MeasureItem** 设置菜单项宽度与高度 3.覆盖** CListBox::DrawItem** 绘制菜单项
class CMyListBox :public CListBox
{
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
int AddIcon(HICON hIcon);
void ProjectImage(CDC* pDC, LPRECT pRect, COLORREF clrBackColor);
DECLARE_MESSAGE_MAP()
afx_msg void OnLbnSelchange();
};
- 覆盖** CListBox::PreCreateWindow** 函数,确保cs style 包含** LBS_OWNERDRAWFIXED** 或** LBS_OWNERDRAWVARIABLE**
CIconListBox::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |=LBS_OWNERDRAWFIXED;
cs.style &=~(LBS_OWNERDRAWFIXED|LBS_SORT);
}
- 覆盖** CListBox::MeasureItem** 函数,设置listbox item 高度与宽度
void CIconListBox::MeasureItem(LPMEASUREITEMSTRUCT lpmis)
{
lpmis->itemHeight = 32;
}
- 覆盖** CListBox::DrawItem** 函数,开始绘制ListBox项
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpdis)
{
CDC dc;
dc.Attach(lpdis->hDC);
CRect rect = lpdis->rcItem;
UINT nIndex = lpdis->itemID;
CBrush* pBrush = new CBrush(::GetSysColor(lpdis->itemState & ODS_SELECTED ? COLOR_HIGHLIGHT : COLOR_WINDOW));
dc.FillRect(rect, pBrush);
delete pBrush;
if (lpdis->itemState&ODS_FOCUS)
{
dc.DrawFocusRect(rect);
}
if (nIndex!=(UINT)-1)
{
dc.DrawIcon(rect.left + 4, rect.top + 2, (HICON)GetItemData(nIndex));
}
dc.Detach();
}
int CMyListBox::AddIcon(HICON hIcon)
{
int nIndex=AddString(_T(""));
if ((nIndex!=LB_ERR)&&(nIndex!=LB_ERRSPACE))
{
SetItemData(nIndex, (DWORD)hIcon);
}
return nIndex;
}
void CMyListBox::ProjectImage(CDC* pDC, LPRECT pRect, COLORREF clrBackColor)
{
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, 32, 32);
CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap);
CBrush* pBrush = new CBrush(clrBackColor);
dcMem.FillRect(CRect(0, 0, 32, 32), pBrush);
delete pBrush;
int nIndex = GetCurSel();
if (nIndex != LB_ERR)
dcMem.DrawIcon(0, 0, (HICON)GetItemData(nIndex));
pDC->StretchBlt(pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top, &dcMem, 0, 0, 32, 32, SRCCOPY);
dcMem.SelectObject(pOldBitmap);
}
- 效果图
|