// MenuIcon.cpp: implementation of the CMenuIcon class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IconMenu.h"
#include "MenuIcon.h"

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

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

CMenuIcon::CMenuIcon()
{
	m_Index     = 0;
	m_IconIndex = 0;
	//创建图像列表
	m_ImageList.Create(16,16,ILC_COLOR24|ILC_MASK,0,0);
	//添加图标
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON4));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON5));
	m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON6));
		m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON7));
		

}

CMenuIcon::~CMenuIcon()
{
	m_ImageList.Detach();
}

BOOL CMenuIcon::AttatchMenu(UINT m_uID)
{
	LoadMenu(m_uID);
	return TRUE;
}

BOOL CMenuIcon::MenuItem(CMenu *pMenu)
{
	if(pMenu != NULL)
	{
		int m_Count = pMenu->GetMenuItemCount();
		for(int i=0;i<m_Count;i++)
		{
			pMenu->GetMenuString(i,m_ItemLists[m_Index].m_ItemText,MF_BYPOSITION);
			int m_itemID = pMenu->GetMenuItemID(i);
			m_ItemLists[m_Index].m_ItemID = m_itemID;
			if(m_itemID>0)
			{
				m_ItemLists[m_Index].m_IconIndex = m_IconIndex;
				m_IconIndex += 1;
			}
			pMenu->ModifyMenu(i,MF_OWNERDRAW|MF_BYPOSITION |MF_STRING,
				m_ItemLists[m_Index].m_ItemID,(LPSTR)&(m_ItemLists[m_Index]));
			m_Index += 1;
			CMenu* m_SubMenu = pMenu->GetSubMenu(i);
			if(m_SubMenu)
			{
				MenuItem(m_SubMenu);
			}
		}
	}
	return TRUE	;
}

void CMenuIcon::MeasureItem( LPMEASUREITEMSTRUCT lpStruct )
{
	if(lpStruct->CtlType == ODT_MENU)
	{
		lpStruct->itemHeight = ITEMHEIGHT;
		lpStruct->itemWidth  = ITEMWIDTH;
		CMenuItem* m_item;
		m_item = (CMenuItem*)lpStruct->itemData;
		lpStruct->itemWidth = ((CMenuItem*)lpStruct->itemData)->m_ItemText.GetLength()*10;
		if(m_item->m_ItemID == 0)	//分隔条
		{
			lpStruct->itemHeight = 2;
		}
	}
}

void CMenuIcon::DrawItemText(CDC *pDC, LPSTR Str, CRect Rect)
{
	pDC->DrawText(Str,Rect,DT_CENTER | DT_VCENTER | DT_SINGLELINE );
}

void CMenuIcon::DrawItemIcon(CDC *pDC, CRect Rect, int Icon)
{
	m_ImageList.Draw(pDC,Icon,CPoint(Rect.left+2,Rect.top+4),ILD_TRANSPARENT);
}

void CMenuIcon::DrawSeparater(CDC *pDC, CRect Rect)
{
	pDC->Draw3dRect(Rect,RGB(255,0,255),RGB(255,0,255));
}

void CMenuIcon::DrawTopMenu(CDC *pDC, CRect Rect, BOOL Selected)
{
	if(Selected)
	{
		pDC->Rectangle(&Rect);
		Rect.DeflateRect(1,1);
		pDC->FillSolidRect(Rect,RGB(190,170,220));
	}
	else
	{
		pDC->FillSolidRect(&Rect,RGB(192,192,192));
	}
}

void CMenuIcon::DrawComMenu(CDC *pDC, CRect Rect, BOOL Selected)
{
	if(Selected)
	{
		pDC->Rectangle(Rect);	 
		Rect.DeflateRect(1,1);
		pDC->FillSolidRect(Rect,RGB(255,0,0));
	}
	else
	{
		pDC->FillSolidRect(Rect,RGB(255, 255, 255));
	}
}

void CMenuIcon::DrawItem( LPDRAWITEMSTRUCT lpStruct )
{
	if (lpStruct->CtlType==ODT_MENU)
	{
		if(lpStruct->itemData == NULL)return;
		unsigned int m_state = lpStruct->itemState;
		CDC*    pDC          = CDC::FromHandle(lpStruct->hDC);
		CString str          = ((CMenuItem*)(lpStruct->itemData))->m_ItemText;
		LPSTR   m_str        = str.GetBuffer(str.GetLength());
		int     m_itemID     = ((CMenuItem*)(lpStruct->itemData))->m_ItemID;
		int     m_itemicon   = ((CMenuItem*)(lpStruct->itemData))->m_IconIndex;
		CRect   m_rect       = lpStruct->rcItem;
		pDC->SetBkMode(TRANSPARENT);
		switch(m_itemID)
		{
		case -1:
			{

				DrawTopMenu(pDC,m_rect,(m_state&ODS_SELECTED)||(m_state&0x0040)); //0x0040 ==ODS_HOTLIGHT
				DrawItemText(pDC,m_str,m_rect);
				break;
			}
		case 0:
			{
				DrawSeparater(pDC,m_rect);
				break;
			}
		default:
			{		
				DrawComMenu(pDC,m_rect,m_state&ODS_SELECTED);
				DrawItemText(pDC,m_str,m_rect);
				DrawItemIcon(pDC,m_rect,m_itemicon);
				break;
			}
		}
	}
}
