📄 buttonex.cpp
字号:
// buttonEx.cpp : implementation file
//
#include "stdafx.h"
#include "buttonEx.h"
#include "Img2Rgn.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBtnTextSample
CBtnTextSample::CBtnTextSample()
{
m_format = DT_CENTER | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER;
m_bgColor = ::GetSysColor( COLOR_WINDOW );
m_fgColor = ::GetSysColor( COLOR_WINDOWTEXT );
}
CBtnTextSample::~CBtnTextSample()
{
}
BEGIN_MESSAGE_MAP(CBtnTextSample, CButton)
//{{AFX_MSG_MAP(CBtnTextSample)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBtnTextSample message handlers
void CBtnTextSample::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rcItem(lpDrawItemStruct->rcItem);
BOOL bFocus = (GetFocus() == this);
COLORREF clrHighLight = ::GetSysColor(COLOR_HIGHLIGHT);
COLORREF clrHighLightText = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
pDC->SetBkMode( TRANSPARENT );
if( bFocus )
{
pDC->SetTextColor( clrHighLightText );
pDC->SetBkColor( clrHighLight );
pDC->FillRect(rcItem, &CBrush( clrHighLight ) );
}
else
{
pDC->SetTextColor( m_fgColor );
pDC->FillRect(rcItem, &CBrush( m_bgColor ) );
}
// draw sample text
if( !m_sampleText.IsEmpty() )
{
CFont * pFont = NULL;
if( m_font.GetSafeHandle() != NULL )
{
pFont = (CFont*)pDC->SelectObject( &m_font );
}
pDC->DrawText( m_sampleText, rcItem, m_format );
if( pFont != NULL )
{
pDC->SelectObject( pFont );
}
}
if( bFocus )
{
pDC->DrawFocusRect(rcItem); //draw focus rectangle
}
}
/////////////////////////////////////////////////////////////////////////////
// CBtnColor
CBtnColor::CBtnColor()
{
m_color = ::GetSysColor(COLOR_WINDOW);
m_pushStyle = BTNPUSH_NONE;
m_pushed = FALSE;
}
CBtnColor::~CBtnColor()
{
}
BEGIN_MESSAGE_MAP(CBtnColor, CButton)
//{{AFX_MSG_MAP(CBtnColor)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBtnColor message handlers
void CBtnColor::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CRect rect(lpDrawItemStruct->rcItem);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
pDC->FillRect( rect, &CBrush(m_color));
if( m_pushStyle == BTNPUSH_NONE )
{
return;
}
if( m_pushed )
{
pDC->DrawEdge( rect, EDGE_SUNKEN, BF_RECT );
}
else
{
pDC->DrawEdge( rect, EDGE_RAISED, BF_RECT );
}
}
BOOL CBtnColor::Create( const RECT& rect, CWnd* pParentWnd, UINT nID )
{
return CButton::Create(
"ColorBtn",
BS_OWNERDRAW | BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
rect, pParentWnd, nID);
}
void CBtnColor::OnLButtonDown(UINT nFlags, CPoint point)
{
switch( m_pushStyle )
{
case BTNPUSH_CHECK:
m_pushed = !m_pushed;
break;
case BTNPUSH_STD:
m_pushed = TRUE;
break;
default:
m_pushed = FALSE;
}
// SetCapture();
Invalidate();
CButton::OnLButtonDown(nFlags, point);
}
void CBtnColor::OnLButtonUp(UINT nFlags, CPoint point)
{
switch( m_pushStyle )
{
case BTNPUSH_CHECK:
break;
case BTNPUSH_STD:
m_pushed = FALSE;
break;
default:
m_pushed = FALSE;
}
// ::ReleaseCapture();
Invalidate();
CButton::OnLButtonUp(nFlags, point);
}
void CBtnColor::Check( BOOL push )
{
m_pushed = push;
Invalidate();
}
int CBtnColor::GetCheckStatus()
{
return m_pushed;
}
/////////////////////////////////////////////////////////////////////////////
// CBtnImage
BEGIN_MESSAGE_MAP(CBtnImage, CButton)
//{{AFX_MSG_MAP(CBtnImage)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CBtnImage::CBtnImage()
{
m_enableStretch = TRUE;
m_focusOnMouseMove = FALSE;
m_haveMouse = FALSE;
m_isPushed = FALSE;
m_isChecked = FALSE;
}
CBtnImage::~CBtnImage()
{
}
BOOL CBtnImage::SetImgIDNormal( UINT imgID )
{
if( m_bitmapNormal.GetSafeHandle() != NULL )
{
m_bitmapNormal.DeleteObject();
}
return m_bitmapNormal.LoadBitmap( imgID );
}
BOOL CBtnImage::SetImgIDDown( UINT imgID )
{
if( m_bitmapDown.GetSafeHandle() != NULL )
{
m_bitmapDown.DeleteObject();
}
return m_bitmapDown.LoadBitmap( imgID );
}
BOOL CBtnImage::SetImgIDFocus( UINT imgID )
{
if( m_bitmapFocus.GetSafeHandle() != NULL )
{
m_bitmapFocus.DeleteObject();
}
return m_bitmapFocus.LoadBitmap( imgID );
}
BOOL CBtnImage::SetImgIDDisable( UINT imgID )
{
if( m_bitmapDisable.GetSafeHandle() != NULL )
{
m_bitmapDisable.DeleteObject();
}
return m_bitmapDisable.LoadBitmap( imgID );
}
BOOL CBtnImage::SetImgIDCheck( UINT imgID )
{
if( m_bitmapCheck.GetSafeHandle() != NULL )
{
m_bitmapCheck.DeleteObject();
}
return m_bitmapCheck.LoadBitmap( imgID );
}
BOOL CBtnImage::SetImgIDHaveMouse( UINT imgID )
{
if( m_bitmapHaveMouse.GetSafeHandle() != NULL )
{
m_bitmapHaveMouse.DeleteObject();
}
return m_bitmapHaveMouse.LoadBitmap( imgID );
}
BOOL CBtnImage::SetImgNameNormal( UINT imgName )
{
return TRUE;
}
BOOL CBtnImage::SetImgNameDown( UINT imgName )
{
return TRUE;
}
BOOL CBtnImage::SetImgNameFocus( UINT imgName )
{
return TRUE;
}
BOOL CBtnImage::SetImgNameDisable( UINT imgName )
{
return TRUE;
}
BOOL CBtnImage::SetImgNameCheck( UINT imgName )
{
return TRUE;
}
BOOL CBtnImage::SetImgNameHaveMouse( UINT imgName )
{
return TRUE;
}
void CBtnImage::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rcItem(lpDrawItemStruct->rcItem);
BOOL bFocus = (GetFocus() == this);
CBitmap * pBitmap;
//设按钮位图
pBitmap = &m_bitmapNormal;
if( ::GetWindowLong( m_hWnd, GWL_STYLE ) & WS_DISABLED
&& (m_bitmapDisable.GetSafeHandle() != NULL) )
{//按钮被禁止
pBitmap = &m_bitmapDisable;
}
else if( (0x0004 & GetState()) && (m_bitmapDown.GetSafeHandle() != NULL) )
// if( (lpDrawItemStruct->itemState == ODS_CHECKED) && (m_bitmapDown.GetSafeHandle() != NULL) )
{//按钮被按下
pBitmap = &m_bitmapDown;
}
else if( m_haveMouse )
{//鼠标在按钮上
pBitmap = &m_bitmapHaveMouse;
}
// else if( (0x0003 & GetState()) && (m_bitmapCheck.GetSafeHandle() != NULL ) )
else if( (lpDrawItemStruct->itemState == ODS_CHECKED) && (m_bitmapCheck.GetSafeHandle() != NULL ) )
{//按钮被选中,Check or Radio 按钮
pBitmap = &m_bitmapCheck;
}
else if( bFocus && (m_bitmapFocus.GetSafeHandle() != NULL) )
{//按钮有输入焦点
pBitmap = &m_bitmapFocus;
}
if( pBitmap->GetSafeHandle() == NULL )
{
return;
}
CDC memDC;
if( !memDC.CreateCompatibleDC( pDC ) )
{
return;
}
CBitmap * oldBitmap;
BITMAP sBitmap;
pBitmap->GetBitmap( &sBitmap );
if( !m_enableStretch )
{//不允许缩放位图,要先调整控件大小
if( rcItem.Width() != sBitmap.bmWidth
|| rcItem.Height() != sBitmap.bmHeight )
{
CWnd * pWnd = GetParent();
rcItem.right = rcItem.left + sBitmap.bmWidth;
rcItem.bottom = rcItem.top + sBitmap.bmHeight;
if( pWnd != NULL )
{
CRect wndRect;
GetWindowRect( wndRect );
pWnd->ScreenToClient( wndRect );
wndRect.right = wndRect.left + sBitmap.bmWidth;
wndRect.bottom = wndRect.top + sBitmap.bmHeight;
MoveWindow( wndRect, FALSE );//调窗口大小但不重画
}
}
}
oldBitmap = memDC.SelectObject( pBitmap );
pDC->StretchBlt(
rcItem.left, rcItem.top,
rcItem.Width(), rcItem.Height(),
&memDC,
0, 0,
sBitmap.bmWidth, sBitmap.bmHeight,
SRCCOPY );
memDC.SelectObject( oldBitmap );
/* if( bFocus )
{
pDC->DrawFocusRect(rcItem); //draw focus rectangle
}
*/
}
void CBtnImage::OnMouseMove(UINT nFlags, CPoint point)
{
CButton::OnMouseMove(nFlags, point);
if( m_focusOnMouseMove )
{
CRect rect;
GetWindowRect( rect );
if( !rect.PtInRect( point + CPoint( rect.left, rect.top ) ) )
{
m_haveMouse = FALSE;
::ReleaseCapture();
Invalidate();
return;
}
if( !m_haveMouse )
{
Invalidate();
m_haveMouse = TRUE;
}
if( GetCapture() != this )
{
SetCapture();
}
}
}
void CBtnImage::OnLButtonUp(UINT nFlags, CPoint point)
{
CButton::OnLButtonUp(nFlags, point);
if( m_haveMouse )
{
m_haveMouse = FALSE;
Invalidate();
}
}
BOOL CBtnImage::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
// Round Buttons!
//
// Written by Chris Maunder (Chris.Maunder@cbr.clw.csiro.au)
// Copyright (c) 1997,1998.
//
// Modified: 2 Feb 1998 - Fix vis problem, CRgn resource leak,
// button reposition code redone. CJM.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
/////////////////////////////////////////////////////////////////////////////
#include "math.h"
// Calculate colour for a point at the given angle by performing a linear
// interpolation between the colours crBright and crDark based on the cosine
// of the angle between the light source and the point.
//
// Angles are measured from the +ve x-axis (i.e. (1,0) = 0 degrees, (0,1) = 90 degrees )
// But remember: +y points down!
COLORREF CBtnEx::GetColour(double dAngle, COLORREF crBright, COLORREF crDark)
{
#define Rad2Deg 180.0/3.1415
// For better light-continuity along the edge of a stretched button:
// LIGHT_SOURCE_ANGLE == -1.88
//#define LIGHT_SOURCE_ANGLE -2.356 // -2.356 radians = -135 degrees, i.e. From top left
#define LIGHT_SOURCE_ANGLE -1.88
ASSERT(dAngle > -3.1416 && dAngle < 3.1416);
double dAngleDifference = LIGHT_SOURCE_ANGLE - dAngle;
if (dAngleDifference < -3.1415) dAngleDifference = 6.293 + dAngleDifference;
else if (dAngleDifference > 3.1415) dAngleDifference = 6.293 - dAngleDifference;
double Weight = 0.5*(cos(dAngleDifference)+1.0);
BYTE Red = (BYTE) (Weight*GetRValue(crBright) + (1.0-Weight)*GetRValue(crDark));
BYTE Green = (BYTE) (Weight*GetGValue(crBright) + (1.0-Weight)*GetGValue(crDark));
BYTE Blue = (BYTE) (Weight*GetBValue(crBright) + (1.0-Weight)*GetBValue(crDark));
//TRACE("LightAngle = %0.0f, Angle = %3.0f, Diff = %3.0f, Weight = %0.2f, RGB %3d,%3d,%3d\n",
// LIGHT_SOURCE_ANGLE*Rad2Deg, dAngle*Rad2Deg, dAngleDifference*Rad2Deg, Weight,Red,Green,Blue);
return RGB(Red, Green, Blue);
}
void CBtnEx::DrawCircle(CDC* pDC, CPoint p, LONG lRadius, COLORREF crColour, BOOL bDashed)
{
const int nDashLength = 1;
LONG lError, lXoffset, lYoffset;
int nDash = 0;
BOOL bDashOn = TRUE;
//Check to see that the coordinates are valid
ASSERT( (p.x + lRadius <= LONG_MAX) && (p.y + lRadius <= LONG_MAX) );
ASSERT( (p.x - lRadius >= LONG_MIN) && (p.y - lRadius >= LONG_MIN) );
//Set starting values
lXoffset = lRadius;
lYoffset = 0;
lError = -lRadius;
do {
if (bDashOn) {
pDC->SetPixelV(p.x + lXoffset, p.y + lYoffset, crColour);
pDC->SetPixelV(p.x + lXoffset, p.y - lYoffset, crColour);
pDC->SetPixelV(p.x + lYoffset, p.y + lXoffset, crColour);
pDC->SetPixelV(p.x + lYoffset, p.y - lXoffset, crColour);
pDC->SetPixelV(p.x - lYoffset, p.y + lXoffset, crColour);
pDC->SetPixelV(p.x - lYoffset, p.y - lXoffset, crColour);
pDC->SetPixelV(p.x - lXoffset, p.y + lYoffset, crColour);
pDC->SetPixelV(p.x - lXoffset, p.y - lYoffset, crColour);
}
//Advance the error term and the constant X axis step
lError += lYoffset++;
//Check to see if error term has overflowed
if ((lError += lYoffset) >= 0)
lError -= --lXoffset * 2;
if (bDashed && (++nDash == nDashLength)) {
nDash = 0;
bDashOn = !bDashOn;
}
} while (lYoffset <= lXoffset); //Continue until halfway point
}
// The original Drawcircle function is split up into DrawCircleRight and DrawCircleLeft
// to make stretched buttons
//
void CBtnEx::DrawCircleRight(CDC* pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark)
{
LONG lError, lXoffset, lYoffset;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -