-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatbutton.pas
More file actions
103 lines (88 loc) · 2.54 KB
/
Copy pathflatbutton.pas
File metadata and controls
103 lines (88 loc) · 2.54 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
unit flatbutton;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Controls, Buttons, Graphics, Themes, LCLType, Types;
type
TFlatButton = class(TSpeedButton)
private
FOffsetY: Integer;
procedure SetOffsetY(AValue: Integer);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
published
// Vertical offset for the caption relative to the icon center (0 = default centered)
property OffsetY: Integer read FOffsetY write SetOffsetY default 0;
end;
implementation
constructor TFlatButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FOffsetY := 0;
Flat := True; // Always draw as a flat button
end;
procedure TFlatButton.SetOffsetY(AValue: Integer);
begin
if FOffsetY = AValue then Exit;
FOffsetY := AValue;
Invalidate;
end;
procedure TFlatButton.Paint;
var
r: TRect;
xIcon, yIcon, xText: Integer;
ts: TTextStyle;
Details: TThemedElementDetails;
imgW, imgH: Integer;
begin
// Toolbar theme elements give the native flat look
if Down then
Details := ThemeServices.GetElementDetails(ttbButtonPressed)
else if MouseInClient then
Details := ThemeServices.GetElementDetails(ttbButtonHot)
else
Details := ThemeServices.GetElementDetails(ttbButtonNormal);
// Draw the themed background
ThemeServices.DrawElement(Canvas.Handle, Details, ClientRect);
// Determine icon dimensions from ImageList or Glyph
if (ImageIndex >= 0) and (Images <> nil) then
begin
imgW := Images.Width;
imgH := Images.Height;
end
else if (Glyph <> nil) and (not Glyph.Empty) then
begin
imgW := Glyph.Width;
imgH := Glyph.Height;
end
else
begin
imgW := 0;
imgH := 0;
end;
// Draw the icon vertically centered
if imgW > 0 then
begin
xIcon := 2; // left padding
yIcon := (ClientRect.Height - imgH) div 2;
if (ImageIndex >= 0) and (Images <> nil) then
Images.Draw(Canvas, xIcon, yIcon, ImageIndex, Enabled)
else if (Glyph <> nil) and (not Glyph.Empty) then
Canvas.Draw(xIcon, yIcon, Glyph);
xText := xIcon + imgW + 4; // gap between icon and text
end
else
xText := 4; // no icon
// Caption rectangle shifted vertically by OffsetY
r := Rect(xText, FOffsetY, ClientRect.Width - 4, ClientRect.Height + FOffsetY);
// Preserve the button's font settings
Canvas.Font.Assign(Font);
// Draw caption centered vertically inside the shifted rectangle
ts := Canvas.TextStyle;
ts.Alignment := taLeftJustify;
ts.Layout := tlCenter;
Canvas.TextRect(r, r.Left, r.Top, Caption, ts);
end;
end.