#include <afxwin.h>
#include <afxcmn.h>
#include <afxtempl.h>
#include <BitmapMenu.h>
BMenuData::BMenuData()
{
menuIconNormal = -1;
menuIconSelected = -1;
menuIconDisabled = -1;
nID = 0;
}
CBitmapMenu::CBitmapMenu(CImageList* pList)
{
SetImageList(pList);
m_clrText = GetSysColor(COLOR_MENUTEXT);
m_clrBack = GetSysColor(COLOR_MENU);
m_brBackground.CreateSolidBrush(m_clrBack);
m_penBack.CreatePen(PS_SOLID,0,m_clrBack);
m_bLBtnDown = FALSE;
m_hilightStyle = Normal;
m_clrHilight = GetSysColor(COLOR_HIGHLIGHT);
m_brSelect.CreateSolidBrush(m_clrHilight);
m_clrHilightText = GetSysColor(COLOR_HIGHLIGHTTEXT);
ZeroMemory((LPVOID)&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 = nm.iMenuHeight;
m_fontMenu.CreateFontIndirect(&m_lf);
}
CBitmapMenu::~CBitmapMenu()
{
DestroyMenu();
}
BOOL CBitmapMenu::DestroyMenu()
{
// Destroy Sub menus:
int m;
for( m = m_SubMenus.GetUpperBound(); m >= 0; m-- )
delete(m_SubMenus[m]);
m_SubMenus.RemoveAll();
// Destroy brushes:
if( (HBRUSH)m_brBackground != NULL )
m_brBackground.DeleteObject();
if( (HFONT)m_fontMenu != NULL )
m_fontMenu.DeleteObject();
if( (HBRUSH)m_brSelect != NULL )
m_brSelect.DeleteObject ();
// Destroy menu data
int numItems;
numItems = m_MenuList.GetUpperBound();
for( m = 0; m <= numItems; m++ )
delete(m_MenuList[m]);
m_MenuList.RemoveAll();
// Call base-class implementation last:
return(CMenu::DestroyMenu());
}
/*
=================================================================================
void CBitmapMenu::DrawItem(LPDRAWITEMSTRUCT)
---------------------------------------
Called by the framework when a particular item needs to be drawn. We overide
this to draw the menu item in a custom-fashion, including icons and the 3D
rectangle bar.
=================================================================================
*/
void CBitmapMenu::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
ASSERT(lpDIS != NULL);
CDC* pDC;
pDC = CDC::FromHandle(lpDIS->hDC);
CRect rect;
HICON hIcon;
COLORREF clrText;
clrText = m_clrText;
// draw the colored rectangle portion
rect.CopyRect(&lpDIS->rcItem);
// draw the up/down/focused/disabled state
UINT action;
UINT state;
action = lpDIS->itemAction;
state = lpDIS->itemState;
CString strText;
LOGFONT lf;
lf = m_lf;
CFont dispFont;
CFont *pFont;
if( lpDIS->itemData != NULL )
{
int nIconNormal = (((BMenuData*)(lpDIS->itemData))->menuIconNormal);
int nIconSelected = (((BMenuData*)(lpDIS->itemData))->menuIconSelected);
int nIconDisabled = (((BMenuData*)(lpDIS->itemData))->menuIconDisabled);
strText = (((BMenuData*)(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 = m_pList->ExtractIcon(nIconSelected);
}
else
{
if( state & ODS_GRAYED )
// Disabled (selected or not)
if( nIconDisabled != -1 )
hIcon = m_pList->ExtractIcon(nIconDisabled);
}
// If we didn't manage to select a specific icon, we'll use the
// default (normal) one...
if( hIcon == NULL )
hIcon = m_pList->ExtractIcon(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);
else
pDC->FillRect(rect,&m_brSelect);
pDC->SelectObject(pOldPen);
// 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);
clrText = m_clrHilightText;
}
else
{
CPen *pOldPen = pDC->SelectObject(&m_penBack);
pDC->FillRect (rect,&m_brBackground);
pDC->SelectObject (pOldPen);
// draw the up edges
pDC->Draw3dRect (rect,m_clrBack,m_clrBack);
if( (HFONT)dispFont != NULL )
dispFont.DeleteObject();
dispFont.CreateFontIndirect(&lf);
}
// 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 + (rect.Height() - m_nIconY) / 2, hIcon,
m_nIconX, m_nIconY, 0, NULL, DI_NORMAL);
rect.left = rect.left + m_nIconX + 2;
if( !strText.IsEmpty() )
{
int iOldMode = pDC->GetBkMode();
pDC->SetBkMode(TRANSPARENT);
// Draw the text in the correct colour:
UINT nFormat = DT_LEFT|DT_SINGLELINE|DT_EXPANDTABS|DT_VCENTER;
if( !(lpDIS->itemState & ODS_GRAYED) )
{
pDC->SetTextColor(clrText);
pDC->DrawText (strText,rect,nFormat);
}
else
{
// Draw the slightly lighter greyed text at an offset:
RECT offset = *rect;
offset.left += 1;
offset.right += 1;
offset.top += 1;
offset.bottom += 1;
pDC->SetTextColor(GetSysColor(COLOR_BTNHILIGHT));
pDC->DrawText(strText,&offset, nFormat);
// And the standard Grey text:
pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
pDC->DrawText(strText,rect, nFormat);
}
pFont = pDC->SelectObject(&dispFont);
pDC->SetBkMode(iOldMode);
pDC->SelectObject(pFont); //set it to the old font
}
dispFont.DeleteObject();
}
/*
=================================================================================
void CBitmapMenu::MeasureItem(LPMEASUREITEMSTRUCT)
---------------------------------------------
Called by the framework when it wants to know what the width and height of our
item will be. To accomplish this we provide the width of the icon plus the
width of the menu text, and then the height of the icon.
=================================================================================
*/
void CBitmapMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
// Obtain the width of the text:
CWnd *pWnd = AfxGetMainWnd(); // Get main window
CDC *pDC = pWnd->GetDC(); // Get device context
CFont* pFont = pDC->SelectObject(&m_fontMenu); // Select menu font in...
LPCTSTR lpstrText = (((BMenuData*)(lpMIS->itemData))->menuText); // Get pointer to text
SIZE t;
GetTextExtentPoint32(pDC->GetSafeHdc(), lpstrText, strlen(lpstrText), &t); // Width of caption
pDC->SelectObject(pFont); // Select old font in
pWnd->ReleaseDC(pDC); // Release the DC
lpMIS->itemWidth = t.cx;
lpMIS->itemHeight = max(GetSystemMetrics(SM_CYMENU), t.cy);
lpMIS->itemWidth += m_nIconX;
lpMIS->itemHeight = max(m_nIconY, lpMIS->itemHeight);
}
void CBitmapMenu::SetTextColor(COLORREF clrText)
{
m_clrText = clrText;
}
void CBitmapMenu::SetBackColor(COLORREF clrBack)
{
m_clrBack = clrBack;
if( (HBRUSH)m_brBackground != NULL )
m_brBackground.DeleteObject();
m_brBackground.CreateSolidBrush (clrBack);
}
void CBitmapMenu::SetHighlightColor(COLORREF clrHilight)
{
m_clrHilight = clrHilight;
if( (HBRUSH)m_brSelect != NULL )
m_brSelect.DeleteObject();
m_brSelect.CreateSolidBrush(clrHilight);
}
void CBitmapMenu::SetHighlightTextColor(COLORREF clrHilightText)
{
m_clrHilightText = clrHilightText;
}
void CBitmapMenu::SetHighlightStyle(HIGHLIGHTSTYLE hilightStyle)
{
m_hilightStyle = hilightStyle;
}
void CBitmapMenu::SetImageList(CImageList* pList)
{
m_pList = pList;
if( m_pList != NULL )
{
IMAGEINFO info;;
m_pList->GetImageInfo(0, &info);
m_nIconX = in
在菜单中显示位图的源码,一起来研究一下
需积分: 9 56 浏览量
2008-11-17
22:30:40
上传
评论
收藏 5KB ZIP 举报
badbombaby
- 粉丝: 10
- 资源: 17
最新资源
- date0425111111111111111111111
- 包含贪心算法的定义及python代码部分实现
- 自动驾驶-状态估计和定位之扩展卡尔曼滤波.pdf
- csdn之x-ca-key,x-ca-nonce,x-ca-signature与x-ca-signature-headers探索
- 基于TM1620数码显示芯片STM32单片机驱动程序软件源代码.zip
- 【tomcat6使用redis配置session共享】
- 包含杨辉三角的说明及java代码实现
- FDN371N-NL-VB一款SOT23封装N-Channel场效应MOS管
- AutomotiveSPICE-V4.0 中文版
- Java实现杨辉三角的生成和打印代码示例.md
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈