-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBitmapButton.cpp
118 lines (92 loc) · 3.34 KB
/
BitmapButton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// *****************************************************************************
//
// Copyright (c) 2008, Pleora Technologies Inc., All rights reserved.
//
// *****************************************************************************
#include "stdafx.h"
#include "BitmapButton.h"
BEGIN_MESSAGE_MAP(BitmapButton, CButton)
END_MESSAGE_MAP()
// =============================================================================
BitmapButton::BitmapButton()
{
}
// =============================================================================
BitmapButton::~BitmapButton()
{
}
// =============================================================================
void BitmapButton::DrawItem( LPDRAWITEMSTRUCT lpDIS )
{
CDC lDC;
lDC.Attach( lpDIS->hDC );
CRect lRect( &lpDIS->rcItem );
// Clean background
lDC.FillSolidRect( lRect, lDC.GetBkColor() );
// Load them
HTHEME lTheme = ::OpenThemeData( GetSafeHwnd(), _T( "BUTTON" ) );
// Set state for theme background
int lState = 0;
if ( ( lpDIS->itemState & ODS_SELECTED ) != 0 )
{
lState = PBS_PRESSED;
}
else if ( ( lpDIS->itemState & ODS_DISABLED ) != 0 )
{
lState = PBS_DISABLED;
}
else
{
lState = PBS_NORMAL;
}
// Draw theme background
::DrawThemeBackground( lTheme, lpDIS->hDC, BP_PUSHBUTTON, lState, &lpDIS->rcItem, NULL );
// We are done with the theme, close it
::CloseThemeData( lTheme );
CString lText;
GetWindowText( lText );
// Process dynamic layout
CSize lTextSize = lDC.GetTextExtent( lText );
int lSpace = 3;
int lTotalX = max( mBitmapSize.cx, lTextSize.cx );
int lTotalY = mBitmapSize.cy + lSpace + lTextSize.cy;
int lBitmapX = ( lRect.Width() / 2 ) - ( mBitmapSize.cx / 2 );
int lBitmapY = ( lRect.Height() / 2 ) - ( lTotalY / 2 );
if ( ( lpDIS->itemState & ODS_DISABLED ) == 0 )
{
// Enabled - use TransparentBlt
CDC lBitmapDC;
lBitmapDC.CreateCompatibleDC( &lDC );
CSize lSize = lBitmapDC.GetWindowExt();
CBitmap *lOldBitmap = lBitmapDC.SelectObject( &mBitmap );
lDC.TransparentBlt(
lBitmapX, lBitmapY, mBitmapSize.cx, mBitmapSize.cy,
&lBitmapDC,
0, 0, mBitmapSize.cx, mBitmapSize.cy,
RGB( 0xFF, 0xFF, 0xFF ) );
}
else
{
// Disabled - use DrawState. White will be converted to background color
lDC.DrawState( CPoint( lBitmapX, lBitmapY ), mBitmapSize, mBitmap, DSS_DISABLED );
}
// Compute text position
int lTextX = ( lRect.Width() / 2 ) - ( lTextSize.cx / 2 );
int lTextY = ( lRect.Height() / 2 ) + lTotalY / 2 - lTextSize.cy;
// Draw text - transparent, taking in account enabled/disabled state
CRect lTextRect( lTextX, lTextY, lTextX + lTextSize.cx, lTextY + lTextSize.cy );
lDC.SetBkMode( TRANSPARENT );
lDC.SetTextColor( ( ( lpDIS->itemState & ODS_DISABLED ) != 0 ) ?
::GetSysColor( COLOR_GRAYTEXT ) :
::GetSysColor( COLOR_BTNTEXT ) );
lDC.DrawText( lText, &lTextRect, DT_LEFT | DT_TOP );
}
// =============================================================================
void BitmapButton::SetBitmap( int aResourceID )
{
VERIFY( mBitmap.LoadBitmap( aResourceID ) );
BITMAP lBm;
mBitmap.GetBitmap( &lBm );
mBitmapSize.cx = lBm.bmWidth;
mBitmapSize.cy = lBm.bmHeight;
}