www.pudn.com > CbuttonListBoxDemo.zip > LedButton.cpp


// LedButton.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "LedButton.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CLedButton 
 
CLedButton::CLedButton(CString ButtonText,CWnd* Parent) 
{ 
	m_pParent = Parent; 
	m_ButtonText = ButtonText; 
} 
 
CLedButton::~CLedButton() 
{ 
 
} 
 
 
BEGIN_MESSAGE_MAP(CLedButton, CButton) 
	//{{AFX_MSG_MAP(CLedButton) 
	ON_WM_CREATE() 
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLedButton message handlers 
 
BOOL CLedButton::CreateLED() 
{ 
	if( IsWindow( m_Led ) ) 
      return TRUE; 
 
	CentreLED(); 
 
	BOOL a_bResult = m_Led.Create(NULL,WS_CHILD | WS_VISIBLE, m_LEDLocation, this,1002); 
	m_Led.SetLed(CLed::LED_COLOR_YELLOW, CLed::LED_OFF, CLed::LED_ROUND); 
	 
	// Set up default BK, text and text colours 
	m_ButtonColour  = GetSysColor(COLOR_3DFACE); 
	m_TextColour	= GetSysColor(COLOR_BTNTEXT); 
 
 
	return a_bResult; 
} 
 
void CLedButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)  
{ 
   // If the LED is not yet created, create it once only 
	if( !IsWindow( m_Led ) ) 
	{ 
		CreateLED(); 
	} 
 
	CRect rt; 
	CDC dc; 
	CPen pen; 
	CFont font; 
	CPen* pOldpen = NULL; 
	CFont* pOldfont = NULL; 
 
	dc.Attach(lpDrawItemStruct->hDC);			//Get device context object 
 
	pen.CreatePen(PS_SOLID,3,RGB(50,50,50)); 
	font.CreatePointFont(110, "MS Sans Serif", &dc); 
	pOldpen = dc.SelectObject(&pen); 
	pOldfont = dc.SelectObject(&font); 
	 
	rt = lpDrawItemStruct->rcItem;				//Get button rect 
 
	dc.FillSolidRect(rt, m_ButtonColour);		//paint the Button the appropriate colour 
 
	UINT state = lpDrawItemStruct->itemState;	//Get state of the button 
	if ( (state & ODS_SELECTED) )				// If it is pressed 
	{ 
		dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT);    // Draw a sunken face 
	} 
	else 
	{ 
		dc.DrawEdge(rt,EDGE_RAISED,BF_RECT);	// Draw a raised face 
	} 
 
	dc.SetTextColor(m_TextColour);				// Set the color of the caption 
 
	rt.left = m_LEDLocation.right + 10; 
 
	// Redraw the LED in whatever state its in 
	m_Led.Invalidate(); 
 
	rt.DeflateRect(2,2,2,2); 
	dc.DrawText(m_ButtonText,rt,DT_LEFT|DT_VCENTER|DT_SINGLELINE);		// Draw out the caption 
	 
	if ( (state & ODS_FOCUS ) )					// If the button is focused 
	{ 
		// Draw a focus rect which indicates the user  
		// that the button is focused 
		int iChange = 3; 
		rt.top += iChange; 
		rt.left += iChange; 
		rt.right -= iChange; 
		rt.bottom -= iChange; 
	} 
 
	if(pOldpen) 
	{ 
		dc.SelectObject(pOldpen); 
	}//if 
	 
	if(pOldfont) 
	{ 
		dc.SelectObject(pOldfont); 
	}//if	 
	 
	 
	dc.Detach();	 
	 
} 
 
void CLedButton::CentreLED() 
{ 
	int LEDTop =0; 
	CRect ButtonRect; 
	GetWindowRect(ButtonRect); 
 
	// put the LED in the left of the button and half way up 
	LEDTop = (ButtonRect.Height() / 2) - (LED_SIZE / 2); 
	m_LEDLocation.SetRect(LEDLEFTOFFSET,LEDTop,LED_SIZE + LEDLEFTOFFSET,LEDTop + LED_SIZE); 
	 
} 
 
int CLedButton::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CButton::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// LED creation 
	CreateLED(); 
 
	return 0; 
} 
 
void CLedButton::OnClicked()  
{ 
	HWND ParentHwnd = m_pParent->m_hWnd; 
 
	::PostMessage(ParentHwnd,UM_BUTTON_CLICK,0,(DWORD)this); 
 
}