int x,y; x=ImageRect.left+int((ImageRect.Width()-m_iImageWidth)/2.0); y=ImageRect.top+int((ImageRect.Height()-m_iImageWidth)/2.0); m_ImageList.Draw(pDC,item.m_iIndex,CPoint(x,y),ILD_FOCUS);
when u use it u can add member m_menu to a class then where u want pop u should add m_menu.TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);but first add m_Menu.SetBackImage(IDB_BMP_MENU,21,10); m_Menu.SetImage(IDB_EVERY,16,8); m_Menu.AppendCoolMenu(1000,"文本"); m_Menu.AppendCoolMenu(1001,"2"); m_Menu.AppendCoolMenu(MF_SEPARATOR,""); m_Menu.AppendCoolMenu(1002,"3"); m_Menu.AppendCoolMenu(1003,"4"); m_Menu.AppendCoolMenu(1004,"5"); m_Menu.AppendCoolMenu(1005,"6"); m_Menu.AppendCoolMenu(1006,"7"); m_Menu.EnableMenuItem(0,MF_BYPOSITION|MF_GRAYED); in menu construtor also should add menu id into resource file
i need it , please ctr+v it,thank you!!
#define AFX_COOLPOPUPMENU_H__50444B2C_1510_11D4_9E06_0050BABF8C7F__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#include "Afxtempl.h"
struct MenuItem
{
CString m_strMenuText;
int m_iIndex;
};class CCoolPopupMenu : public CMenu
{
public:
CCoolPopupMenu();
virtual ~CCoolPopupMenu();// Operations
public:
void SetImage(UINT nID,int cx,int row);
void AppendCoolMenu(UINT nID,CString text);
void SetBackImage(UINT nID,int cx,int row);public:
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpDrawItemStruct) ;
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );public:
void SetMenuWidth(int width) { m_iMenuWidth=width; }
int GetMenuWidth() { return m_iMenuWidth; } void SetMenuHeight(int height){ m_iMenuHeight=height; }
int GetMenuHeight() { return m_iMenuHeight;}private:
int m_iMenuWidth; //菜单宽度
int m_iMenuHeight; //菜单高度 int m_iBackImageWidth; //背景图象的宽度
int m_iBackImageHeight; //背景图象的高度 int m_iImageWidth; //图象的宽度
int m_iImageHeight; //图象的宽度
private:
CImageList m_BackImageList;
CImageList m_ImageList;
CArray<CString,CString>m_MenuText; //保存每一项菜单的数据
CMap<UINT,UINT,MenuItem,MenuItem> m_MapMenu; //保存菜单的数据
int m_iItemNum;
int m_iUpHeight;
};#endif // !defined(AFX_COOLPOPUPMENU_H__50444B2C_1510_11D4_9E06_0050BABF8C7F__INCLUDED_)
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "Menu.h"
#include "CoolPopupMenu.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define RGB_BUTTON_BLACK (GetSysColor(COLOR_WINDOWFRAME))
#define RGB_BUTTON_WHITE (GetSysColor(COLOR_BTNHIGHLIGHT))
#define RGB_BUTTON_LIGHT (GetSysColor(COLOR_BTNFACE))
#define RGB_BUTTON_DARK (GetSysColor(COLOR_BTNSHADOW))
#define RGB_MENU_FACE (GetSysColor(COLOR_MENU))
#define RGB_MENUTEXT_BACK (GetSysColor(COLOR_HIGHLIGHT))
#define RGB_MENU_TEXT (GetSysColor(COLOR_MENUTEXT))
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CCoolPopupMenu::CCoolPopupMenu()
{
m_iMenuWidth=100;
m_iMenuHeight=20;
m_iItemNum=0;
m_iUpHeight=0;
CreatePopupMenu ();
}CCoolPopupMenu::~CCoolPopupMenu()
{
DestroyMenu();
}void CCoolPopupMenu::SetBackImage(UINT nID,int cx,int row)
{
COLORREF cl=GetSysColor(COLOR_MENU);
m_BackImageList.Create(nID,m_iMenuHeight,row,cl);
m_iBackImageHeight=m_iMenuHeight; m_iMenuHeight=cx;
m_iBackImageWidth=21;
}void CCoolPopupMenu::SetImage(UINT nID, int cx, int row)
{
COLORREF cl=GetSysColor(COLOR_MENU);
m_ImageList.Create(nID,cx,row,cl);
m_iImageWidth=cx;
}void CCoolPopupMenu::AppendCoolMenu(UINT nID, CString text)
{
if(nID==MF_SEPARATOR)
AppendMenu(MF_SEPARATOR|MF_OWNERDRAW, nID,(LPCTSTR)text);
else
AppendMenu(MF_ENABLED | MF_OWNERDRAW, nID,(LPCTSTR)text);
m_MenuText.Add(text); MenuItem item;
item.m_strMenuText=text;
item.m_iIndex=m_iItemNum;
m_MapMenu.SetAt(nID,item); m_iItemNum+=1;
}void CCoolPopupMenu::MeasureItem(LPMEASUREITEMSTRUCT lpDrawItemStruct)
{
if(lpDrawItemStruct->itemID==MF_SEPARATOR)
lpDrawItemStruct->itemHeight = 10;
else
lpDrawItemStruct->itemHeight = m_iMenuHeight;
lpDrawItemStruct->itemWidth = m_iMenuWidth;
}void CCoolPopupMenu::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rc=lpDrawItemStruct->rcItem;
UINT ID=lpDrawItemStruct->itemID;
DWORD Data=lpDrawItemStruct->itemData; MenuItem item;
m_MapMenu.Lookup(ID,item);
if(ID==MF_SEPARATOR)
{
CRect rect;
rect.left=rc.left+m_iBackImageWidth;
rect.right=rc.right;
rect.top=rc.top+int((rc.bottom-rc.top)/2.0);
rect.bottom=rc.bottom;
pDC->DrawEdge(rect,EDGE_ETCHED,BF_TOP);
m_iUpHeight+=rc.Height()-rect.Height();
}
else
{
// rc.top-=m_iUpHeight;
// rc.bottom-=m_iUpHeight; pDC->SetBkMode(TRANSPARENT); CRect ImageRect,TextRect;
ImageRect=rc;
ImageRect.left+=m_iBackImageWidth+2;
ImageRect.right=ImageRect.left+20; TextRect=rc;
TextRect.left=ImageRect.right;
if((lpDrawItemStruct->itemState & ODS_SELECTED)&&
(lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
pDC->Draw3dRect(&ImageRect,RGB_BUTTON_WHITE,RGB_BUTTON_DARK);
pDC->Draw3dRect(&TextRect,RGB_BUTTON_DARK,RGB_BUTTON_WHITE);
}
if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & ODA_SELECT))
{
pDC->Draw3dRect(&ImageRect,RGB_MENU_FACE,RGB_MENU_FACE);
pDC->Draw3dRect(&TextRect,RGB_MENU_FACE,RGB_MENU_FACE);
}
int x,y;
x=ImageRect.left+int((ImageRect.Width()-m_iImageWidth)/2.0);
y=ImageRect.top+int((ImageRect.Height()-m_iImageWidth)/2.0);
m_ImageList.Draw(pDC,item.m_iIndex,CPoint(x,y),ILD_FOCUS);
TextRect.left+=5; //===============================================================
//
//
//简单地进行了处理,你可以根据你自己的需要再做其他的处理
//
//
//===============================================================
if(lpDrawItemStruct->itemState & ODS_GRAYED)
{
pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
pDC->DrawText(item.m_strMenuText,TextRect,DT_LEFT|DT_SINGLELINE|DT_VCENTER);
}
else
pDC->DrawText(item.m_strMenuText,TextRect,DT_LEFT|DT_SINGLELINE|DT_VCENTER);
} //绘制背景图
if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
m_BackImageList.Draw(pDC,item.m_iIndex,CPoint(rc.left,rc.top),ILD_NORMAL);}
u can add member m_menu to a class
then where u want pop u should add
m_menu.TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);but first add
m_Menu.SetBackImage(IDB_BMP_MENU,21,10);
m_Menu.SetImage(IDB_EVERY,16,8); m_Menu.AppendCoolMenu(1000,"文本");
m_Menu.AppendCoolMenu(1001,"2");
m_Menu.AppendCoolMenu(MF_SEPARATOR,"");
m_Menu.AppendCoolMenu(1002,"3");
m_Menu.AppendCoolMenu(1003,"4");
m_Menu.AppendCoolMenu(1004,"5");
m_Menu.AppendCoolMenu(1005,"6");
m_Menu.AppendCoolMenu(1006,"7");
m_Menu.EnableMenuItem(0,MF_BYPOSITION|MF_GRAYED);
in menu construtor
also should add menu id into resource file