Visual C++中自绘菜单的实现(9)
pSubMenu =pMenu->GetSubMenu(i);
if(pSubMenu)
{
nPosition =0;
//注意j一定要为int类型,如果为UINT是检查不出j>=0!
for(j=(int)pSubMenu->GetMenuItemCount()-1;j>=0;j--)
{
lpItem =new MENUITEM;
lpItem->uID =pSubMenu->GetMenuItemID(j);
lpItem->uPositionImageLeft =nPosition;
if(lpItem->uID>0)
{
nPosition +=m_szImage.cy+4;
pSubMenu->GetMenuString(j,lpItem->strText,MF_BYPOSITION);
//由工具栏位图中寻找菜单项的位图,如果没有则uIndex为-1
lpItem->uIndex =-1;
for(m=0; m<(pToolBar->GetToolBarCtrl().GetButtonCount()) ;m++)
{
int inx;
UINT idx,x;
pToolBar->GetButtonInfo(m,idx,x,inx);
if(idx==lpItem->uID)
{
lpItem->uIndex=inx;
break;
}
}
}
else
{
//间隔条;
nPosition +=m_nSeparator;
}
m_ListMenu.AddTail(lpItem);
//修改菜单绘制风格;
pSubMenu->ModifyMenu(j,MF_BYPOSITION|MF_OWNERDRAW,
lpItem->uID,LPCTSTR(lpItem));
pSubsub =pSubMenu->GetSubMenu(j);
if(pSubsub)
ChangeStyle(pSubsub,pToolBar);
}
}
}
m_bInitial =TRUE;
}
//////////////////////////////////////////////////////重载CMenu类的MeasureItem()函数;
void CMenuEx::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
MENUITEM *lpItem =(LPMENUITEM)lpMIS->itemData;
if(lpItem->uID==0)//分隔条的高度为10个像素;
{
lpMIS->itemHeight =m_nSeparator;
}
else
{
//填充lpMIS结构;
CDC *pDC =AfxGetMainWnd()->GetDC();
CString strText=lpItem->strText;
CSize size;
size=pDC->GetTextExtent(lpItem->strText);
lpMIS->itemWidth = size.cx +m_szImage.cx+4;
lpMIS->itemHeight =m_szImage.cy+4;
AfxGetMainWnd()->ReleaseDC(pDC);
}
}