【问题标题】:Delphi. Set button position when form is created德尔福。创建表单时设置按钮位置
【发布时间】:2014-03-05 09:05:09
【问题描述】:

我有一些代码,它将用按钮创建表单,但是当表单显示按钮位置错误时。

var
  Form: TForm;
  Button: TButton;
begin
  Form := TForm.Create(Self);
  try
    Button := TButton.Create(Form);
    Button.Parent := Form;
    Button.Left := 8;
    Button.Top := 8;
    Button.Height := 185;
    Button.Width := 292;
    Button.Anchors := [];
    Form.ShowModal;
  finally
    FreeAndNil(Form);
  end;

知道如何解决这个问题吗?

【问题讨论】:

    标签: winforms button position delphi-xe2 delphi-xe


    【解决方案1】:

    不要清除Anchors。这会导致按钮在调整窗体大小时四处移动。将默认的 Anchors 设置为 [akLeft,akTop],这样按钮将遵循您的 Left/Top 值。

    【讨论】:

    • 那么您没有正确管理您的Anchors。必须启用akLeft 才能使Left 正常工作,并且必须启用akTop 才能使Top 正常工作。
    • 好的,所以我需要设置下一个属性Button.Anchors := [akBottom, akRight];,但是按钮位置错误。创建表单时,她有位置(-8,-30)。但是当我创建并显示表单时,然后创建按钮 - 在正确位置创建按钮
    • 这不是我说的。改为设置Button.Anchors := [akLeft, akTop];,无论如何这是默认设置。然后阅读Anchors 的实际含义以及它们的工作原理。
    【解决方案2】:

    当我在创建控件之前使用 HandleNeeded 时

    Form.HandleNeeded
    

    当表单显示时 - 按钮位置正确

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多