-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageButton.pas
465 lines (419 loc) · 10.8 KB
/
ImageButton.pas
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//----------------------------------------
//
// Copyright © ying32. All Rights Reserved.
//
// Licensed under Lazarus.modifiedLGPL
//
//----------------------------------------
///=======================================================
/// 功能:图像状态按钮
///
/// 注: 一般使推荐使用png图片,当然jpg、bmp也是可以的。
/// 一张png图的排序为 Normal, Hover, Down, Disabled,其中
/// Disabled状态可忽略
/// Orientation 分为横向排列和纵向排列的图片
///=======================================================
unit ImageButton;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
{$IFDEF FPC}
LMessages,
{$ELSE}
Winapi.Messages,
{$ENDIF}
Classes,
SysUtils,
Types,
Controls,
Graphics,
Forms;
type
{$IFNDEF FPC}
TLMessage = TMessage;
{$ENDIF}
{ TImageButton }
TImageOrientation = (ioHorizontal, ioVertical);
TImageButton = class(TGraphicControl)
private type
TButtonState = (bsNormal, bsHover, bsDown, bsDisabled);
private
FOrientation: TImageOrientation;
FState: TButtonState;
FPicture: TPicture;
FImageCount: Integer;
FCaption: string;
FShowCaption: Boolean;
FFont: TFont;
FWordwarp: Boolean;
FModalResult: TModalResult;
FMouseLeave: Boolean;
FCacheBmps: TImageList;
procedure SetOrientation(AValue: TImageOrientation);
procedure UpdateCacheBmps;
procedure SetPicture(const Value: TPicture);
procedure OnPictureChanged(Sender: TObject);
procedure OnFontChanged(Sender: TObject);
procedure SetImageCount(const Value: Integer);
procedure CMMouseEnter(var Message: TLMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TLMessage); message CM_MOUSELEAVE;
procedure SetCaption(const Value: string);
procedure SetShowCaption(const Value: Boolean);
procedure SetFont(const Value: TFont);
procedure SetWordwarp(const Value: Boolean);
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure Resize; override;
procedure SetEnabled(Value: Boolean); override;
procedure Paint; override;
procedure ResetSize;
procedure SetAutoSize(Value: Boolean); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Click; override;
published
property Action;
property Align;
property Anchors;
property AutoSize;
property Constraints;
property Caption: string read FCaption write SetCaption;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font: TFont read FFont write SetFont;
property ImageCount: Integer read FImageCount write SetImageCount default 3;
property Orientation: TImageOrientation read FOrientation write SetOrientation default ioHorizontal;
property ModalResult: TModalResult read FModalResult write FModalResult default 0;
property ParentShowHint;
property ParentFont;
property Picture: TPicture read FPicture write SetPicture;
property PopupMenu;
property ShowHint;
property ShowCaption: Boolean read FShowCaption write SetShowCaption;
property Visible;
property Wordwarp: Boolean read FWordwarp write SetWordwarp;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
end;
implementation
function IfThen(ABool: Boolean; ATrue, AFalse: Integer): Integer; inline;
begin
if ABool then Result := ATrue else Result := AFalse;
end;
{ TImageButton }
constructor TImageButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCacheBmps := TImageList.Create(Self);
FPicture := TPicture.Create;
FPicture.OnChange := {$IFDEF FPC}@{$ENDIF}OnPictureChanged;
FFont := TFont.Create;
FFont.OnChange := {$IFDEF FPC}@{$ENDIF}OnFontChanged;
FState := bsNormal;
FOrientation := ioHorizontal;
FImageCount := 3;
Width := 80;
Height := 25;
FShowCaption := False;
FCaption := '';
FModalResult := mrNone;
end;
destructor TImageButton.Destroy;
begin
FCacheBmps.Free;
FFont.Free;
FPicture.Free;
inherited;
end;
procedure TImageButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
FMouseLeave := False;
if csDesigning in ComponentState then Exit;
if not Enabled then Exit;
FState := bsDown;
Invalidate;
end;
procedure TImageButton.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
if csDesigning in ComponentState then Exit;
if not Enabled then Exit;
if FState = bsDown then Exit;
if FState <> bsHover then
begin
FState := bsHover;
Invalidate;
end;
end;
procedure TImageButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
if csDesigning in ComponentState then Exit;
if not Enabled then Exit;
if not FMouseLeave then
FState := bsHover
else FState := bsNormal;
Invalidate;
end;
procedure TImageButton.OnFontChanged(Sender: TObject);
begin
inherited Canvas.Font.Assign(Font);
Invalidate;
end;
procedure TImageButton.OnPictureChanged(Sender: TObject);
begin
FCacheBmps.Clear;
UpdateCacheBmps;
ResetSize;
FState := bsNormal; // 重设状态
Invalidate;
end;
procedure TImageButton.Click;
var
Form: TCustomForm;
begin
Form := GetParentForm(Self);
if Form <> nil then Form.ModalResult := ModalResult;
inherited Click;
end;
procedure TImageButton.Paint;
{$IFDEF FPC}
procedure DrawText;
var
LR: TRect;
LStyle: TTextStyle;
begin
if FShowCaption then
begin
LR := ClientRect;
inherited Canvas.Brush.Style := bsClear;
LStyle.Alignment := taCenter;
LStyle.Layout := tlCenter;
LStyle.Wordbreak := FWordwarp;
LStyle.SingleLine := not FWordwarp;
Canvas.TextRect(LR, 0, 0, FCaption, LStyle);
end;
end;
{$ELSE}
procedure DrawText;
begin
// 只用于在GenlibLcl工具中生成代码用
end;
{$ENDIF}
var
R: TRect;
begin
inherited Paint;
if not Visible then Exit;
if csDesigning in ComponentState then
begin
with Canvas do
begin
Pen.Style := psDash;
Brush.Style := bsClear;
Rectangle(0, 0, Self.Width-1, Self.Height-1);//????
end;
end;
if FPicture.Graphic = nil then
begin
DrawText;
Exit;
end;
{$ifdef fpc}
R := ClientRect;
case FCacheBmps.Count of
1: FCacheBmps.StretchDraw(Canvas, 0, R);
2:
begin
if FState = bsNormal then
FCacheBmps.StretchDraw(Canvas, 0, R)
else
FCacheBmps.StretchDraw(Canvas, 1, R)
end;
3:
begin
if FState = bsNormal then
FCacheBmps.StretchDraw(Canvas, 0, R)
else if FState = bsHover then
FCacheBmps.StretchDraw(Canvas, 1, R)
else
FCacheBmps.StretchDraw(Canvas, 2, R)
end;
4,5:
begin
if FState = bsNormal then
FCacheBmps.StretchDraw(Canvas, 0, R)
else if FState = bsHover then
FCacheBmps.StretchDraw(Canvas, 1, R)
else if FState = bsDown then
FCacheBmps.StretchDraw(Canvas, 2, R)
else
FCacheBmps.StretchDraw(Canvas, 3, R)
end;
end;
{$endif}
DrawText;
end;
procedure TImageButton.ResetSize;
begin
if AutoSize then
begin
if (FPicture.Graphic <> nil) and ((FPicture.Width > 0) and (FPicture.Height > 0)) then
begin
if Align in [alNone, alLeft, alRight] then
Width := FCacheBmps.Width; //IfThen(FOrientation = ioHorizontal, FPicture.Width div FImageCount, FPicture.Heigh;
if Align in [alNone, alTop, alBottom] then
Height := FCacheBmps.Height;//FPicture.Height;
end;
end;
end;
procedure TImageButton.SetAutoSize(Value: Boolean);
begin
inherited SetAutoSize(Value);
ResetSize;
end;
procedure TImageButton.Resize;
begin
inherited;
Invalidate;
end;
procedure TImageButton.SetCaption(const Value: string);
begin
if FCaption <> Value then
begin
FCaption := Value;
if FShowCaption then
Invalidate;
end;
end;
procedure TImageButton.SetEnabled(Value: Boolean);
begin
inherited SetEnabled(Value);
if not Value then
FState := bsDisabled
else FState := bsNormal;
Invalidate;
end;
procedure TImageButton.SetFont(const Value: TFont);
begin
FFont.Assign(Value);
end;
procedure TImageButton.SetImageCount(const Value: Integer);
begin
if FImageCount <> Value then
begin
FImageCount := Value;
if FImageCount <= 0 then
FImageCount := 1;
if FImageCount > 5 then
FImageCount := 5; // 5状态没有,先弄个吧
FState := bsNormal; // 重设状态
FCacheBmps.Clear;
UpdateCacheBmps;
ResetSize;
Invalidate;
end;
end;
procedure TImageButton.UpdateCacheBmps;
{$ifdef fpc}
procedure ReAdd;
begin
if FCacheBmps.Count <> 0 then
Exit;
FCacheBmps.BeginUpdate;
try
FCacheBmps.AddSliced(FPicture.Bitmap, IfThen(FOrientation = ioHorizontal, FImageCount, 1), IfThen(FOrientation = ioHorizontal, 1, FImageCount));
finally
FCacheBmps.EndUpdate;
end;
end;
{$endif}
var
LW, LH: Integer;
begin
if FPicture.Graphic = nil then
begin
FCacheBmps.Clear;
Exit;
end;
LW := IfThen(FOrientation = ioHorizontal, FPicture.Width div FImageCount, FPicture.Width);
LH := IfThen(FOrientation = ioHorizontal, FPicture.Height, FPicture.Height div FImageCount);
FCacheBmps.Width := LW;
FCacheBmps.Height := LH;
{$ifdef fpc}
ReAdd;
//// 貌似是个bug
if FCacheBmps.Count <> FImageCount then
begin
FCacheBmps.Clear;
ReAdd;
end;
{$endif}
end;
procedure TImageButton.SetOrientation(AValue: TImageOrientation);
begin
if FOrientation=AValue then Exit;
FOrientation:=AValue;
ResetSize;
FCacheBmps.Clear;
UpdateCacheBmps;
Invalidate;
end;
procedure TImageButton.SetPicture(const Value: TPicture);
begin
FCacheBmps.Clear;
FPicture.Assign(Value);
end;
procedure TImageButton.SetShowCaption(const Value: Boolean);
begin
if FShowCaption <> Value then
begin
FShowCaption := Value;
Invalidate;
end;
end;
procedure TImageButton.SetWordwarp(const Value: Boolean);
begin
if FWordwarp <> Value then
begin
FWordwarp := Value;
if FShowCaption then
Invalidate;
end;
end;
procedure TImageButton.CMMouseEnter(var Message: TLMessage);
begin
FMouseLeave := False;
if csDesigning in ComponentState then Exit;
if not Enabled then Exit;
FState := bsHover;
Invalidate;
end;
procedure TImageButton.CMMouseLeave(var Message: TLMessage);
begin
FMouseLeave := True;
if csDesigning in ComponentState then Exit;
if not Enabled then Exit;
FState := bsNormal;
Invalidate;
end;
end.