-
Notifications
You must be signed in to change notification settings - Fork 1
/
lzroundedpanel.pas
194 lines (170 loc) · 4.84 KB
/
lzroundedpanel.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
{
LZRoundedPanel
by Lainz
Last modified: 2020-09-06 20:45 GMT-3
Changelog:
- 2020-09-06: Initial version with color and style properties and paint event.
}
unit LZRoundedPanel;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
BGRABitmap, BGRABitmapTypes, Math;
type
TLZRoundedPanel = class;
// Event to draw before the image is sent to canvas
TLZRoundedImagePaintEvent = procedure(const Sender: TLZRoundedPanel;
const Bitmap: TBGRABitmap) of object;
// Control that draws a panel with a rounded border
{ TLZRoundedPanel }
TLZRoundedPanel = class(TCustomControl)
private
FBorderWidth: single;
FFillColor: TColor;
FOnPaintEvent: TLZRoundedImagePaintEvent;
FPenColor: TColor;
FRounding: single;
FBorderStyle: TRoundRectangleOptions;
procedure SetBorderWidth(AValue: single);
procedure SetFillColor(AValue: TColor);
procedure SetPenColor(AValue: TColor);
procedure SetRounding(AValue: single);
procedure SetCustomBorderStyle(AValue: TRoundRectangleOptions);
protected
// The color of the canvas
procedure SetColor(Value: TColor); override;
// The caption
procedure RealSetText(const AValue: TCaption); override;
public
constructor Create(AOwner: TComponent); override;
procedure Paint; override;
published
// Rounding of the borders
property Rounding: single read FRounding write SetRounding;
// Color of the border
property PenColor: TColor read FPenColor write SetPenColor;
// Color of the panel
property FillColor: TColor read FFillColor write SetFillColor;
// Thickness of the border
property BorderWidth: single read FBorderWidth write SetBorderWidth;
// The style of the rounded rectangle
property BorderStyle: TRoundRectangleOptions
read FBorderStyle write SetCustomBorderStyle;
// You can paint before text is drawn and before the bitmap is drawn on canvas
property OnPaintEvent: TLZRoundedImagePaintEvent
read FOnPaintEvent write FOnPaintEvent;
published
property Caption;
property Color;
property Font;
property Align;
property Anchors;
property ChildSizing;
property OnClick;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseEnter;
property OnMouseLeave;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Lainz Controls', [TLZRoundedPanel]);
end;
{ TLZRoundedPanel }
procedure TLZRoundedPanel.SetRounding(AValue: single);
begin
if FRounding = AValue then
Exit;
FRounding := AValue;
Invalidate;
end;
procedure TLZRoundedPanel.SetCustomBorderStyle(AValue: TRoundRectangleOptions);
begin
if FBorderStyle = AValue then
Exit;
FBorderStyle := AValue;
Invalidate;
end;
procedure TLZRoundedPanel.SetFillColor(AValue: TColor);
begin
if FFillColor = AValue then
Exit;
FFillColor := AValue;
Invalidate;
end;
procedure TLZRoundedPanel.SetBorderWidth(AValue: single);
begin
if FBorderWidth = AValue then
Exit;
FBorderWidth := AValue;
Invalidate;
end;
procedure TLZRoundedPanel.SetPenColor(AValue: TColor);
begin
if FPenColor = AValue then
Exit;
FPenColor := AValue;
Invalidate;
end;
procedure TLZRoundedPanel.SetColor(Value: TColor);
begin
inherited SetColor(Value);
Invalidate;
end;
procedure TLZRoundedPanel.RealSetText(const AValue: TCaption);
begin
inherited RealSetText(AValue);
Invalidate;
end;
constructor TLZRoundedPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csAcceptsControls, csParentBackground];
FPenColor := clSilver;
FFillColor := clWhite;
FRounding := 10;
FBorderWidth := 1;
end;
procedure TLZRoundedPanel.Paint;
var
bgra: TBGRABitmap;
textStyle: TTextStyle;
offset: single;
begin
bgra := TBGRABitmap.Create(Width, Height, BGRAPixelTransparent);
if (trunc(FBorderWidth) mod 2 = 0) and (frac(FBorderWidth)=0) then
offset := 0.5
else
offset := 0;
try
// Colors, BorderWidth, Fill and Pen color, BorderStyle
bgra.RoundRectAntialias(trunc(FBorderWidth / 2)-offset, trunc(FBorderWidth / 2)-offset, trunc(Width -
(FBorderWidth / 2))-offset, trunc(Height - (FBorderWidth / 2))-offset, FRounding, FRounding,
FPenColor, FBorderWidth, FFillColor, FBorderStyle);
// OnPaintEvent
if Assigned(FOnPaintEvent) then
FOnPaintEvent(Self, bgra);
bgra.Draw(Canvas, 0, 0, False);
finally
bgra.Free;
end;
// Caption
if Caption <> '' then
begin
textStyle.Alignment := taCenter;
textStyle.Layout := tlCenter;
textStyle.Opaque := False;
textStyle.SystemFont := False;
textStyle.Wordbreak := True;
textStyle.SingleLine := False;
// Add some margins
Canvas.TextRect(Rect(5, 5, Width - 5, Height - 5), 0, 0, Caption, textStyle);
end;
end;
end.