Disable form resizing in delphi

后端 未结 3 1443
渐次进展
渐次进展 2021-02-18 22:49

Is there any way to stop the user resizing the form?

Currently I am using:

When form size changed....

MainForm.Height := 761;
MainForm.Width := 7         


        
3条回答
  •  滥情空心
    2021-02-18 23:15

    If you want your form to not resize at all, then setting the form border style to bsSingle is the right thing to do, as then the mouse cursor will not change to one of the sizing cursors when moved over the form borders, so it is obvious to the user that this form can not be resized.

    If you want to set a minimum and / or a maximum size for the form, then bsSizeable is the correct border style, and you can use the Constraints of the form to specify the limits. There is however the problem that the Constraints property doesn't prevent the resizing of the form, it only causes the sizes to be adjusted after the fact so that the limits are not violated. This will have the negative side effect that sizing the form with the left or upper border will move it. To prevent this from happening you need to prevent the resizing in the first place. Windows sends the WM_GETMINMAXINFO message to retrieve the minimum and maximum tracking sizes for a top level window. Handling this and returning the correct constraints fixes the moving form issue:

    type
      TForm1 = class(TForm)
      private
        procedure WMGetMinMaxInfo(var AMsg: TWMGetMinMaxInfo);
          message WM_GETMINMAXINFO;
      end;
    
    // ...
    
    procedure TForm1.WMGetMinMaxInfo(var AMsg: TWMGetMinMaxInfo);
    begin
      inherited;
      with AMsg.MinMaxInfo^ do begin
        ptMinTrackSize := Point(Constraints.MinWidth, Constraints.MinHeight);
        ptMaxTrackSize := Point(Constraints.MaxWidth, Constraints.MaxHeight);
      end;
    end;
    

提交回复
热议问题