Solved

How to make a Radio Button auto-size?

Posted on 2004-09-04
5
708 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 125
add combobox item based on numbers 9 154
Delphi - replicating a form 8 81
Firemonkey BASS_Init into a thread 17 30
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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