www.pudn.com > 44757463.rar > LineMenu.cpp


// LineMenu.cpp: implementation of the CLineMenu class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "LineMenu.h" 
#include "GraphSoft.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CLineMenu::CLineMenu() 
{ 
   m_nSeparatorHeight = 10;	//default height of separator  
   m_nLineLength = 40;      //deafult length of line 
   m_colMenu =::GetSysColor(COLOR_MENU); 
   m_colText =::GetSysColor(COLOR_ACTIVECAPTION); 
   m_colTextSelected =::GetSysColor(COLOR_WINDOWTEXT); 
   m_bGrayed=FALSE; 
} 
/////////////////////////////////////////////////////// 
CLineMenu::~CLineMenu() 
{ 
   	while(!m_ListMenu.IsEmpty()) 
		delete m_ListMenu.RemoveHead(); 
	TRACE("DESTRUCTOR of LineMenu\n"); 
} 
/////////////////////////////////////////////////////// 
void CLineMenu::MeasureItem(LPMEASUREITEMSTRUCT  lpMIS) 
{ 
	TRACE("CLineMenu::MeasureItem\n"); 
	MENUITEM *lpItem =(LPMENUITEM)lpMIS->itemData;	 
	if(lpItem->uID==0)//separator 
	{ 
		lpMIS->itemHeight =m_nSeparatorHeight;		 
	} 
	else 
	{ 
		CDC	 *pDC =AfxGetMainWnd()->GetDC(); 
		 
		CString	strText=lpItem->strText; 
		CSize  size;		 
		size=pDC->GetTextExtent(lpItem->strText); 
 
       	lpMIS->itemWidth =size.cx+m_nLineLength+5;			 
		lpMIS->itemHeight =size.cy+8; 
 
		m_nHeight=size.cy+8; 
		AfxGetMainWnd()->ReleaseDC(pDC); 
	} 
} 
/////////////////////////////////////////////////////// 
void CLineMenu::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{ 
	TRACE("CLineMenu::DrawItem\n"); 
	CDC	dc; 
	LPMENUITEM lpItem; 
	CRect rect(lpDIS->rcItem); 
	dc.Attach(lpDIS->hDC); 
	lpItem =(LPMENUITEM)lpDIS->itemData; 
 
    bool bGrayed=FALSE; 
	CString strText=lpItem->strText; 
	int n=strText.GetLength(); 
	char chGrayed=strText.GetAt(n-1);	 
	strText=strText.Left(n-1); 
	if(chGrayed=='1'){ 
	 	bGrayed=TRUE;	 
	}else{ 
		bGrayed=FALSE; 
	} 
 
	if(lpDIS->itemState & ODS_SELECTED){ 
		dc.SetTextColor(m_colTextSelected);		 
	}else{ 
		dc.SetTextColor(m_colText); 
	} 
 
	//Set the Background color 
	CBrush brush(m_colMenu); 
	dc.FillRect(&rect, &brush); 
	 
	//Set the show mode 
	//dc.SetBkMode(TRANSPARENT); 
 
	if(lpItem->uID==0)//Separator 
	{ 
		rect.top =rect.Height()/2+rect.top ; 
		rect.bottom =rect.top +2; 
		rect.left +=2; 
		rect.right -=2; 
		dc.Draw3dRect(rect,RGB(64,0,128),RGB(255,255,255)); 
	} 
	else 
	{ 
		BOOL	bSelected =lpDIS->itemState & ODS_SELECTED; 
		BOOL	bChecked  =lpDIS->itemState & ODS_CHECKED; 
	//	BOOL	bGrayed	  =lpDIS->itemState & ODS_GRAYED;		 
	      
		//Draw the text of Menu item 
		CRect	rtText(rect.left+m_nLineLength+7,rect.top,rect.right ,rect.bottom ); 
		rtText.InflateRect(-2,-2);		 
		DrawText(&dc,rect,rtText,bSelected,bGrayed,strText);         
		//attention : you have Check that whether menu item is selected while drawing text 
        ///////////// so you need not check again while drawing line     
		 
		//Draw the line 
        CString str=lpItem->strText.Left(lpItem->strText.FindOneOf(_T(" "))); 
    	float fLineWidth=StrToFloat(str);  
		 
	    DrawLine(&dc,rect.left+1,(rect.top+rect.bottom)/2,rect.left+40,(rect.top+rect.bottom)/2,bSelected,bGrayed,fLineWidth);    	 
	} 
	dc.Detach(); 
} 
////////////////////////////////////////////////////////// 
//change the style of Menu  
void CLineMenu::ChangeStyle(CMenu* pMenu) 
{	 
	TRACE("CLineMenu::ChangeStyle\n"); 
	LPMENUITEM	lpItem; 
	CMenu		*pSubMenu; 
	int			nPosition=0;	//该变量用来绘制侧边位图 
    bool bGrayed; 
	for(int i=(int)GetMenuItemCount()-1 ;i>=0; i--) 
	{ 
		lpItem=new  MENUITEM(); 
		lpItem->uID=GetMenuItemID(i); 
		lpItem->uPositionImageLeft =nPosition; 
		 
		if(pMenu->GetMenuState(i,MF_BYPOSITION)&MF_GRAYED){		            
			bGrayed=TRUE; 
		}else{ 
			bGrayed=FALSE; 
		} 
		if(lpItem->uID >0) 
		{  
			nPosition +=m_nHeight; 
			//Save the Text of menu item 
    		pMenu->GetMenuString(i,lpItem->strText,MF_BYPOSITION); 
			if(bGrayed){//通过标题挂带灰色标志 
				lpItem->strText+="1"; 
			}else 
			{ 
				 lpItem->strText+="0"; 
			} 
			//if this item has sub item , call this function again 
			pSubMenu =pMenu->GetSubMenu(i);	 
			if(pSubMenu) 
				ChangeStyle(pSubMenu); 
		} 
		else 
		{		 
			//((CLineMenu*)pMenu)->m_bGrayed=m_bGrayed; 
			nPosition +=m_nSeparatorHeight; 
		} 
		//Change the mode of menu item 
		pMenu->ModifyMenu(i,MF_BYPOSITION|MF_OWNERDRAW|MF_DISABLED,lpItem->uID,(LPCTSTR)lpItem);			 
	  	m_ListMenu.AddTail(lpItem); 
	} 
	//SetTransparent(70); 
} 
////////////////////////////////////////////////////////// 
float CLineMenu::StrToFloat(CString str) 
/////////////////////////////////////////StrToFloat 
//Author: xgl 
//Date:   2004.3.17 
//str:    string that will be converted to float value 
// return value: 
// |____ if success,    float value of the str  
// |____ if unsuccess,  0 
///////////////////////////////////////// 
{ 
	char  chGet; 
	float fResult=0; 
	int   nPos; 
	float fMulty1=10,fMulty2=1,flag=1; 
	for(nPos=0;nPos0.09)			 
			{ 
				fResult=0; 
				break; 
			} 
			fMulty1=1; 
			fMulty2=0.1; 
			flag=0.1; 
			continue; 
		} 
		else if(chGet>='0'&&chGet<='9') 
		{ 
			fResult=fResult*fMulty1+(chGet-'0')*fMulty2; 
            fMulty2=fMulty2*flag; 
		} 
		else 
		{ 
            fResult=0; 
			break; 
		} 
	}	 
	return fResult; 
} 
 
///////////////////////////////////////////////////////////////// 
void CLineMenu::DrawLine(CDC *pDC, int xf,int yf,int xT,int yT, BOOL bSelected, BOOL bGrayed,int nWidth) 
{   
	TRACE("CLineMenu::DrawLine\n"); 
	//you can add other operation about drawing line here  
	CPen pen,*poldPen; 
	LOGBRUSH logbr; 
	logbr.lbStyle=BS_SOLID; 
	logbr.lbColor=RGB(0,0,0);	 
	if(bSelected){	 
		logbr.lbColor=RGB(127,127,255); 
	} 
	if(bGrayed){ 
	    logbr.lbColor=RGB(127,127,127); 
	}     
	 
 
	pen.CreatePen(PS_GEOMETRIC,nWidth, &logbr);	  
	poldPen=pDC->SelectObject(&pen);       
	pDC->MoveTo(xf,yf); 
	pDC->LineTo(xT,yT); 
	pDC->SelectObject(poldPen); 
} 
 
//////////////////////////////////////////////////// 
void CLineMenu::DrawText(CDC *pDC, CRect &rtMenu,CRect rtText,BOOL bSelected, BOOL bGrayed, CString strText) 
{ 
	TRACE("CLineMenu::DrawText\n"); 
	if(bSelected) 
		pDC->FillSolidRect(rtText,RGB(127,127,255)); 
	if(bGrayed) 
	{ 
		GrayString(pDC,strText,rtText); 
	} 
	else 
	{ 
		pDC->DrawText(strText,rtText,DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
	} 
} 
/////////////////////////////////////// 
void CLineMenu::GrayString(CDC *pDC, const CString &str, const CRect rect) 
{ 
	TRACE("CLineMenu::GrayString\n"); 
	CRect	rt(rect); 
 
	rt.left +=1; 
	rt.top +=1;	 
	pDC->SetTextColor(RGB(255,255,255)); 
	pDC->DrawText(str,&rt,DT_EXPANDTABS|DT_VCENTER|DT_SINGLELINE); 
	 
	rt.left -=1; 
	rt.top -=1; 
	pDC->SetTextColor(RGB(128,128,128)); 
	pDC->DrawText(str,&rt,DT_EXPANDTABS|DT_VCENTER|DT_SINGLELINE); 
	 
} 
void CLineMenu::SetTransparent(int i) 
{ 
//	if(i>100||i<0) 
//		return; 
//  if(i == 0) //如果为0,则设为100,即不透明 
//		i = 100; 
//	//获得窗口句柄	 
//	   HWND hWnd =G_; 
//	   //获取User32.DLL的句柄 
//	   HMODULE hModule =GetModuleHandle("User32.DLL");	    
//	   if(hModule == NULL) 
//	   { 
//		   //如果没有得到就退出 
//		   return; 
//	   } 
//	   //自定义函数指针变量类型 
//	   typedef BOOL (WINAPI* SETLAYEREDWND)( HWND, COLORREF, BYTE, DWORD);	    
//	   //定义函数指针 
//	   SETLAYEREDWND SetLayeredWindowPtr = NULL; 
//	   //从User32.DLL中获得函数指针 
//	   SetLayeredWindowPtr = (SETLAYEREDWND)GetProcAddress(hModule, "SetLayeredWindowAttributes"); 
//	   if(SetLayeredWindowPtr) 
//	   { 
//		   //获取原有窗口风格并加入WS_SYSMENU风格,因为只有此风格的窗口才能设置为透明 
//		   LONG lStyle = GetWindowLong(hWnd, GWL_EXSTYLE) | WS_SYSMENU; 
//		   //设置窗口风格 
//		   SetWindowLong( hWnd, GWL_EXSTYLE, lStyle);		    
//		   //设置窗口透明 
//		   SetLayeredWindowPtr( hWnd, 0, BYTE((255 * i) / 100), 2); 
//		   FreeLibrary(hModule); 
//	   }	    
}