Author: Tomas Rutkauskas
How to draw a TRadioGroup without a frame
Answer:
1 unit GSRadioGroup;
2
3 interface
4
5 uses
6 Windows, SysUtils, Classes, Forms, ExtCtrls;
7
8 type
9 TGSRadioGroup = class(TRadioGroup)
10 private
11 FBorderStyle: TBorderStyle;
12 FValues: TStrings;
13 protected
14 procedure SetBorderStyle(Value: TBorderStyle);
15 procedure Paint; override;
16 function GetValues: TStrings;
17 procedure SetValues(Value: TStrings);
18 public
19 constructor Create(AOwner: TComponent); override;
20 destructor Destroy; override;
21 published
22 property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle
23 default bsNone;
24 property Values: TStrings read GetValues write SetValues;
25 end;
26
27 procedure register;
28
29 implementation
30
31 constructor TGSRadioGroup.Create(AOwner: TComponent);
32 begin
33 inherited Create(AOwner);
34 FBorderStyle := bsNone;
35 FValues := TStringList.Create;
36 end;
37
38 destructor TGSRadioGroup.Destroy;
39 begin
40 FValues.Free;
41 inherited Destroy;
42 end;
43
44 function TGSRadioGroup.GetValues;
45 begin
46 Result := FValues;
47 end;
48
49 procedure TGSRadioGroup.SetValues(Value: TStrings);
50 begin
51 if Value <> FValues then
52 begin
53 FValues.Assign(Value);
54 end;
55 end;
56
57 procedure TGSRadioGroup.SetBorderStyle(Value: TBorderStyle);
58 begin
59 if FBorderStyle <> Value then
60 begin
61 FBorderStyle := Value;
62 RecreateWnd;
63 end;
64 end;
65
66 procedure TGSRadioGroup.Paint;
67 var
68 c: Integer;
69 diff: Integer;
70 H: Integer;
71 R: TRect;
72 begin
73 if FBorderStyle = bsSingle then
74 inherited Paint
75 else
76 begin
77 with Canvas do
78 begin
79 if Text <> EmptyStr then
80 begin
81 Font := Self.Font;
82 H := TextHeight('0');
83 R := Rect(8, 0, 0, H);
84 DrawText(Handle, PChar(Text), Length(Text), R, DT_LEFT or DT_SINGLELINE or
85 DT_CALCRECT);
86 Brush.Color := Color;
87 DrawText(Handle, PChar(Text), Length(Text), R, DT_LEFT or DT_SINGLELINE);
88 end
89 else
90 begin
91 if ControlCount > 0 then
92 begin
93 diff := Controls[0].Top;
94 for c := 0 to ControlCount - 1 do
95 begin
96 Controls[c].Top := Controls[c].Top - diff;
97 end;
98 {You may want to adjust the height here}
99 end;
100 end;
101 end;
102 end;
103 end;
104
105 procedure register;
106 begin
107 RegisterComponents('Garlin', [TGSRadioGroup]);
108 end;
109
110 end.
|