Solved

How to make a Radio Button auto-size?

Posted on 2004-09-04
5
690 Views
Last Modified: 2010-04-05
Anybody here knows how to make a radio button auto-size like TLabel has?
0
Comment
Question by:edeaux
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
LRHGuy earned 125 total points
ID: 11983083
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
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11984172
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is there a maximum column width (in pixels) in a TListView? 22 102
code issue 8 99
When I am typing into a TEdit last word repeats 7 117
Base1 Encode/Decode 3 67
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now