使窗体尺寸最小

本文介绍了一种在窗体调整大小时使面板始终保持在窗体中心的技术,并且确保窗体不会缩小到比面板更小的方法。通过实现特定的消息处理程序和调整面板位置的过程,可以有效地保持用户界面的一致性和可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 当用户改变窗体的尺寸时,这个面板总位于窗体的中心,并且不允许把窗体设得比面板还小.
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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值