-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRoundedGroupBoxUnit.pas
More file actions
288 lines (252 loc) · 9.19 KB
/
RoundedGroupBoxUnit.pas
File metadata and controls
288 lines (252 loc) · 9.19 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
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
unit RoundedGroupBoxUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Winapi.GDIPOBJ, Winapi.GDIPAPI, System.UITypes;
type
TControlAccess = class(Vcl.Controls.TControl);
TRoundedGroupBox = class(TCustomControl)
private
FBorderRadius: Integer;
FBorderThickness: Single;
FBorderColor: TColor;
FBackgroundColor: TColor;
FShowCaption: Boolean;
procedure SetBorderRadius(const Value: Integer);
procedure SetBorderThickness(const Value: Single);
procedure SetBorderColor(const Value: TColor);
procedure SetBackgroundColor(const Value: TColor);
procedure SetShowCaption(const Value: Boolean);
function ColorToGPColor(const Value: TColor): TGPColor;
function VclFontStyleToGPFontStyle(AStyle: TFontStyles): Integer;
procedure UpdateRegion;
protected
procedure Paint; override;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure Resize; override;
procedure CreateWnd; override;
public
constructor Create(AOwner: TComponent); override;
published
property Caption;
property BorderRadius: Integer read FBorderRadius write SetBorderRadius default 10;
property BorderThickness: Single read FBorderThickness write SetBorderThickness;
property BorderColor: TColor read FBorderColor write SetBorderColor default $00CCCCCC;
property BackgroundColor: TColor read FBackgroundColor write SetBackgroundColor default clWhite;
property ShowCaption: Boolean read FShowCaption write SetShowCaption default True;
property Font;
property ParentFont;
property Enabled;
property Visible;
property Anchors;
property Align;
property Padding;
property Color;
property ParentColor;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('FluidVCL', [TRoundedGroupBox]);
end;
constructor TRoundedGroupBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csAcceptsControls, csOpaque];
Width := 200;
Height := 150;
FBorderRadius := 10;
FBorderThickness := 1.5; // Slightly thicker default for better visibility
FBorderColor := $00CCCCCC;
FBackgroundColor := clWhite;
FShowCaption := True;
Padding.Left := 10;
Padding.Top := 25;
Padding.Right := 10;
Padding.Bottom := 10;
end;
procedure TRoundedGroupBox.UpdateRegion;
var
R: TRect;
hRgn: Winapi.Windows.HRGN;
begin
if not HandleAllocated then Exit;
R := ClientRect;
{
We create the region slightly larger (Right+1, Bottom+1) than the control
or exactly at the boundary. To avoid the OS clipping our anti-aliased
GDI+ border, the border must be drawn slightly inside this region.
}
hRgn := Winapi.Windows.CreateRoundRectRgn(R.Left, R.Top, R.Right + 1, R.Bottom + 1, FBorderRadius * 2, FBorderRadius * 2);
if hRgn <> 0 then
begin
if Winapi.Windows.SetWindowRgn(Handle, hRgn, True) = 0 then
Winapi.Windows.DeleteObject(hRgn);
end;
end;
procedure TRoundedGroupBox.CreateWnd;
begin
inherited;
UpdateRegion;
end;
procedure TRoundedGroupBox.Resize;
begin
inherited;
UpdateRegion;
end;
function TRoundedGroupBox.VclFontStyleToGPFontStyle(AStyle: TFontStyles): Integer;
var
StyleValue: Integer;
begin
StyleValue := 0;
if (fsBold in AStyle) then StyleValue := StyleValue + 1;
if (fsItalic in AStyle) then StyleValue := StyleValue + 2;
if (fsUnderline in AStyle) then StyleValue := StyleValue + 4;
if (fsStrikeOut in AStyle) then StyleValue := StyleValue + 8;
Result := StyleValue;
end;
function TRoundedGroupBox.ColorToGPColor(const Value: TColor): TGPColor;
var
RGBValue: LongInt;
begin
RGBValue := ColorToRGB(Value);
Result := MakeColor(255, GetRValue(RGBValue), GetGValue(RGBValue), GetBValue(RGBValue));
end;
procedure TRoundedGroupBox.Paint;
var
Graphics: TGPGraphics;
BorderPath, FillPath: TGPGraphicsPath;
GPRect, LayoutRect, MeasureRect, BoundingBox: TGPRectF;
Pen: TGPPen;
Brush, FontBrush: TGPSolidBrush;
GPFont: TGPFont;
ParentBg: TColor;
CaptionOffset, TextWidth, TextPadding, D, Inset: Single;
CaptionText: WideString;
begin
Graphics := TGPGraphics.Create(Canvas.Handle);
try
Graphics.SetSmoothingMode(SmoothingModeAntiAlias);
Graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
if (Parent <> nil) then ParentBg := TControlAccess(Parent).Color else ParentBg := clBtnFace;
Graphics.Clear(ColorToGPColor(ParentBg));
CaptionText := WideString(Caption);
GPFont := nil;
TextWidth := 0;
TextPadding := 6;
if FShowCaption and (CaptionText <> '') then
begin
GPFont := TGPFont.Create(WideString(Font.Name), Font.Size, VclFontStyleToGPFontStyle(Font.Style), UnitPoint);
MeasureRect.X := 0; MeasureRect.Y := 0; MeasureRect.Width := Width; MeasureRect.Height := Height;
Graphics.MeasureString(CaptionText, -1, GPFont, MeasureRect, nil, BoundingBox);
TextWidth := BoundingBox.Width;
end;
CaptionOffset := 0;
if Assigned(GPFont) then CaptionOffset := BoundingBox.Height / 2;
{
CRITICAL ADJUSTMENT:
GDI+ pens draw centered on the path. To prevent the outer half of the pen
from being clipped by the Window Region at the Right and Bottom, we
must inset the rectangle by the full border thickness.
}
Inset := FBorderThickness + 1;
GPRect.X := Inset / 2;
GPRect.Y := CaptionOffset + (Inset / 2);
GPRect.Width := Width - Inset;
GPRect.Height := Height - GPRect.Y - (Inset / 2);
D := FBorderRadius * 2;
if D < 1 then D := 1;
// Fill the background
FillPath := TGPGraphicsPath.Create;
try
FillPath.AddArc(GPRect.X, GPRect.Y, D, D, 180, 90);
FillPath.AddArc(GPRect.X + GPRect.Width - D, GPRect.Y, D, D, 270, 90);
FillPath.AddArc(GPRect.X + GPRect.Width - D, GPRect.Y + GPRect.Height - D, D, D, 0, 90);
FillPath.AddArc(GPRect.X, GPRect.Y + GPRect.Height - D, D, D, 90, 90);
FillPath.CloseFigure;
Brush := TGPSolidBrush.Create(ColorToGPColor(FBackgroundColor));
Graphics.FillPath(Brush, FillPath);
Brush.Free;
finally
FillPath.Free;
end;
// Draw the border path
BorderPath := TGPGraphicsPath.Create;
try
if Assigned(GPFont) and (TextWidth > 0) then
begin
BorderPath.AddArc(GPRect.X, GPRect.Y, D, D, 180, 90);
// Top line segment 1 (before text)
BorderPath.AddLine(GPRect.X + FBorderRadius, GPRect.Y, FBorderRadius + TextPadding, GPRect.Y);
// Start new figure after text gap
BorderPath.StartFigure;
BorderPath.AddLine(FBorderRadius + TextPadding + TextWidth + TextPadding, GPRect.Y, GPRect.X + GPRect.Width - FBorderRadius, GPRect.Y);
BorderPath.AddArc(GPRect.X + GPRect.Width - D, GPRect.Y, D, D, 270, 90);
BorderPath.AddArc(GPRect.X + GPRect.Width - D, GPRect.Y + GPRect.Height - D, D, D, 0, 90);
BorderPath.AddArc(GPRect.X, GPRect.Y + GPRect.Height - D, D, D, 90, 90);
BorderPath.AddLine(GPRect.X, GPRect.Y + GPRect.Height - FBorderRadius, GPRect.X, GPRect.Y + FBorderRadius);
end
else
begin
BorderPath.AddArc(GPRect.X, GPRect.Y, D, D, 180, 90);
BorderPath.AddArc(GPRect.X + GPRect.Width - D, GPRect.Y, D, D, 270, 90);
BorderPath.AddArc(GPRect.X + GPRect.Width - D, GPRect.Y + GPRect.Height - D, D, D, 0, 90);
BorderPath.AddArc(GPRect.X, GPRect.Y + GPRect.Height - D, D, D, 90, 90);
BorderPath.CloseFigure;
end;
Pen := TGPPen.Create(ColorToGPColor(FBorderColor), FBorderThickness);
Pen.SetLineJoin(LineJoinRound);
Graphics.DrawPath(Pen, BorderPath);
Pen.Free;
if Assigned(GPFont) then
begin
FontBrush := TGPSolidBrush.Create(ColorToGPColor(Font.Color));
try
LayoutRect.X := FBorderRadius + TextPadding;
LayoutRect.Y := 0;
LayoutRect.Width := TextWidth + 5;
LayoutRect.Height := BoundingBox.Height;
Graphics.DrawString(CaptionText, -1, GPFont, LayoutRect, nil, FontBrush);
finally
FontBrush.Free;
end;
end;
finally
BorderPath.Free;
if Assigned(GPFont) then GPFont.Free;
end;
finally
Graphics.Free;
end;
end;
procedure TRoundedGroupBox.CMTextChanged(var Message: TMessage);
begin
Invalidate;
end;
procedure TRoundedGroupBox.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
Message.Result := 1;
end;
procedure TRoundedGroupBox.SetBackgroundColor(const Value: TColor);
begin
if FBackgroundColor <> Value then begin FBackgroundColor := Value; Invalidate; end;
end;
procedure TRoundedGroupBox.SetBorderColor(const Value: TColor);
begin
if FBorderColor <> Value then begin FBorderColor := Value; Invalidate; end;
end;
procedure TRoundedGroupBox.SetBorderRadius(const Value: Integer);
begin
if FBorderRadius <> Value then begin FBorderRadius := Value; UpdateRegion; Invalidate; end;
end;
procedure TRoundedGroupBox.SetBorderThickness(const Value: Single);
begin
if FBorderThickness <> Value then begin FBorderThickness := Value; Invalidate; end;
end;
procedure TRoundedGroupBox.SetShowCaption(const Value: Boolean);
begin
if FShowCaption <> Value then begin FShowCaption := Value; Invalidate; end;
end;
end.