Winforms: how to prevent vertical resizing in VB.NET

Working with winforms I wonder if there is a way to prevent vertical form changes. I would like to allow the user to resize the form in all directions except vertically. Also, I would like to allow vertical resizing up, but not down.

I tried using the maximum size by setting it to: Me.maximumsize = new size (0, me.height)

I set the width to 0 because I want to allow the user to change the width of the form.

Unfortunately this will not work.

Any ideas?

+3
source share
5 answers

, . , , DPI . , . Load. :

protected override void OnLoad(EventArgs e) {
  Screen scr = Screen.FromControl(this);
  this.MinimumSize = new Size(this.MinimumSize.Width, this.Height);
  this.MaximumSize = new Size(scr.WorkingArea.Width, this.Height);
}

, , , , . , WM_NCHITTEST WndProc :

protected override void WndProc(ref Message m) {
  base.WndProc(ref m);
  if (m.Msg == 0x84) {  // Trap WM_NCHITTEST
    switch (m.Result.ToInt32()) {
      case 12: m.Result = (IntPtr)2; break;  // HTTOP to HTCAPTION
      case 13: m.Result = (IntPtr)10; break; // etc..
      case 14: m.Result = (IntPtr)11; break;
      case 15: m.Result = (IntPtr)1; break;
      case 16: m.Result = (IntPtr)10; break;
      case 17: m.Result = (IntPtr)11; break;
    }
  }
}
+8

" " Form_Load:

this.MaximumSize=new System.Drawing.Size(2048, 300);
this.MinimumSize=new System.Drawing.Size(0, 300);

.

+3

. :

Dim originalSize As Integer = Me.Height

Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    If Me.Height <> originalSize Then
        Me.Height = originalSize
    End If
End Sub

, , , .

+1

private void FormLogin_Load(object sender, EventArgs e)
{
     this.MaximumSize = this.Size;
     this.MinimumSize = this.Size;

}
+1
0

Source: https://habr.com/ru/post/1741757/


All Articles