Solved

How to make a Radio Button auto-size?

Posted on 2004-09-04
5
719 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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