BeginDeferWindowPos 和 DeferWindowPos、EndDeferWindowPos 是一组一起使用的函数, 可对一组窗口的位置、大小、Z 序等进行调整, 在 ExtCtrls 单元有用到.
下面先用常规方法实现对 Panel1 中的一组 Button 进行调整, 然后再用上面三个函数重新实现.
用 BeginDeferWindowPos、DeferWindowPos、EndDeferWindowPos 重新实现的代码(窗体和运行效果是一样的):
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; DeferHandle: THandle; begin num := Panel1.ControlCount; L := 10; T := 10; W := (Panel1.ClientWidth - L * (num+1)) div num; H := (Panel1.ClientHeight - T * (num+1)) div num; DeferHandle := BeginDeferWindowPos(num); {准备调整一组窗口} for i := 0 to num - 1 do begin if Panel1.Controls[i] is TButton then begin btn := TButton(Panel1.Controls[i]); DeferHandle := DeferWindowPos(DeferHandle, btn.Handle, HWND_TOP, {此参数决定 Z 序} L, (H + T) * i + T, W, H, {新的位置与大小} SWP_NOZORDER {更多控制, 现在是不改变 Z 序} ); end; end; EndDeferWindowPos(DeferHandle); {实施调整} end; procedure TForm1.RadioButton2Click(Sender: TObject); var num,i: Integer; btn: TButton; L,T,W,H: Integer; DeferHandle: THandle; begin num := Panel1.ControlCount; L := 10; T := 10; W := (Panel1.ClientWidth - L * (num+1)) div num; H := (Panel1.ClientHeight - T * (num+1)) div num; DeferHandle := BeginDeferWindowPos(num); for i := 0 to num - 1 do begin if Panel1.Controls[i] is TButton then begin btn := TButton(Panel1.Controls[i]); DeferHandle := DeferWindowPos(DeferHandle, btn.Handle, HWND_TOP, (W + L) * i + L, T, W, H, SWP_NOZORDER ); end; end; EndDeferWindowPos(DeferHandle); end; end.
标签:BeginDeferWindowPos,num,Windows,end,API,Panel1,btn,DeferHandle,TButton From: https://www.cnblogs.com/lucken2000/p/17396976.html