www.pudn.com > fractal.rar > ColorButton.cpp


//贵州省安顺市第二高级中学 高二一班 蒋伟	 
//2002.7.1 
//请勿修改版权信息 谢谢 
//Neil 
// 
// ColorButton.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "ColorButton.h" 
 
#include "ColorPopup.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define MAX_ARROW_WIDTH 12 
 
///////////////////////////////////////////////////////////////////////////// 
// CColorButton 
 
CColorButton::CColorButton() 
{ 
	m_MouseOnButton=FALSE; 
	m_bArrowPress =FALSE; 
	m_bButtonPress = FALSE; 
	m_bTrackSelection = FALSE; 
	 
	m_clrCurrent = 0; 
 
} 
 
CColorButton::~CColorButton() 
{ 
} 
 
BEGIN_MESSAGE_MAP(CColorButton, CButton) 
	//{{AFX_MSG_MAP(CColorButton) 
	ON_WM_MOUSEMOVE() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(CPN_SELENDOK,     OnSelEndOK) 
  ON_MESSAGE(CPN_SELENDCANCEL, OnSelEndCancel) 
  ON_MESSAGE(CPN_SELCHANGE,    OnSelChange) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CColorButton message handlers 
 
void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDIS )  
{ 
	// TODO: Add your code to draw the specified item 
	CDC* pDC = CDC::FromHandle(lpDIS->hDC); 
	UINT state = lpDIS->itemState; 
	 
	// Set the Button Rect 
	CRect itemRect = lpDIS->rcItem; 
	CRect ButtonRect(itemRect); 
	CRect ArrowRect(itemRect); 
	 
	ButtonRect.InflateRect (0 , 0, - MAX_ARROW_WIDTH,0);   
	 
	m_rectButton = ButtonRect; 
	 
	ArrowRect.InflateRect ( -ButtonRect.Width() ,0,0,0); 
	 
	m_rectArrow = ArrowRect;  
	 
	BOOL bIsPressed  = (lpDIS->itemState & ODS_SELECTED); 
	BOOL bIsFocused  = (lpDIS->itemState & ODS_FOCUS); 
	BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED); 
 
	pDC->SetBkMode(TRANSPARENT); 
	  
	 
	// Draw pressed button 
	if (bIsPressed)	{ 
		if (m_bButtonPress){ 
			pDC->Draw3dRect(ButtonRect, ::GetSysColor(COLOR_BTNSHADOW),  
							::GetSysColor(COLOR_BTNHILIGHT)); 
		} 
		if (m_bArrowPress){ 
			pDC->Draw3dRect(ArrowRect, ::GetSysColor(COLOR_BTNSHADOW),  
							::GetSysColor(COLOR_BTNHILIGHT)); 
		} 
	} 
	else{ 
		if(m_MouseOnButton ){ 
				pDC->Draw3dRect(ButtonRect, ::GetSysColor(COLOR_BTNHILIGHT), 
								::GetSysColor(COLOR_BTNSHADOW)); 
				pDC->Draw3dRect(ArrowRect, ::GetSysColor(COLOR_BTNHILIGHT), 
								::GetSysColor(COLOR_BTNSHADOW)); 
			} 
	}    
	 
/*	// Draw the Demo Word  
	CRect rectWord = m_rectButton; 
	rectWord.InflateRect (-6,-6); 
	rectWord.OffsetRect (-1,-2); 
    pDC->DrawText("C", rectWord, DT_CENTER |DT_VCENTER | DT_SINGLELINE); 
	*/ 
	// Draw the Demo fillRect 
	CRect rectFillRect = m_rectButton; 
	pDC->Rectangle(m_rectButton); 
	rectFillRect.InflateRect (-1,-1); 
	pDC->FillSolidRect(rectFillRect, GetColor()); 
     
	// Draw the DownArrow  
	ArrowRect.InflateRect (-1,-2); 
	pDC->DrawFrameControl(&ArrowRect, DFC_SCROLL, DFCS_SCROLLDOWN  );  
	 
	// Erasure the frame of the little Arrow  
	pDC->Draw3dRect(&ArrowRect,::GetSysColor(COLOR_BTNFACE), 
					::GetSysColor(COLOR_BTNFACE)); 
	ArrowRect.InflateRect (-1,-1); 
	pDC->Draw3dRect(&ArrowRect,::GetSysColor(COLOR_BTNFACE), 
					::GetSysColor(COLOR_BTNFACE)); 
 
} 
 
void CColorButton::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return; 
	 
	if (GetCapture() != this){ 
		m_MouseOnButton = TRUE; 
		SetCapture(); 
		Invalidate(); 
	} 
	else{ 
		POINT p2 = point; 
		ClientToScreen(&p2); 
		CWnd* wndUnderMouse = WindowFromPoint(p2); 
		if (wndUnderMouse && wndUnderMouse->m_hWnd != this->m_hWnd){ 
			// Redraw only if mouse goes out 
			if (m_MouseOnButton == TRUE){ 
				m_MouseOnButton = FALSE; 
				Invalidate(); 
			} 
			// If user is NOT pressing left button then release capture! 
			if (!(nFlags & MK_LBUTTON))  
				ReleaseCapture(); 
		}	 
	} 
	 
	CButton::OnMouseMove(nFlags, point); 
} 
 
 
void CColorButton::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if (m_rectButton.PtInRect (point)) 
		m_bButtonPress = TRUE; 
	else 
		m_bButtonPress = FALSE; 
 
	if (m_rectArrow.PtInRect (point)) 
			m_bArrowPress = TRUE; 
		else  
			m_bArrowPress = FALSE; 
	 
	CButton::OnLButtonDown(nFlags, point); 
} 
 
void CColorButton::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
 
	m_bArrowPress = FALSE; 
	m_bButtonPress = FALSE; 
	 
	if (m_rectArrow.PtInRect (point)){ 
		CRect rect; 
		GetWindowRect(rect); 
		new CColorPopup(CPoint(rect.left, rect.bottom),   
			            GetColor(), this, _T("自动选择颜色"),_T("其他颜色 ...")); 
	}	 
	 
	CButton::OnLButtonUp(nFlags, point); 
} 
 
//////////////////////////////////////////////////// 
COLORREF CColorButton::GetColor() 
{ 
	return m_clrCurrent; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CColorButton message handlers 
 
LONG CColorButton::OnSelEndOK(UINT lParam, LONG /*wParam*/) 
{ 
    COLORREF crNewColor = (COLORREF) lParam; 
    m_MouseOnButton = FALSE; 
	SetColor(crNewColor); 
	 
//	CFontTestView *pView = (CFontTestView *)((CMainFrame*)AfxGetMainWnd())->GetActiveView(); 
	 
//	pView->SetEditFont (crNewColor); 
     
	return TRUE; 
} 
 
///////////////////////////////////////////////////////////////////// 
LONG CColorButton::OnSelEndCancel(UINT lParam, LONG /*wParam*/) 
{ 
    m_MouseOnButton = FALSE; 
     
	SetColor((COLORREF) lParam); 
	 
	return TRUE; 
} 
 
//////////////////////////////////////////////////////////////////// 
LONG CColorButton::OnSelChange(UINT lParam, LONG /*wParam*/) 
{ 
	m_MouseOnButton = FALSE; 
    if (m_bTrackSelection) 
	SetColor((COLORREF) lParam); 
	 
	return TRUE; 
} 
 
/////////////////////////////////////////////////////////////////////// 
void CColorButton::SetColor(COLORREF clrColor) 
{  
  m_clrCurrent = clrColor;  
	Invalidate(); 
}