Menus[m_index].m_MenuID = m;// m_pMenu->GetMenuItemID(i); //如果为弹出式菜单,返回-1,
//分隔条返回0,普通菜单,返回ID
if ( ((Menus[m_index].m_MenuID<0) &&IsTop== TRUE)) //顶层菜单
Menus[m_index].m_MenuID = -2;
Menus[m_index].m_Iconindex = -1;
m_pMenu->GetMenuString(i,Menus[m_index].m_Text,MF_BYPOSITION);//获取菜单项文本
m_pMenu->ModifyMenu(i,MF_OWNERDRAW|MF_BYPOSITION |MF_STRING,
Menus[m_index].m_MenuID,(LPCTSTR)(&Menus[m_index]));//修改菜单信息
if (Menus[m_index].m_MenuID>0)
{
Menus[m_index].m_Iconindex = m_imageindex;//设置菜单项图标索引
m_imageindex += 1;
}
m_index+=1;
CMenu* m_pSubmenu = m_pMenu->GetSubMenu(i);//获取子菜单
if (m_pSubmenu )//如果存在子菜单,利用递归修改其风格
{
ResetMenuStyle(m_pSubmenu->GetSafeHmenu());
}
}
}
}
(5)添加AttachMenu方法,该方法将菜单与指定的句柄关联,并调用ResetMenuStyle方法修改菜单风格。代码如下:
/***************************************
函 数 名: AttachMenu
作 用: 将菜单资源与菜单类关联,改变菜单风格
参 数: m_hmenu是菜单资源句柄
返 回 值: 执行成功返回值为true,否则出错
***************************************/
bool CMyMenu::AttachMenu(HMENU m_hmenu)
{
Attach(m_hmenu);
ResetMenuStyle(m_hmenu,true);
return true;
}
(6)改写菜单项的MeasureItem方法,设置顶层菜单、弹出示菜单、分隔条、普通菜单的大小。代码如下:
//根据菜单的风格确定菜单项的大小
void CMyMenu::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{
lpMeasureItemStruct->itemHeight = 26;
lpMeasureItemStruct->itemWidth = 130;
UINT m_type;
毕业论文http://www.youerw.com/ 论文网http://www.youerw.com/
break;
}
case -1: //弹出式菜单
{
lpMeasureItemStruct->itemWidth = ((CMenuItemInfo*)
(lpMeasureItemStruct->itemData))->m_Text.GetLength()*10;
break;
}
case 0: //分隔条
{
lpMeasureItemStruct->itemHeight = 10;
break;
}
default: //普通菜单项
{
break;
}
}
}
(7)改写菜单项的DrawItem方法,修改绘制菜单。在该方法中调用了多个自定义的方法,这些方法用户绘制菜单的图标、文本、背景颜色等,由于篇幅关系,具体代码请参考光盘。
//重新绘制菜单项
void CMyMenu::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
if (lpDrawItemStruct->CtlType = ODT_MENU)
{
if(lpDrawItemStruct->itemData == NULL) return;
CDC* pdc = CDC::FromHandle(lpDrawItemStruct->hDC);
pdc->SetBkMode(TRANSPARENT);
CRect m_rect (lpDrawItemStruct->rcItem);
CString m_text = ((CMenuItemInfo*)(lpDrawItemStruct->itemData))->m_Text;
UINT m_MenuID,m_State,m_imageid;
m_imageid = ((CMenuItemInfo*)(lpDrawItemStruct->itemData))->m_Iconindex;
m_State = lpDrawItemStruct->itemState;
m_MenuID = ((CMenuItemInfo*)(lpDrawItemStruct->itemData))->m_MenuID;
switch(m_MenuID)
{
case -2:
{
DrawTopMenu(pdc,m_rect,m_State&ODS_SELECTED);
DrawText(pdc,m_rect,m_text);
break;
}
case -1:
{
if (m_State&ODS_SELECTED)
DrawBestRect(pdc,m_rect,0xff00ff,0xfffff,false);
else
DrawTopMenu(pdc,m_rect,false);
DrawText(pdc,m_rect,m_text); break;
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] 下一页