-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBitmapButton.pas
More file actions
221 lines (185 loc) · 6.67 KB
/
BitmapButton.pas
File metadata and controls
221 lines (185 loc) · 6.67 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
unit BitmapButton;
{
Inno Setup
Copyright (C) 1997-2025 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
A TImage-like component for bitmaps and png files without the TPicture bloat and
which is actually a button with a focus rectangle when focused - in
other words: an accessible TImage
Also supports other TGraphic types which can be assigned to a TBitmap
Make sure to set the Caption property, even if it isn't visible
Also see TBitmapImage which is the TGraphicControl version
}
interface
uses
Windows, Messages, ShellAPI, Controls, Graphics, Classes, Imaging.pngimage,
BitmapImage;
type
TBitmapButton = class(TCustomControl)
private
FFocusBorderWidthHeight: Integer;
FImpl: TBitmapImageImplementation;
procedure SetBackColor(Value: TColor);
procedure SetBitmap(Value: TBitmap);
procedure SetCenter(Value: Boolean);
procedure SetGraphic(Value: TGraphic);
procedure SetOpacity(Value: Byte);
procedure SetPngImage(Value: TPngImage);
procedure SetReplaceColor(Value: TColor);
procedure SetReplaceWithColor(Value: TColor);
procedure SetStretch(Value: Boolean);
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
protected
procedure CreateParams(var Params: TCreateParams); override;
function GetPalette: HPALETTE; override;
procedure Paint; override;
procedure SetAutoSize(Value: Boolean); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function InitializeFromIcon(const Instance: HINST; const Name: PChar; const BkColor: TColor; const AscendingTrySizes: array of Integer): Boolean;
function InitializeFromStockIcon(const Siid: SHSTOCKICONID; const BkColor: TColor; const AscendingTrySizes: array of Integer): Boolean;
property Bitmap: TBitmap read FImpl.Bitmap write SetBitmap;
property Graphic: TGraphic write SetGraphic;
published
property Align;
property Anchors;
property AutoSize: Boolean read FImpl.AutoSize write SetAutoSize default False;
property BackColor: TColor read FImpl.BackColor write SetBackColor default clNone;
property Caption;
property Center: Boolean read FImpl.Center write SetCenter default True;
property Enabled;
property Opacity: Byte read FImpl.Opacity write SetOpacity default 255;
property ParentShowHint;
property PngImage: TPngImage read FImpl.PngImage write SetPngImage;
property PopupMenu;
property ShowHint;
property Stretch: Boolean read FImpl.Stretch write SetStretch default False;
property ReplaceColor: TColor read FImpl.ReplaceColor write SetReplaceColor default clNone;
property ReplaceWithColor: TColor read FImpl.ReplaceWithColor write SetReplaceWithColor default clNone;
property TabOrder;
property TabStop default True;
property Visible;
property OnClick;
property OnDblClick;
property OnPaint: TPaintEvent read FImpl.OnPaint write FImpl.OnPaint;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('JR', [TBitmapButton]);
end;
constructor TBitmapButton.Create(AOwner: TComponent);
begin
inherited;
ControlStyle := ControlStyle + [csParentBackground, csReplicatable] - [csClickEvents];
{ Using a fixed focus border width/height to avoid design problems between systems }
FFocusBorderWidthHeight := 2;
const DoubleFBWH = 2*FFocusBorderWidthHeight;
FImpl.Init(Self, DoubleFBWH, DoubleFBWH);
Center := True;
TabStop := True;
Width := 75+DoubleFBWH;
Height := 25+DoubleFBWH;
end;
procedure TBitmapButton.CreateParams(var Params: TCreateParams);
begin
inherited;
CreateSubClass(Params, 'BUTTON');
Params.Style := Params.Style or BS_NOTIFY; { For BN_DBLCLK }
end;
destructor TBitmapButton.Destroy;
begin
FImpl.DeInit;
inherited;
end;
function TBitmapButton.InitializeFromIcon(const Instance: HINST; const Name: PChar; const BkColor: TColor; const AscendingTrySizes: array of Integer): Boolean;
begin
Result := FImpl.InitializeFromIcon(Instance, Name, BkColor, AscendingTrySizes);
end;
function TBitmapButton.InitializeFromStockIcon(const Siid: SHSTOCKICONID; const BkColor: TColor; const AscendingTrySizes: array of Integer): Boolean;
begin
Result := FImpl.InitializeFromStockIcon(siid, BkColor, AscendingTrySizes);
end;
procedure TBitmapButton.SetAutoSize(Value: Boolean);
begin
FImpl.SetAutoSize(Self, Value);
end;
procedure TBitmapButton.SetBackColor(Value: TColor);
begin
FImpl.SetBackColor(Self, Value);
end;
procedure TBitmapButton.SetBitmap(Value: TBitmap);
begin
FImpl.SetBitmap(Value);
end;
procedure TBitmapButton.SetCenter(Value: Boolean);
begin
FImpl.SetCenter(Self, Value);
end;
procedure TBitmapButton.SetGraphic(Value: TGraphic);
begin
FImpl.SetGraphic(Value);
end;
procedure TBitmapButton.SetOpacity(Value: Byte);
begin
FImpl.SetOpacity(Self, Value);
end;
procedure TBitmapButton.SetPngImage(Value: TPngImage);
begin
FImpl.SetPngImage(Value);
end;
procedure TBitmapButton.SetReplaceColor(Value: TColor);
begin
FImpl.SetReplaceColor(Self, Value);
end;
procedure TBitmapButton.SetReplaceWithColor(Value: TColor);
begin
FImpl.SetReplaceWithColor(Self, Value);
end;
procedure TBitmapButton.SetStretch(Value: Boolean);
begin
FImpl.SetStretch(Self, Value);
end;
function TBitmapButton.GetPalette: HPALETTE;
begin
Result := FImpl.GetPalette;
end;
procedure TBitmapButton.Paint;
begin
Canvas.Font := Font;
Canvas.Brush.Color := Color;
var R := ClientRect;
if Focused and (SendMessage(Handle, WM_QUERYUISTATE, 0, 0) and UISF_HIDEFOCUS = 0) then begin
{ See TBitBtn.DrawItem in Vcl.Buttons.pas }
Canvas.Pen.Color := clWindowFrame;
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clBtnFace;
{ This might draw a focus border thinner or thicker than our FFocusBorderWidthHeight but that's okay }
Canvas.DrawFocusRect(R);
end;
InflateRect(R, -FFocusBorderWidthHeight, -FFocusBorderWidthHeight);
FImpl.Paint(Self, Canvas, R);
end;
procedure TBitmapButton.WMSetFocus(var Message: TWMSetFocus);
begin
inherited;
Invalidate;
end;
procedure TBitmapButton.WMKillFocus(var Message: TWMKillFocus);
begin
inherited;
Invalidate;
end;
procedure TBitmapButton.CNCommand(var Message: TWMCommand);
begin
if (Message.NotifyCode = BN_CLICKED) then
Click
else if (Message.NotifyCode = BN_DBLCLK) then
DblClick;
end;
end.