Board index » delphi » Help - no Autosize

Help - no Autosize

I create windowed components at runtime, set Autosize to true, then set
their caption or text value and make them visible and they are always
the correct size, ie all text is visible.  

Or at least with TEdits and TLabels it is.  However this doesn't work
with TRadioButtons and TCheckBoxes.  Is there a way I can calculate the
required width of both of these controls and set their width to that
value?  Or is there another way of implementing Autosize functionality?

Thanks in advance,
Paul Ritchie.

 

Re:Help - no Autosize


I rolled my own try something like this

  TSTResizingCheckBox = class(TCheckBox)
  private
    FAutoSize: boolean;
    procedure SetAutoSize(Value: boolean);
  protected
    procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
    procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  public
    constructor Create(AOwner: TComponent); override;
    procedure CreateWnd; override;
  published
    property AutoSize: boolean read FAutoSize write SetAutoSize default
True;
  end;

constructor TSTResizingCheckBox.Create(AOwner: TComponent);
begin
  inherited;
  FAutoSize := True;
  Caption := 'CheckBox';
  Height := 16;
end;

procedure TSTResizingCheckbox.CreateWnd;
begin
  inherited;
  Perform(CM_TEXTCHANGED, 0, 0);
end;

procedure TSTResizingCheckbox.SetAutoSize(Value: boolean);
begin
  if Value <> FAutoSize then
    begin
      FAutoSize := Value;
      Perform(CM_TEXTCHANGED, 0, 0);
    end;
end;

procedure TSTResizingCheckbox.CMTextChanged(var Message: TMessage);
var
  r: TRect;
  dc: HDC;
  h: HWND;
  oldFont: HFONT;
begin
  inherited;
  if Assigned(Parent) and AutoSize then
    begin
      r := ClientRect;
      r.Right := 0;

      h := Handle;
      dc := GetDeviceContext(h);

      oldFont := SelectObject(dc, Font.Handle);

      DrawText(dc, PChar(Caption), Length(Caption), r, DT_NOCLIP or
DT_EXPANDTABS or DT_CALCRECT);

      SelectObject(dc, oldFont);
      ReleaseDC(h, dc);

      if Height > r.Bottom then
        SetBounds(Left, Top, r.right + 20, Height)
      else
        SetBounds(Left, Top, r.right + 20, r.bottom);
    end;
end;

procedure TSTResizingCheckbox.CMFontChanged(var Message: TMessage);
begin
  inherited;
  Perform(CM_TEXTCHANGED, 0, 0);
end;

Re:Help - no Autosize


Quote
In article <3574E16F.54CE2...@xtra.co.nz>, Paul Ritchie wrote:
> with TRadioButtons and TCheckBoxes.  Is there a way I can calculate the
> required width of both of these controls and set their width to that
> value?  Or is there another way of implementing Autosize functionality?

Assuming that the form and the radio/check buttons have the same font then
use

myWidth := Canvas.TextWidth('the width of my string');

then addon a suitable ammount for the radio/checkbox bit

use the TextExtent

 Taz Higgins...

Re:Help - no Autosize


Quote
Rob Whitehead wrote:
> I rolled my own try something like this...

Thanks Rob for an excellent answer.

Paul.

Other Threads