Solved

How to make a Radio Button auto-size?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

23 Experts available now in Live!

Get 1:1 Help Now