Recent

Author Topic: Sizing a TRadioGroup at runtime  (Read 6623 times)

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
Sizing a TRadioGroup at runtime
« on: March 12, 2007, 02:48:27 pm »
Hi,

For a new component I'm designing, I create a TRadioGroup at runtime and place it on a panel.
Now the radiogroup holds two items in two columns.
The item "captions"  are resource strings as is the RadioGroup's caption. These then may be altered by translations.

Now I want to "sculpt" the RadioGroup so that is is as small as possible (widht, height), whilst ensuring that all captions are still readable.

So, I thougth: I need to know the width and height of each item, including the radiobutton, the height of the radiogroup.caption etc, all OS independent.
This is where I get stuck.
TRadioGroup AFAIK provides no info about that.

Anybody got some suggestions as on how to approach this?

Bart

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: Sizing a TRadioGroup at runtime
« Reply #1 on: March 30, 2007, 03:06:13 am »
Your idea is interesting... You have Canvas.TextHeight and Canvas.TextWidth to measure the strings.

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
RE: Sizing a TRadioGroup at runtime
« Reply #2 on: April 15, 2007, 05:35:18 pm »
But TRadioGroup has no canvas...
I decided to use 2 radiobuttons and a bevel instead. This works fine for me right now.

Bart

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: Sizing a TRadioGroup at runtime
« Reply #3 on: April 22, 2007, 02:09:49 pm »
Maybe the ancestor has Canvas property protected.

  type
    trg = class(tcustomradiogroup)
  end;

var
  rg:trg;

implementation

procedure TForm1.FormCreate(Sender: TObject);
var
  i :integer;
begin
  rg := trg.create(self);
  rg.align := alleft;
  rg.parent := form1;
  rg.caption := 'custom_radio_group';
  rg.items.add('one');
  rg.items.add('two');
  rg.items.add('threeeeeeeeeeeeeeeeeee');
  rg.items.add('four');
  rg.items.add('five');
  rg.items.add('six');
  rg.items.add('seven');
  rg.width := 40 + rg.canvas.textwidth(rg.items[0]);
  for i := 1 to rg.items.count -1 do
      if (40 + rg.canvas.textwidth(rg.items) > rg.width) then
         rg.width := 40 + rg.canvas.textwidth(rg.items);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  rg.free;
end;

 

TinyPortal © 2005-2018