www.pudn.com > VC生成漂亮界面.zip > StyleButton.cpp


// StyleButton.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "VCFace.h" 
#include "StyleButton.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CStyleButton 
 
CStyleButton::CStyleButton() 
{ 
	m_bCursorOn=false; 
	m_hIcon_Active=0; 
	m_hIcon_Enable=0; 
	m_IconSize=CSize(0,0); 
	m_nTColor_Active=RGB(0,0,255); 
	m_nTColor_Enable=RGB(0,0,0); 
	m_nBColor_Active=GetSysColor(COLOR_3DFACE)+RGB(10,10,20); 
	m_nBColor_Enable=GetSysColor(COLOR_3DFACE); 
} 
 
CStyleButton::~CStyleButton() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CStyleButton, CButton) 
	//{{AFX_MSG_MAP(CStyleButton) 
	ON_WM_SETCURSOR() 
	ON_WM_MOUSEMOVE() 
	ON_WM_CAPTURECHANGED() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CStyleButton message handlers 
 
BOOL CStyleButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(m_hCursor) 
	{ 
		::SetCursor(m_hCursor); 
		return true; 
	} 
	return CButton::OnSetCursor(pWnd, nHitTest, message); 
} 
 
void CStyleButton::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(m_bCursorOn) 
	{ 
		CRect rect; 
		GetWindowRect(&rect); 
		rect.right-=rect.left; 
		rect.bottom-=rect.top; 
		rect.left=0; 
		rect.top=0; 
		if(!rect.PtInRect(point)) 
		{ 
			m_bCursorOn=false; 
			::ReleaseCapture(); 
			this->Invalidate(); 
		} 
	} 
	else 
	{ 
		m_bCursorOn=true; 
		this->SetCapture(); 
		this->Invalidate(); 
	} 
	CButton::OnMouseMove(nFlags, point); 
} 
 
void CStyleButton::OnCaptureChanged(CWnd *pWnd)  
{ 
	// TODO: Add your message handler code here 
	m_bCursorOn=false; 
	::ReleaseCapture(); 
	this->Invalidate(); 
	CButton::OnCaptureChanged(pWnd); 
} 
 
void CStyleButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)  
{ 
	// TODO: Add your code to draw the specified item 
	CString Title; 
	GetWindowText(Title); 
 
	CRect rect(lpDrawItemStruct->rcItem); 
 
	CDC DC; 
	DC.Attach(lpDrawItemStruct->hDC); 
	DC.SetBkMode(TRANSPARENT); 
 
	CPoint pt; 
	if(Title.IsEmpty()) 
	{ 
		pt.x=(rect.right-rect.left-m_IconSize.cx)/2; 
	} 
	else 
	{ 
		pt.x=m_IconSize.cx/2; 
	} 
	pt.y=(rect.top+rect.bottom-m_IconSize.cy)/2; 
	 
	if(lpDrawItemStruct->itemState&ODS_DISABLED) 
	{	//如果是Disable状态 
		DC.DrawState(pt,m_IconSize,m_hIcon_Enable,DSS_DISABLED,(CBrush*)NULL); 
		rect.left=m_IconSize.cx*3/2; 
		rect.left+=2; 
		rect.top+=2; 
		DC.SetTextColor(GetSysColor(COLOR_3DHILIGHT)); 
		DC.DrawText(Title,&rect,DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
		rect.left-=2; 
		rect.top-=2; 
		DC.SetTextColor(GetSysColor(COLOR_3DSHADOW)); 
		DC.DrawText(Title,&rect,DT_CENTER | DT_VCENTER | DT_SINGLELINE);		 
	} 
	else if(m_bCursorOn) 
	{//如果鼠标在按纽上 
		CBrush Brush; 
		Brush.CreateSolidBrush(m_nBColor_Active); 
		DC.FillRect(&rect,&Brush);		 
		if(lpDrawItemStruct->itemState&ODS_SELECTED) 
		{//且按下按纽 
			rect.left+=2; 
			rect.top+=2; 
			pt.x+=2; 
			pt.y+=2; 
			DC.Draw3dRect(rect,GetSysColor(COLOR_3DSHADOW),GetSysColor(COLOR_3DHILIGHT)); 
			DC.DrawState(pt,m_IconSize,m_hIcon_Active,DSS_NORMAL,(CBrush*)NULL); 
			rect.left=m_IconSize.cx*3/2+2; 
		} 
		else 
		{//且没有按下 
			DC.Draw3dRect(rect,GetSysColor(COLOR_3DHILIGHT),GetSysColor(COLOR_3DSHADOW)); 
			DC.DrawState(pt,m_IconSize,m_hIcon_Active,DSS_NORMAL,(CBrush*)NULL); 
			rect.left=m_IconSize.cx*3/2; 
		}						 
		DC.SetTextColor(m_nTColor_Active); 
		DC.DrawText(Title,&rect,DT_CENTER | DT_VCENTER | DT_SINGLELINE);		 
	} 
	else 
	{//如果鼠标不在按纽上 
		DC.DrawState(pt,m_IconSize,m_hIcon_Enable,DSS_NORMAL,(CBrush*)NULL); 
		rect.left=m_IconSize.cx*3/2; 
		DC.SetTextColor(m_nTColor_Enable); 
		DC.DrawText(Title,&rect,DT_CENTER | DT_VCENTER | DT_SINGLELINE); 
	} 
}