如何在没有按钮相互重叠的情况下更改按钮的大小?

时间:2012-11-19 06:59:09

标签: delphi delphi-xe

我有一个对话框,为用户提供3个选项,是,否,取消..
我将是,否更改为新的标题武器1和武器2.我还更改按钮的宽度以匹配新标题的宽度。但如果新标题太长,按钮就会超过一圈。我该如何解决这个问题?

  Weapon1 := FMyPlayers.player[FGamePlay.chartoattackwith].Values['Attack1'];
  Range1 :=  FMyPlayers.player[FGamePlay.chartoattackwith].Values['Range1'];
  Weapon2 := FMyPlayers.player[FGamePlay.chartoattackwith].Values['Attack2'];
  Range2 :=  FMyPlayers.player[FGamePlay.chartoattackwith].Values['Range2'];
    with CreateMessageDialog('Please pick a weapon:', mtConfirmation, mbYesNoCancel) do
        try
          TButton(findcomponent('Yes')).Width := self.Canvas.TextWidth('    '+Weapon1+':'+range1+'    ');
          TButton(findcomponent('No')).Width :=  self.Canvas.TextWidth('    '+Weapon2+':'+range2+'    ');
          TButton(FindComponent('Yes')).Caption := Weapon1+':'+range1;
          TButton(FindComponent('No')).Caption := Weapon2+':'+range2;
         case ShowModal of
           mrYes: AttackValue := '1';
           mrNo:  AttackValue := '2';
           mrCancel: exit;
         end;
        finally
       Free;
     end;

1 个答案:

答案 0 :(得分:3)

插入此部分代码

  TButton(findcomponent('No')).Left := TButton(findcomponent('Yes')).Width + TButton(findcomponent('Yes')).Left;
  TButton(findcomponent('Cancel')).Left := TButton(findcomponent('No')).Width + TButton(findcomponent('No')).Left;
  Width := 2*TButton(findcomponent('Yes')).Left + TButton(findcomponent('Cancel')).Left + TButton(findcomponent('Cancel')).Width;

根据David Heffernan的说法

var
No,Yes,Cancel:TButton;

.....
No := TButton(findcomponent('No'));
Yes := TButton(findcomponent('Yes'));
Cancel := TButton(findcomponent('Cancel'));

No.Left := Yes.Width + Yes.Left;
Cancel.Left := No.Width + No.Left;
Width := 2 * Yes.Left + Cancel.Left + Cancel.Width;