/********************************************************************
created: 2005/06/03
created: 3:6:2005 13:22
filename: x:\Software\Mfc\Source\Controls\Buttons\RoundButton2.cpp
file path: x:\Software\Mfc\Source\Controls\Buttons
file base: RoundButton2
file ext: cpp
author: Markus Zocholl
purpose: CRoundButton2 defines a universal Button-Control with the
following features:
* Shape is a rounded Rectangle
* Button includes Border and Button-Face
* Many parameters to get an individual look
* Functions of Button to be en- or disabled:
- Button (disabled means a static control with userdefined styles)
- Hover
*********************************************************************/
#include "StdAfx.h"
#include <math.h>
#include ".\RoundButton2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/************************************************************************/
/* Construction and Destruction */
/************************************************************************/
//! Construction
CRoundButton2::CRoundButton2(void):
m_bDefaultButton(false)
, m_bIsCheckButton(false)
, m_bIsRadioButton(false)
, m_bIsHotButton(false)
, m_bMouseOnButton(false)
, m_bIsChecked(false)
, m_ptRoundButtonStyle(NULL)
, m_rBtnSize(CRect(0, 0, 0, 0))
, m_bRedraw(false)
, m_sOldCaption(_T(""))
{
// Set Standards in Font-Style
m_tLogFont.lfHeight = 16;
m_tLogFont.lfWidth = 0;
m_tLogFont.lfEscapement = 0;
m_tLogFont.lfOrientation = 0;
m_tLogFont.lfWeight = FW_BOLD;
m_tLogFont.lfItalic = false;
m_tLogFont.lfUnderline = false;
m_tLogFont.lfStrikeOut = false;
m_tLogFont.lfCharSet = DEFAULT_CHARSET;
m_tLogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
m_tLogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
m_tLogFont.lfQuality = ANTIALIASED_QUALITY;
m_tLogFont.lfPitchAndFamily = DEFAULT_PITCH;
strcpy(m_tLogFont.lfFaceName, "Tahoma");
m_tBtnFont.CreateFontIndirect(&m_tLogFont);
// Set Standard Font-Color
m_tTextColor.m_tDisabled = RGB(64, 64, 64);
m_tTextColor.m_tEnabled = RGB( 0, 0, 0);
m_tTextColor.m_tClicked = RGB( 0, 0, 0);
m_tTextColor.m_tPressed = RGB( 0, 0, 0);
m_tTextColor.m_tHot = RGB( 0, 0, 0);
}
//! Destruction
CRoundButton2::~CRoundButton2(void)
{
}
/************************************************************************/
/* public Functions */
/************************************************************************/
// Set Style of Button
bool CRoundButton2::SetRoundButtonStyle(CRoundButtonStyle* _ptRoundButtonStyle)
{
// Check, if Button-Style is given
if (_ptRoundButtonStyle == NULL)
return false;
// Set Pointer to ButtonStyle
m_ptRoundButtonStyle = _ptRoundButtonStyle;
// Redraw Button
m_bRedraw = true;
// All Done
return false;
}
// Set Font of Button
bool CRoundButton2::SetFont(LOGFONT* _ptLogFont)
{
if (_ptLogFont == NULL)
return false;
// Delete Font, if already given
if (m_tBtnFont.m_hObject != NULL)
m_tBtnFont.DeleteObject();
// Store Infos local
memcpy(&m_tLogFont, _ptLogFont, sizeof(LOGFONT));
// Create new Font
m_tBtnFont.CreateFontIndirect(&m_tLogFont);
// Button should be redrawn
m_bRedraw = true;
return true;
}
// Set Font of Button
bool CRoundButton2::GetFont(LOGFONT* _ptLogFont)
{
if (_ptLogFont == NULL)
return false;
// Store Infos local
memcpy(_ptLogFont, &m_tLogFont, sizeof(LOGFONT));
return true;
}
//! Set Color of Caption
bool CRoundButton2::SetTextColor(tColorScheme* _ptTextColor)
{
if (_ptTextColor == NULL)
return false;
// Store Infos locally
memcpy(&m_tTextColor, _ptTextColor, sizeof(tColorScheme));
// Button should be redrawn
m_bRedraw = true;
return true;
}
//! Get Color of Caption
bool CRoundButton2::GetTextColor(tColorScheme* _ptTextColor)
{
if (_ptTextColor == NULL)
return false;
// Store Infos locally
memcpy(_ptTextColor, &m_tTextColor, sizeof(tColorScheme));
return true;
}
/************************************************************************/
/* Own Drawing-Functions */
/************************************************************************/
//! Generate Bitmaps to hold Buttons
void CRoundButton2::GenButtonBMPs(CDC* _pDC, CRect _rRect)
{
if (m_tBmpBtn.m_hObject != NULL)
m_tBmpBtn.DeleteObject();
m_tBmpBtn.m_hObject = NULL;
// Generate Bitmap
if (m_tBmpBtn.CreateCompatibleBitmap(_pDC, _rRect.Width(), _rRect.Height() * BS_LAST_STATE) == FALSE)
{
m_rBtnSize = CRect(0, 0, 0, 0);
}
else
{
m_rBtnSize = _rRect;
}
}
//! Draw Button-Face
void CRoundButton2::DrawButtonFace(CDC* _pDC)
{
// We need an attached style
if (m_ptRoundButtonStyle == NULL)
return;
// Get Pointer to Bitmap of Masks
CBitmap* pButtonMasks = m_ptRoundButtonStyle->GetButtonEdge(_pDC);
// Create Memory-DC
CDC SourceDC;
SourceDC.CreateCompatibleDC(_pDC);
// Select Working Objects into DCs
HGDIOBJ hOldBmp1 = SourceDC.SelectObject(pButtonMasks);
int nState;
CSize tEdgeSize = m_ptRoundButtonStyle->GetEdgeSize();
CSize tCorrectedEdgeSize;
CSize tMaskSize = m_ptRoundButtonStyle->GetMaskSize();
// Correct Edge-Size for smaller Buttons
tCorrectedEdgeSize.cx = __min(tEdgeSize.cx, __min(m_rBtnSize.Width() / 2, m_rBtnSize.Height() / 2));
tCorrectedEdgeSize.cy = tCorrectedEdgeSize.cx;
for (nState = 0; nState < BS_LAST_STATE; nState++)
{
/************************************************************************/
/* Draw Edges */
/************************************************************************/
// Left-Top
_pDC->StretchBlt(
0,
nState * m_rBtnSize.Height(),
tCorrectedEdgeSize.cx,
tCorrectedEdgeSize.cy,
&SourceDC,
0,
nState * tMaskSize.cy,
tEdgeSize.cx,
tEdgeSize.cy,
SRCCOPY);
// Left-Bottom
_pDC->StretchBlt(
0,
nState * m_rBtnSize.Height() + m_rBtnSize.Height() - tCorrectedEdgeSize.cy,
tCorrectedEdgeSize.cx,
tCorrectedEdgeSize.cy,
&SourceDC,
0,
nState * tMaskSize.cy + tMaskSize.cy - tEdgeSize.cy,
tEdgeSize.cx,
tEdgeSize.cy,
SRCCOPY);
// Right-Top
_pDC->StretchBlt(
m_rBtnSize.Width() - tCorrectedEdgeSize.cx,
nState * m_rBtnSize.Height(),
tCorrectedEdgeSize.cx,
tCorrectedEdgeSize.cy,
&SourceDC,
tMaskSize.cx - tEdgeSize.cx,
nState * tMaskSize.cy,
tEdgeSize.cx,
tEdgeSize.cy,
SRCCOPY);
// Right-Bottom
_pDC->StretchBlt(
m_rBtnSize.Width() - tCorrectedEdgeSize.cx,
nState * m_rBtnSize.Height() + m_rBtnSize.Height() - tCorrectedEdgeSize.cy,
tCorrectedEdgeSize.cx,
tCorrectedEdgeSize.cy,
&SourceDC,
tMaskSize.cx - tEdgeSize.cx,
nState * tMaskSize.cy + tMaskSize.cy - tEdgeSize.cy,
tEdgeSize.cx,
tEdgeSize.cy,
SRCCOPY);
/************************************************************************/
/* Draw Sides */
/************************************************************************/
// Top
_pDC->StretchBlt(
tCorrectedEdgeSize.cx,
nState * m_rBtnSize.Height(),
m_rBtnSize.Width() - 2 * tCorrectedEdgeSize.cx,
tCorrectedEdgeSize.cy,
&SourceDC,
tEdgeSize.cx,
nState * tMaskSize.cy,
1,
tEdgeSize.cy,
SRCCOPY);
// Bottom
_pDC->StretchBlt(
tCorrectedEdgeSize.cx,
nState * m_rBtnSize.Height() + m_rBtnSize.Height() - tCorrectedEdgeSize.cy,
m_rBtnSize.Width() - 2 * tCorrectedEdgeSize.cx,
tCorrectedEdgeSize.cy,
&SourceDC,
tEdgeSize.cx,
nState * tMaskSize.cy + tMaskSize.cy - tEdgeSize
- 1
- 2
- 3
- 4
前往页