|
/////////////////////////////////////////////////////////////////////////////
//
// BUTTON GROUP CONTROL
//
// (c) Andrew Peace 2001
//
// You may use and distribute this control in either source or compiled form
// by any means. You may not, however, remove this notice from the header of
// this file, nor sell this control for profit with express permission from
// the author.
//
// No responsibility will be taken for any damage caused by this control,
// though it has been thoroughly tested. If you find any bugs please let me
// know so that they can be fixed and incorporated into this source file.
//
//
// ButtonGroupCtl.cpp : implementation file
//
#include "stdafx.h"
#include "ButtonGroupCtl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CButtonGroupCtl
CButtonGroupCtl::CButtonGroupCtl()
{
m_pImageList = NULL;
}
CButtonGroupCtl::~CButtonGroupCtl()
{
while (!m_Buttons.IsEmpty())
delete m_Buttons.RemoveHead();
}
BEGIN_MESSAGE_MAP(CButtonGroupCtl, CStatic)
//{{AFX_MSG_MAP(CButtonGroupCtl)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CButtonGroupCtl message handlers
void CButtonGroupCtl::AddButton(int nID, int nImage, LPCTSTR pszToolTip)
{
ASSERT(!::IsWindow(m_hWnd));
FLATBUTTON *pBtn = new FLATBUTTON;
pBtn->ID = nID;
pBtn->ImageNumber = nImage;
pBtn->Tooltip = pszToolTip;
m_Buttons.AddTail(pBtn);
}
void CButtonGroupCtl::SetImageList(CImageList *pImageList)
{
ASSERT(!::IsWindow(m_hWnd));
ASSERT(pImageList);
m_pImageList = pImageList;
}
void CButtonGroupCtl::PreSubclassWindow()
{
ASSERT(m_pImageList);
CRect rc;
GetWindowRect(&rc);
ScreenToClient(&rc);
rc.top+=1; rc.bottom-=1;
rc.right-=1;
rc.left = rc.right-24;
POSITION pos = m_Buttons.GetTailPosition();
while (pos)
{
FLATBUTTON *fb = m_Buttons.GetPrev(pos);
fb->btn.Create(NULL, WS_VISIBLE|BS_ICON|BS_OWNERDRAW|BS_CENTER|BS_VCENTER, rc, this, fb->ID);
fb->btn.SetIcon(m_pImageList->ExtractIcon(fb->ImageNumber));
fb->btn.SetTooltipText(&fb->Tooltip);
rc.OffsetRect(-25,0);
}
CStatic::PreSubclassWindow();
}
BOOL CButtonGroupCtl::DestroyWindow()
{
POSITION pos = m_Buttons.GetHeadPosition();
while (pos)
m_Buttons.GetNext(pos)->btn.DestroyWindow();
return CStatic::DestroyWindow();
}
LRESULT CButtonGroupCtl::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_COMMAND)
{
if (HIWORD(wParam) == BN_CLICKED)
{
// redirect to parent window:
GetParent()->SendMessage(message, wParam, lParam);
}
}
return CStatic::DefWindowProc(message, wParam, lParam);
}
void CButtonGroupCtl::EnableButton(int nID, bool bEnable)
{
CWnd *pWnd = GetDlgItem(nID);
if (pWnd)
pWnd->EnableWindow(bEnable);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Andrew is currently a student at the University of Cambridge, working towards a degree in Computer Science. The word 'working' is used here in a vague sense, with the hope that the reader will realise that the same sentence contained the word 'student'.
Aside from computing he has a strong interest in music, and enjoys the outdoors, particularly when the weather permits going out in them... To Andrew, cycling is fun, sailing is more fun, and the odd camping trip and walk is certainly what the doctor ordered.
In terms of programming experience, he first started writing programs for the Commodore Amiga using BASIC, after which he learned the joys of C and has never looked back. Since, he has added C++ and C# to his repotoire, along with a bunch of other crazy languages like ML that they like to teach in college.