• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

How to make a Radio Button auto-size?

Anybody here knows how to make a radio button auto-size like TLabel has?
0
edeaux
Asked:
edeaux
  • 2
1 Solution
 
LRHGuyCommented:
Try this.   Put it into a unit by itself, then add it to a "package" and install it.

unit RB1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, StdCtrls;

type
  tMyRB=class(tRadioButton)
  private
    fAutoSize:boolean;
  protected
    procedure Adjustbounds;
    procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
    procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
    procedure Loaded; override;
    procedure SetAutoSize(Value: Boolean); override;
  published
    property AutoSize: Boolean read FAutoSize write SetAutoSize;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Custom',[tMyRB]);
end;


procedure tMyRB.AdjustBounds;
var
  DC: HDC;
  Rect: TRect;
  L,W:integer;
begin
  if not (csReading in ComponentState) and FAutoSize then begin
    Rect:=ClientRect;
    DC:=GetDC(0);
    DrawText(DC,PChar(Text),Length(Text),Rect,DT_CALCRECT);
    ReleaseDC(0,DC);
    W:=GetSystemMetrics(SM_CXMENUCHECK);
    Rect.Right:=Rect.Right+W;
    L:=Left;
    SetBounds(L,Top,Rect.Right,Rect.Bottom);  
  end;
end;

procedure tMyRB.CMTextChanged(var Message: TMessage);
begin
  Invalidate;
  AdjustBounds;
end;

procedure tMyRB.CMFontChanged(var Message: TMessage);
begin
  inherited;
  AdjustBounds;
end;

procedure tMyRB.SetAutoSize(Value: Boolean);
begin
  if FAutoSize<>Value then begin
    FAutoSize:=Value;
    AdjustBounds;
  end;
end;

procedure tMyRB.Loaded;
begin
  inherited Loaded;
  AdjustBounds;
end;

end.

0
 
LRHGuyCommented:
I should mention, that once you've installed it, you should select the radio button from the Custom tab in the palette and drop it on your form. Set the autosize property to true, then enjoy!

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now