BeginDeferWindowPos 和 DeferWindowPos、EndDeferWindowPos 是一组一起使用的函数, 可对一组窗口的位置、大小、Z 序等进行调整, 在 ExtCtrls 单元有用到.

下面先用常规方法实现对 Panel1 中的一组 Button 进行调整, 然后再用上面三个函数重新实现.

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.RadioButton1Click(Sender: TObject);
var
  num,i: Integer;
  btn: TButton;
  L,T,W,H: Integer;
begin
  num := Panel1.ControlCount;

  L := 10;
  T := 10;
  W := (Panel1.ClientWidth - L * (num+1)) div num;
  H := (Panel1.ClientHeight - T * (num+1)) div num;

  for i := 0 to num - 1 do
  begin
    if Panel1.Controls[i] is TButton then
    begin
      btn := TButton(Panel1.Controls[i]);
      btn.Left := L;
      btn.Top := (H + T) * i + T;
      btn.Width := W;
      btn.Height := H;
    end;
  end;
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
var
  num,i: Integer;
  btn: TButton;
  L,T,W,H: Integer;
begin
  num := Panel1.ControlCount;

  L := 10;
  T := 10;
  W := (Panel1.ClientWidth - L * (num+1)) div num;
  H := (Panel1.ClientHeight - T * (num+1)) div num;

  for i := 0 to num - 1 do
  begin
    if Panel1.Controls[i] is TButton then
    begin
      btn := TButton(Panel1.Controls[i]);
      btn.Left := (W + L) * i + L;
      btn.Top := T;
      btn.Width := W;
      btn.Height := H;
    end;
  end;
end;

end.

posted on 2014-07-15 11:39  何石-博客  阅读(466)  评论(0编辑  收藏  举报