以一个panel为例
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit4,ExtCtrls, Buttons,Math;
type
TForm3 = class(TForm)
Panel: TPanel;
procedure FormResize(Sender: TObject);
private
{ Private declarations }
procedure CenterPanel;
//处理窗口改变消息
procedure WMWindowposchanging(var Msg:TWMWindowPosChanging);
message WM_WINDOWPOSCHANGING;
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
//使面板始终在窗口中心
procedure TForm3.CenterPanel;
begin
//水平居中
if Panel.Width<ClientWidth then
Panel.Left:=(ClientWidth - Panel.Width)div 2
else
Panel.Left:=0;
//垂直居中
if Panel.Height<ClientHeight then
Panel.Top:=(ClientHeight - Panel.Height) div 2
els