读书人

关于菜单自己绘制的代码有关问题?

发布时间: 2012-01-16 23:36:51 作者: rapoo

关于菜单自己绘制的代码问题??
void CMenuEx::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
// check measure ownerdraw menu or other controler
if(lpMIS-> CtlType!=ODT_MENU)
return;

MENUITEM *lpMenuItem =(LPMENUITEM)lpMIS-> itemData;

// initialize width and height with itemSize
lpMIS-> itemWidth=lpMenuItem-> itemSize.cx+10;
lpMIS-> itemHeight=lpMenuItem-> itemSize.cy;


// not separator,may be popupmenu or normal menu item
if(lpMenuItem-> nID!=0)
基本上很多代码都定义MENUITEM 结构保存菜单基本信息,但是代码中都找不到给
lpMIS-> itemData;赋值,为什么
MENUITEM *lpMenuItem =(LPMENUITEM)lpMIS-> itemData;能从它中获得相关数据



[解决办法]
MeasureItem是一个处理通知消息的函数,其参数由调用者提供,也就是系统。
[解决办法]
具体你查一下MSDN中WM_MEASUREITEM消息和MEASUREITEMSTRUCT结构。
[解决办法]
那个代码我看过

1:MEASUREITEMSTRUCT 结构msdn注释
For a menu, this member contains the value that was passed to the menu by one of the following:
CMenu::AppendMenu
CMenu::InsertMenu
CMenu::ModifyMenu

意思是说MEASUREITEMSTRUCT 结构里的Itemdata来自于你ModifyMenu的LPCTSTR lpszNewItem 这个参数

2:再看ModifyMenu的msdn注释
ModifyMenu :
MF_OWNERDRAW Contains an application-supplied 32-bit value that the application can use to maintain additional data associated with the menu item. This 32-bit value is available to the application when it processes MF_MEASUREITEM and MF_DRAWITEM.

意思是当你的菜单有自绘风格的时候,你的LPCTSTR lpszNewItem 参数可以给你自定义的数据ItemData

3:再看看你的菜单类:
CMenu::ModifyMenu( nPosition,MF_BYPOSITION|MF_OWNERDRAW,nIDNewItem,(LPCTSTR)pMenuItem)
这里的pMenuItem就是你的 MENUITEM*呀,就是在这个时候把值给itemData的。
你修改菜单时正是给的MENUITEM*呀。

所以:
你可以 MENUITEM *lpMenuItem =(LPMENUITEM)lpMIS-> itemData;


[解决办法]
是在添加菜单项时赋值的,
AppendMenu(MF_OWNERDRAW, 0, (LPCTSTR)(&(ItemInfo)));

读书人网 >VC/MFC

热点推荐