Delphi 限制窗体大小的最大值与最小值

【Delphi】限制窗体大小的最大值与最小值
 
QQ主窗体可以随意拉动,但在拉小时,会在达到某个最小
 
宽度或高度后无法再拉动,这里有2种方法:
 
1):使用VCL窗体控件的Constraints子组件,如下设置:
 
procedure TForm1.FormCreate(Sender: TObject);
begin
 
    //设置窗体容器高度和宽度大小的最大值和最小值,0表示无限制
    Self.Constraints.MaxHeight := 800;
    Self.Constraints.MaxWidth := 800;
    Self.Constraints.MinHeight := 200;
    Self.Constraints.MinWidth := 200;
 
end;
 
上面这种方法有个缺点,就是在鼠标按住窗体左边边沿拉小窗体达到最小值时,窗体会移动,解决办法就是使用下面第二种方法。
 
2):处理windows消息WM_WINDOWPOSCHANGING
 
该消息在窗体大小位置正在改变时触发,代码如下:
 
 
 
TForm1 = class(TForm)
 
private
    { Private declarations }
    procedure WndProc(var Msg: TMessage);override;
 
end;
 
//重载窗体过程
 
procedure TForm1.WndProc(var Msg: TMessage);
 
var
   NewWidth,
   NewHeight: Integer;
begin
 
  case Msg.Msg of
 
     WM_WINDOWPOSCHANGING: //正在改变位置或大小
     begin
         NewWidth := PWindowPos(Msg.LParam)^.cx;
         NewHeight:= PWindowPos(Msg.LParam)^.cy;
         if (NewWidth>0) and (NewHeight>0) then
        if (NewWidth<200)or (NewHeight<200)or (NewWidth>800)or (NewHeight >800)   then
         PWindowPos(Msg.LParam)^.flags := SWP_NOSIZEor SWP_NOMOVE;
      end;
 
   end;
 
   inherited;  
 
end;
posted @ 2022-06-25 15:10  一曲轻扬  阅读(507)  评论(0)    收藏  举报