www.pudn.com > CbuttonListBoxDemo.zip > ButtonListBox.cpp
// ButtonListBox.cpp : implementation file
//
#include "stdafx.h"
#include "ButtonListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonListBox
CButtonListBox::CButtonListBox()
{
}
CButtonListBox::~CButtonListBox()
{
for(int i=0; i<= m_ButtonArray.GetUpperBound();i ++)
delete m_ButtonArray.ElementAt(i);
}
void CButtonListBox::AddItem(LPCTSTR ButtonText)
{
CLedButton* pAddedButton;
static int count = 0;
CRect rect;
CRect ButtonRect;
count++;
GetClientRect( rect );
pAddedButton = new CLedButton(ButtonText,this);
ButtonRect.SetRect( 0, 0, rect.Width(), (rect.Height() / NUMBER_OF_BUTTONS_IN_LIST_VIEW) );
pAddedButton->Create( "", WS_CHILD|BS_OWNERDRAW, ButtonRect, this, 1001+count );
int AddedAt = m_ButtonArray.Add( pAddedButton );
//Adds a string and assigns nIndex the index of the current item
int nIndex = AddString( "" );
// Set a hight for the data item
CListBox::SetItemHeight( nIndex, rect.Height() / NUMBER_OF_BUTTONS_IN_LIST_VIEW );
//If no error, associates the index with the button
if( nIndex!=LB_ERR && nIndex!=LB_ERRSPACE )
SetItemData( nIndex, (DWORD)m_ButtonArray.ElementAt(AddedAt) );
}
BEGIN_MESSAGE_MAP(CButtonListBox, CListBox)
//{{AFX_MSG_MAP(CButtonListBox)
ON_WM_DRAWITEM_REFLECT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonListBox message handlers
void CButtonListBox::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CListBox::PreSubclassWindow();
}
void CButtonListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect LedButtonRect = lpDrawItemStruct->rcItem; //Gets the rect of the item
CLedButton* pLedButton = reinterpret_cast(lpDrawItemStruct->itemData);
if(!pLedButton)
return;
pLedButton->MoveWindow(LedButtonRect);
pLedButton->CentreLED();
pLedButton->ShowWindow( SW_SHOW );
}
BOOL CButtonListBox::OnEraseBkgnd(CDC* pDC)
{
// Doing this speeds up drawing apparently
//return CWnd::OnEraseBkgnd(pDC);
return TRUE;
}
BOOL CButtonListBox::PreTranslateMessage(MSG* pMsg)
{
CLedButton* pButton = NULL;
CWnd* Parent = NULL;
CWnd* dispaly;
CString texts;
Parent = GetParent();
if(pMsg->message == UM_BUTTON_CLICK)
{
pButton = (CLedButton*)pMsg->lParam;
pButton->GetLed()->SetLed(CLed::LED_COLOR_RED,CLed::LED_ON,CLed::LED_ROUND);
pButton->GetLed()->Flash();
if(Parent)
{
dispaly = Parent->GetDlgItem(1002);
texts = pButton->GetButtonText();
dispaly->SetWindowText(texts);
}
}
return CListBox::PreTranslateMessage(pMsg);
}