⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mymenu.cpp

📁 一个Visual C++的自己绘制菜单的控件
💻 CPP
字号:
// MyMenu.cpp: implementation of the CMyMenu class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "OwnerdrawMenu.h"
#include "MyMenu.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyMenu::CMyMenu()
{

}

CMyMenu::~CMyMenu()
{

}
void CMyMenu::ChangeToOwnerDraw(CMyMenu *pMyMenu)
{
	CString str;	//use to hold the caption temporarily
	CMyMenu* pMenu;	//use to hold the sub menu
	CMyMenuData* pMenuData;	//use to hold the menu data
	//get the number of the menu items of the parent menu
	int iMenuCount = pMyMenu->GetMenuItemCount();
	UINT nID;	//use to hold the identifier of the menu items
	for (int i=0; i<iMenuCount; i++)
	{
		//get the caption of the menu item
   		pMyMenu->GetMenuString(i, str, MF_BYPOSITION);
		pMenu = 0;	//reset pointer for safety
		pMenuData = 0; //reset pointer for safety
		pMenuData = new CMyMenuData;
		pMenuData->m_strCaption = str;
		rgpMyMenuData.push_back(pMenuData);
		
		if (pMyMenu->GetSubMenu(i))	//if the parent menu has sub menu
		{
			pMyMenu->ModifyMenu(i, MF_BYPOSITION | MF_OWNERDRAW, 0, (LPCTSTR)pMenuData);
			pMenu = new CMyMenu;
			rgpMyMenu.push_back(pMenu);
			HMENU hMenu = pMyMenu->GetSubMenu(i)->GetSafeHmenu();
			pMenu->Attach(hMenu);
			ChangeToOwnerDraw(pMenu);
		}
		else
		{
			nID = pMyMenu->GetMenuItemID(i);
			pMyMenu->ModifyMenu(i, MF_BYPOSITION | MF_OWNERDRAW, (UINT)nID, (LPCTSTR)pMenuData);
		}
	}
}


void CMyMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	CMyMenuData* pMyMenuData = (CMyMenuData*)lpMeasureItemStruct->itemData;
	//get the caption of the menu item
	CString str = pMyMenuData->m_strCaption;
	//assign the height of the menu item
	lpMeasureItemStruct->itemHeight = 23;
	//assign the width of the menu item
	lpMeasureItemStruct->itemWidth = str.GetLength()*7;	
}

void CMyMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	//draw an orange background
	pDC->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,150,0));
	//if the menu item is selected
	if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
		(lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
	{
		//draw a blue background
		pDC->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0,150,255));
	}
	
	CMyMenuData* pMyMenuData = (CMyMenuData*)lpDrawItemStruct->itemData;

	CString str = pMyMenuData->m_strCaption;
	//draw the caption of the menu item
	pDC->TextOut(lpDrawItemStruct->rcItem.left,lpDrawItemStruct->rcItem.top, str);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -