自绘制带图标的菜单项

None.gif const int MAX_MENUCOUNT = 60 ,ITEMHEIGHT = 26 ,ITEMWIDTH = 120 ; // 菜单项数量
None.gif

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*************************************
InBlock.gifCMenuItemInfo结构用于记录菜单项信息
ExpandedBlockEnd.gif************************************
*/

None.gif
struct CMenuItemInfo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifCStringm_ItemText;
//菜单项文本
InBlock.gif
intm_IconIndex;//菜单项索引
InBlock.gif
intm_ItemID;//菜单标记-2顶层菜单,-1弹出式菜单,0分隔条,其他普通菜单
ExpandedBlockEnd.gif
}
;
None.gif
None.gif
None.gif
class CIconMenu: public CMenu
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gifCMenuItemInfom_ItemLists[MAX_MENUCOUNT];
//菜单项信息
InBlock.gif
intm_index;//临时索引
InBlock.gif
intm_iconindex;
InBlock.gifCImageListm_imagelist;
//存储菜单项图标
InBlock.gif

InBlock.gifCIconMenu();
InBlock.gifBOOLChangeMenuItem(CMenu
*m_menu,BOOLm_Toped=FALSE);
InBlock.gifBOOLAttatchMenu(HMENUm_hmenu);
InBlock.gif
voidDrawItemText(CDC*m_pdc,LPSTRstr,CRectm_rect);
InBlock.gif
voidDrawTopMenu(CDC*m_pdc,CRectm_rect,BOOLm_selected=FALSE);//绘制顶层菜单
InBlock.gif
voidDrawSeparater(CDC*m_pdc,CRectm_rect);//绘制分隔条
InBlock.gif
voidDrawComMenu(CDC*m_pdc,CRectm_rect,COLORREFm_fromcolor,COLORREFm_tocolor,BOOLm_selected=FALSE);
InBlock.gif
voidDrawMenuIcon(CDC*m_pdc,CRectm_rect,intm_icon);
InBlock.gif
InBlock.gif
//overridememu'sviutualmethod
InBlock.gif
virtual~CIconMenu();
InBlock.gif
virtualvoidMeasureItem(LPMEASUREITEMSTRUCTlpStruct);//设置菜单项大小
InBlock.gif
virtualvoidDrawItem(LPDRAWITEMSTRUCTlpStruct);//重绘菜单项
InBlock.gif

ExpandedBlockEnd.gif}
;
None.gif

None.gif
None.gifCIconMenu::CIconMenu()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifm_index
=0;
InBlock.gifm_iconindex
=0;
InBlock.gif
//创建图像列表
InBlock.gif
m_imagelist.Create(16,16,ILC_COLOR24|ILC_MASK,0,0);
InBlock.gif
InBlock.gif
//添加图标
InBlock.gif
m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON2));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON3));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON4));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON5));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON6));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON7));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON8));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON9));
InBlock.gifm_imagelist.Add(AfxGetApp()
->LoadIcon(IDI_ICON10));
ExpandedBlockEnd.gif}

None.gif
None.gifCIconMenu::
~ CIconMenu()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifm_imagelist.Detach();
ExpandedBlockEnd.gif}

None.gif
None.gifBOOLCIconMenu::AttatchMenu(HMENUm_hmenu)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
this->Attach(m_hmenu);
InBlock.gif
returnTRUE;
ExpandedBlockEnd.gif}

None.gif
None.gifBOOLCIconMenu::ChangeMenuItem(CMenu
* m_menu,BOOLm_Toped)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(m_menu!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
intm_itemcount=m_menu->GetMenuItemCount();
InBlock.gif
for(inti=0;i<m_itemcount;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_menu
->GetMenuString(i,m_ItemLists[m_index].m_ItemText,MF_BYPOSITION);
InBlock.gif
intm_itemID=m_menu->GetMenuItemID(i);
InBlock.gif
if(m_itemID==-1&&m_Toped)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_itemID
=-2;//顶层菜单
ExpandedSubBlockEnd.gif
}
;
InBlock.gifm_ItemLists[m_index].m_ItemID
=m_itemID;
InBlock.gif
if(m_itemID>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_ItemLists[m_index].m_IconIndex
=m_iconindex;
InBlock.gifm_iconindex
+=1;
ExpandedSubBlockEnd.gif}

InBlock.gifm_menu
->ModifyMenu(i,MF_OWNERDRAW|MF_BYPOSITION|MF_STRING,m_ItemLists[m_index].m_ItemID,(LPSTR)&(m_ItemLists[m_index]));
InBlock.gif
InBlock.gifm_index
+=1;
InBlock.gifCMenu
*m_subMenu=m_menu->GetSubMenu(i);
InBlock.gif
InBlock.gif
if(m_subMenu)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifChangeMenuItem(m_subMenu);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
returnTRUE;
ExpandedBlockEnd.gif}

None.gif
None.gif
void CIconMenu::MeasureItem(LPMEASUREITEMSTRUCTlpStruct)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(lpStruct->CtlType==ODT_MENU)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giflpStruct
->itemHeight=ITEMHEIGHT;
InBlock.giflpStruct
->itemWidth=ITEMWIDTH;
InBlock.gifCMenuItemInfo
*m_iteminfo;
InBlock.gifm_iteminfo
=(CMenuItemInfo*)lpStruct->itemData;
InBlock.giflpStruct
->itemWidth=((CMenuItemInfo*)lpStruct->itemData)->m_ItemText.GetLength()*10;
InBlock.gif
switch(m_iteminfo->m_ItemID)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case0://分隔条
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giflpStruct
->itemHeight=1;
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
void CIconMenu::DrawItem(LPDRAWITEMSTRUCTlpStruct)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(lpStruct->CtlType==ODT_MENU)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(lpStruct->itemData==NULL)return;
InBlock.gifunsigned
intm_state=lpStruct->itemState;
InBlock.gifCDC
*m_dc=CDC::FromHandle(lpStruct->hDC);
InBlock.gif
//m_dc.Attach(lpStruct->hDC);
InBlock.gif

InBlock.gifCStringstr
=((CMenuItemInfo*)(lpStruct->itemData))->m_ItemText;
InBlock.gifLPSTRm_str
=str.GetBuffer(str.GetLength());
InBlock.gif
InBlock.gif
intm_itemID=((CMenuItemInfo*)(lpStruct->itemData))->m_ItemID;
InBlock.gif
intm_itemicon=((CMenuItemInfo*)(lpStruct->itemData))->m_IconIndex;
InBlock.gifCRectm_rect
=lpStruct->rcItem;
InBlock.gif
InBlock.gifm_dc
->SetBkMode(TRANSPARENT);
InBlock.gif
InBlock.gif
switch(m_itemID)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case-2:
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifDrawTopMenu(m_dc,m_rect,(m_state
&ODS_SELECTED)||(m_state&0x0040));//0x0040==ODS_HOTLIGHT
InBlock.gif

InBlock.gifDrawItemText(m_dc,m_str,m_rect);
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
case-1:
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDrawItemText(m_dc,m_str,m_rect);
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
case0:
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDrawSeparater(m_dc,m_rect);
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
default:
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDrawComMenu(m_dc,m_rect,
0xfaa0,0xf00ff,m_state&ODS_SELECTED);
InBlock.gifDrawItemText(m_dc,m_str,m_rect);
InBlock.gifDrawMenuIcon(m_dc,m_rect,m_itemicon);
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*************************************************************
InBlock.gif功能描述:绘制菜单项文本
InBlock.gif
InBlock.gif参数说明:m_pdc标识画布对象,str标识菜单文本,m_rect标识菜单区域
ExpandedBlockEnd.gif************************************************************
*/

None.gif
void CIconMenu::DrawItemText(CDC * m_pdc,LPSTRstr,CRectm_rect)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifm_rect.DeflateRect(
20,0);
InBlock.gifm_pdc
->DrawText(str,m_rect,DT_SINGLELINE|DT_VCENTER|DT_LEFT);
ExpandedBlockEnd.gif}

None.gif
None.gif
void CIconMenu::DrawTopMenu(CDC * m_pdc,CRectm_rect,BOOLm_selected)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(m_selected)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_pdc
->SelectStockObject(BLACK_PEN);
InBlock.gifm_pdc
->Rectangle(&m_rect);
InBlock.gifm_rect.DeflateRect(
1,1);
InBlock.gifm_pdc
->FillSolidRect(m_rect,RGB(150,185,255));
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifCRectrect;
InBlock.gifAfxGetMainWnd()
->GetClientRect(rect);
InBlock.gifrect.top
=m_rect.top;
InBlock.gifrect.bottom
=m_rect.bottom;
InBlock.gifrect.left
=360;
InBlock.gifrect.right
+=4;
InBlock.gif
//CRectc_rect(m_rect);
InBlock.gif
m_pdc->FillSolidRect(&rect,RGB(200,187,255));
InBlock.gifm_pdc
->FillSolidRect(&m_rect,RGB(200,187,255));
InBlock.gif
//m_pdc->SelectStockObject(BLACK_PEN);
InBlock.gif
//m_pdc->FillSolidRect(m_rect,RGB(100,185,255));
ExpandedSubBlockEnd.gif
}

ExpandedBlockEnd.gif}

None.gif
None.gif
void CIconMenu::DrawSeparater(CDC * m_pdc,CRectm_rect)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(m_pdc!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_pdc
->Draw3dRect(m_rect,RGB(255,0,0),RGB(0,0,255));
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
void CIconMenu::DrawComMenu(CDC * m_pdc,CRectm_rect,COLORREFm_fromcolor,COLORREFm_tocolor,BOOLm_selected)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(m_selected)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifm_pdc
->Rectangle(m_rect);
InBlock.gifm_rect.DeflateRect(
1,1);
InBlock.gif
intr1,g1,b1;
InBlock.gif
//读取渐变起点的颜色值
InBlock.gif
r1=GetRValue(m_fromcolor);
InBlock.gifg1
=GetGValue(m_fromcolor);
InBlock.gifb1
=GetBValue(m_fromcolor);
InBlock.gif
intr2,g2,b2;
InBlock.gif
InBlock.gif
InBlock.gif
//读取渐变终点的颜色值
InBlock.gif
r2=GetRValue(m_tocolor);
InBlock.gifg2
=GetGValue(m_tocolor);
InBlock.gifb2
=GetBValue(m_tocolor);
InBlock.gif
InBlock.gif
floatr3,g3,b3;//菜单区域水平方向每个点RGB值应该变化的度(范围)
InBlock.gif

InBlock.gifr3
=((float)(r2-r1))/(float)(m_rect.Height());
InBlock.gifg3
=(float)(g2-g1)/(float)(m_rect.Height());
InBlock.gifb3
=(float)(b2-b1)/(float)(m_rect.Height());
InBlock.gif
InBlock.gifCOLORREFr,g,b;
//菜单区域水平方向每个点的颜色值
InBlock.gif
CPen*m_oldpen;
InBlock.gif
InBlock.gif
for(inti=m_rect.top;i<m_rect.bottom;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifr
=r1+(int)r3*(i-m_rect.top);
InBlock.gifg
=g1+(int)g3*(i-m_rect.top);
InBlock.gifb
=b1+(int)b3*(i-m_rect.top);
InBlock.gif
InBlock.gifCPenm_pen(PS_SOLID,
1,RGB(r,g,b));
InBlock.gifm_oldpen
=m_pdc->SelectObject(&m_pen);
InBlock.gifm_pdc
->MoveTo(m_rect.left,i);
InBlock.gifm_pdc
->LineTo(m_rect.right,i);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifm_pdc
->SelectObject(m_oldpen);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_pdc
->FillSolidRect(m_rect,RGB(0x000000F9,0x000000F8,0x000000F7));
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
void CIconMenu::DrawMenuIcon(CDC * m_pdc,CRectm_rect, int m_icon)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifm_imagelist.Draw(m_pdc,m_icon,CPoint(m_rect.left
+2,m_rect.top+4),ILD_TRANSPARENT);
ExpandedBlockEnd.gif}

None.gif
None.gif

2008031901.jpg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值