Ограничение на изменение размера формы по размеру панели на ней
Автор: Xavier Pacheco
{
Copyright © 1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira
}
unit BlueBackFrm;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls;
type
TBlueBackForm = class(TForm)
pnlMain: TPanel;
bbtnOK: TBitBtn;
bbtnCancel: TBitBtn;
procedure FormResize(Sender: TObject);
private
procedure CenterPanel;
{ Create a message handler for the WM_WINDOWPOSCHANGING message }
procedure WMWindowPosChanging(var Msg: TWMWindowPosChanging);
message WM_WINDOWPOSCHANGING;
end;
var
BlueBackForm: TBlueBackForm;
implementation
uses Math;
{$R *.DFM}
procedure TBlueBackForm.CenterPanel;
{ This procedure centers the main panel horizontally and
vertically inside the form's client area
}
begin
{ Center horizontally }
if pnlMain.Width < ClientWidth then
pnlMain.Left := (ClientWidth - pnlMain.Width) div 2
else
pnlMain.Left := 0;
{ Center vertically }
if pnlMain.Height < ClientHeight then
pnlMain.Top := (ClientHeight - pnlMain.Height) div 2
else
pnlMain.Top := 0;
end;
procedure TBlueBackForm.WMWindowPosChanging(var Msg: TWMWindowPosChanging);
var
CaptionHeight: integer;
begin
{ Calculate the caption height }
CaptionHeight := GetSystemMetrics(SM_CYCAPTION);
{ This procedure does not take into account the width and
height of the form's frame. You can use
GetSystemMetrics() to obtain these values. }
// Prevent window from shrinking smaller then MainPanel's width
Msg.WindowPos^.cx := Max(Msg.WindowPos^.cx, pnlMain.Width + 20);
// Prevent window from shrinking smaller then MainPanel's width
Msg.WindowPos^.cy := Max(Msg.WindowPos^.cy, pnlMain.Height + 20 +
CaptionHeight);
inherited;
end;
procedure TBlueBackForm.FormResize(Sender: TObject);
begin
CenterPanel; // Center MainPanel when the form is resized.
end;
end.
|