-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbutton.pas
460 lines (405 loc) · 10.5 KB
/
xbutton.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
//----------------------------------------
//
// Copyright © ying32. All Rights Reserved.
//
// Licensed under Lazarus.modifiedLGPL
//
//----------------------------------------
unit xButton;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
Classes, SysUtils, Controls, Graphics
{$IFNDEF FPC}
,Winapi.Messages, System.Types
{$ELSE}
,LMessages
{$ENDIF};
type
{$IFDEF FPC}
TMessage = TLMessage;
{$ENDIF}
{ TXButton }
TDrawImageMode = (dimNormal, dimCenter, dimStretch);
TXButtonState = (xbsNone, xbsHot, xbsDown, xbsDisabled);
TXButton = class(TGraphicControl)
private
FCaption: string;
FDownFontColor: TColor;
FDrawMode: TDrawImageMode;
FHoverFontColor: TColor;
FNormalFontColor: TColor;
FShowCaption: Boolean;
FState: TXButtonState;
FBackColor: TColor;
FBorderColor: TColor;
FBorderWidth: Integer;
FDownColor: TColor;
FHoverColor: TColor;
FPicture: TPicture;
FBufferBmp: TBitmap;
FMouseInControl: Boolean;
procedure StateChanged;
procedure DoPictureChange(Sender: TObject);
procedure DrawBmp;
procedure SetBackColor(AValue: TColor);
procedure SetBorderColor(AValue: TColor);
procedure SetBorderWidth(AValue: Integer);
procedure SetCaption(AValue: string);
procedure SetDownColor(AValue: TColor);
procedure SetDownFontColor(AValue: TColor);
procedure SetDrawMode(AValue: TDrawImageMode);
procedure SetHoverColor(AValue: TColor);
procedure SetHoverFontColor(AValue: TColor);
procedure SetNormalFontColor(AValue: TColor);
procedure SetPicture(AValue: TPicture);
procedure SetShowCaption(AValue: Boolean);
protected
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure MouseDown(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
procedure Resize; override;
published
property Caption: string read FCaption write SetCaption;
property ShowCaption: Boolean read FShowCaption write SetShowCaption;
property BackColor: TColor read FBackColor write SetBackColor;
property HoverColor: TColor read FHoverColor write SetHoverColor;
property DownColor: TColor read FDownColor write SetDownColor;
property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
property BorderColor: TColor read FBorderColor write SetBorderColor;
property Picture: TPicture read FPicture write SetPicture;
property DrawMode: TDrawImageMode read FDrawMode write SetDrawMode;
property NormalFontColor: TColor read FNormalFontColor write SetNormalFontColor;
property DownFontColor: TColor read FDownFontColor write SetDownFontColor;
property HoverFontColor: TColor read FHoverFontColor write SetHoverFontColor;
property Action;
property Align;
property Anchors;
property BiDiMode;
property Constraints;
property Enabled;
property Font;
property ParentFont;
property ParentShowHint;
property ParentBiDiMode;
property PopupMenu;
property ShowHint;
property Visible;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
end;
implementation
{ TXButton }
constructor TXButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width := 80;
Height := 40;
FBufferBmp := TBitmap.Create;
FPicture := TPicture.Create;
FPicture.OnChange := {$IFDEF FPC}@{$ENDIF}DoPictureChange;
FShowCaption := True;
FDrawMode := dimCenter;
FBackColor := clBtnFace;
FState := xbsNone;
end;
destructor TXButton.Destroy;
begin
FPicture.Free;
FBufferBmp.Free;;
inherited Destroy;
end;
procedure TXButton.Paint;
begin
inherited Paint;
if csDesigning in ComponentState then
begin
Canvas.Draw(1, 1,FBufferBmp);
Canvas.Pen.Style := psDot;
Canvas.Pen.Color := clBlack;
Canvas.Brush.Style := bsClear;
Canvas.Rectangle(Rect(0, 0, Width -1, Height - 1)); ///???
end else
Canvas.Draw(0, 0,FBufferBmp);
end;
procedure TXButton.Resize;
begin
inherited Resize;
if Assigned(FBufferBmp) then
begin
FBufferBmp.SetSize(Width, Height);
FBufferBmp.PixelFormat := pf24bit;
StateChanged;
end;
end;
procedure TXButton.StateChanged;
type
TRGB = record
{$if Defined(WINDOWS) or Defined(MSWINDOWS)}
B, G, R: Byte;
{$elseif Defined(LINUX) or Defined(DARWIN)}
R, G, B: Byte;
{$ENDIF}
end;
PRGB = ^TRGB;
var
X, Y: Integer;
Ptr: PByte;
LGray: Byte;
LC: PRGB;
begin
DrawBmp;
if not Enabled then
begin
{$IFDEF FPC}
FBufferBmp.BeginUpdate;
try
{$ENDIF}
// 灰度化图像
for Y := 0 to FBufferBmp.Height - 1 do
begin
Ptr := PByte(FBufferBmp.ScanLine[Y]);
for X := 0 to FBufferBmp.Width - 1 do
begin
LC := PRGB(Ptr + X * 3);
LGray := Byte(Round(0.299 * LC^.R + 0.587 * LC^.G + 0.114 * LC^.B + 0.1));
LC^.R := LGray;
LC^.G := LGray;
LC^.B := LGray;
end;
end;
{$IFDEF FPC}
finally
FBufferBmp.EndUpdate;
end;
{$ENDIF}
end;
Invalidate;
end;
procedure TXButton.DoPictureChange(Sender: TObject);
begin
StateChanged;
end;
procedure TXButton.DrawBmp;
var
LR: TRect;
{$IFDEF FPC}
LTextStyle: TTextStyle;
{$ENDIF}
LBrushColor, LFontColor: TColor;
begin
LR := ClientRect;//Rect(0, 0, Width, Height);
with FBufferBmp do
begin
FreeImage;
if (FBufferBmp.Width <> LR.Width) or (FBufferBmp.Height <> LR.Height) then
FBufferBmp.SetSize(LR.Width, LR.Height);
Canvas.Font := Self.Font;
// pen清除
Canvas.Pen.Style := psClear;
// 画背景 鼠标背景
Canvas.Brush.Style:= bsSolid;
case FState of
xbsDown:
begin
LBrushColor := FDownColor;
LFontColor := FDownFontColor;
end;
xbsHot:
begin
LBrushColor := FHoverColor;
LFontColor := FHoverFontColor;
end;
else
LBrushColor := FBackColor;
LFontColor := FNormalFontColor
end;
Canvas.Brush.Color:= LBrushColor;
Canvas.Rectangle(LR);
// 画图片背景
if Assigned(FPicture) and Assigned(FPicture.Graphic) then
begin
case FDrawMode of
dimNormal:
Canvas.Draw(0, 0, FPicture.Graphic);
dimCenter:
Canvas.Draw((LR.Width - FPicture.Graphic.Width) div 2, (LR.Height - FPicture.Graphic.Height) div 2, FPicture.Graphic);
dimStretch:
Canvas.StretchDraw(LR, FPicture.Graphic);
end;
end;
// 画边框
if FBorderWidth > 0 then
begin
Canvas.Pen.Width := FBorderWidth;
Canvas.Pen.Color := FBorderColor;
Canvas.Pen.Style := psSolid;
Canvas.Brush.Style := bsClear;
Canvas.Rectangle(LR);
end;
// 画文字
if FShowCaption and (FCaption <> '') then
begin
Canvas.Brush.Style := bsClear;
Canvas.Font.Color:= LFontColor;
{$IFDEF FPC}
LTextStyle := Canvas.TextStyle;
LTextStyle.Alignment := taCenter;
LTextStyle.Layout := tlCenter;
Canvas.TextRect(LR, 0, 0, FCaption, LTextStyle);
{$ELSE}
Canvas.TextRect(LR, FCaption, [tfCenter, tfVerticalCenter, tfSingleLine]);
{$ENDIF}
end;
end;
end;
procedure TXButton.SetBackColor(AValue: TColor);
begin
if FBackColor=AValue then Exit;
FBackColor:=AValue;
StateChanged;
end;
procedure TXButton.SetBorderColor(AValue: TColor);
begin
if FBorderColor=AValue then Exit;
FBorderColor:=AValue;
StateChanged;
end;
procedure TXButton.SetBorderWidth(AValue: Integer);
begin
if FBorderWidth=AValue then Exit;
FBorderWidth:=AValue;
StateChanged;
end;
procedure TXButton.SetCaption(AValue: string);
begin
if FCaption=AValue then Exit;
FCaption:=AValue;
StateChanged;
end;
procedure TXButton.SetDownColor(AValue: TColor);
begin
if FDownColor=AValue then Exit;
FDownColor:=AValue;
StateChanged;
end;
procedure TXButton.SetDownFontColor(AValue: TColor);
begin
if FDownFontColor=AValue then Exit;
FDownFontColor:=AValue;
StateChanged;
end;
procedure TXButton.SetDrawMode(AValue: TDrawImageMode);
begin
if FDrawMode=AValue then Exit;
FDrawMode:=AValue;
StateChanged;
end;
procedure TXButton.SetHoverColor(AValue: TColor);
begin
if FHoverColor=AValue then Exit;
FHoverColor:=AValue;
StateChanged;
end;
procedure TXButton.SetHoverFontColor(AValue: TColor);
begin
if FHoverFontColor=AValue then Exit;
FHoverFontColor:=AValue;
StateChanged;
end;
procedure TXButton.SetNormalFontColor(AValue: TColor);
begin
if FNormalFontColor=AValue then Exit;
FNormalFontColor:=AValue;
StateChanged;
end;
procedure TXButton.SetPicture(AValue: TPicture);
begin
FPicture.Assign(AValue);
end;
procedure TXButton.SetShowCaption(AValue: Boolean);
begin
if FShowCaption=AValue then Exit;
FShowCaption:=AValue;
StateChanged;
end;
procedure TXButton.CMMouseEnter(var Message: TMessage);
begin
inherited;
if csDesigning in ComponentState then
Exit;
if not Enabled then
Exit;
FMouseInControl := True;
FState := xbsHot;
StateChanged;
end;
procedure TXButton.CMMouseLeave(var Message: TMessage);
begin
inherited;
if csDesigning in ComponentState then
Exit;
if not Enabled then
Exit;
FState := xbsNone;
StateChanged;
FMouseInControl := False;
end;
procedure TXButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if csDesigning in ComponentState then
Exit;
if Button = mbLeft then
begin
if not Enabled then
Exit;
FState := xbsDown;
StateChanged;
end;
end;
procedure TXButton.CMEnabledChanged(var Message: TMessage);
begin
inherited;
if Enabled then
FState := xbsNone
else FState := xbsDisabled;
StateChanged;
end;
procedure TXButton.CMFontChanged(var Message: TMessage);
begin
inherited;
StateChanged;
end;
procedure TXButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited MouseUp(Button, Shift, X, Y);
if csDesigning in ComponentState then
Exit;
if Button = mbLeft then
begin
if not Enabled then
Exit;
if FMouseInControl then
FState := xbsHot
else FState := xbsNone;
StateChanged;
end;
end;
end.