VCL 中的 Windows API 函數(6): BeginDeferWindowPos

來源:互聯網
上載者:User
BeginDeferWindowPos 和 DeferWindowPos、EndDeferWindowPos 是一組一起使用的函數, 可對一組視窗的位置、大小、Z 序等進行調整, 在 ExtCtrls 單元有用到.

下面先用常規方法實現對 Panel1 中的一組 Button 進行調整, 然後再用上面三個函數重新實現.

本例:


代碼檔案:
unit Unit1;interfaceuses  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.

表單檔案:

object Form1: TForm1  Left = 0  Top = 0  Caption = 'Form1'  ClientHeight = 220  ClientWidth = 307  Color = clBtnFace  Font.Charset = DEFAULT_CHARSET  Font.Color = clWindowText  Font.Height = -11  Font.Name = 'Tahoma'  Font.Style = []  OldCreateOrder = False  PixelsPerInch = 96  TextHeight = 13  object Panel1: TPanel    Left = 8    Top = 8    Width = 289    Height = 161    Caption = 'Panel1'    TabOrder = 0    object Button1: TButton      Left = 152      Top = 72      Width = 75      Height = 25      Caption = 'Button1'      TabOrder = 0    end    object Button2: TButton      Left = 160      Top = 80      Width = 75      Height = 25      Caption = 'Button2'      TabOrder = 1    end    object Button3: TButton      Left = 168      Top = 88      Width = 75      Height = 25      Caption = 'Button3'      TabOrder = 2    end    object Button4: TButton      Left = 176      Top = 96      Width = 75      Height = 25      Caption = 'Button4'      TabOrder = 3    end  end  object RadioButton1: TRadioButton    Left = 50    Top = 183    Width = 113    Height = 17    Caption = 'RadioButton1'    TabOrder = 1    OnClick = RadioButton1Click  end  object RadioButton2: TRadioButton    Left = 184    Top = 183    Width = 113    Height = 17    Caption = 'RadioButton2'    TabOrder = 2    OnClick = RadioButton2Click  endend

用 BeginDeferWindowPos、DeferWindowPos、EndDeferWindowPos 重新實現的代碼(表單和運行效果是一樣的):

unit Unit1;interfaceuses  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.
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.