FIX: MinimumSize and MaximumSize properties are not obeyed when window is an MDI child (327824)



The information in this article applies to:

  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q327824

SYMPTOMS

The size of a Multiple Document Interface (MDI) child form is not constrained by MinimumSize and MaximumSize properties at run time, although the boundaries are respected at design time.

WORKAROUND

To work around this problem, you can paste the following sample code in the Form class of your MDI child. This code intercepts the GetMinMaxInfo window message, tells the message what the MinimumSize and MaixmumSize properties are, and then passes the edited message along:

Visual Basic .NET
    Private Const WM_GETMINMAXINFO As Integer = &H24

    Private Structure POINTAPI
        Dim x As Integer
        Dim y As Integer
    End Structure

    Private Structure MINMAXINFO
        Dim ptReserved As POINTAPI
        Dim ptMaxSize As POINTAPI
        Dim ptMaxPosition As POINTAPI
        Dim ptMinTrackSize As POINTAPI
        Dim ptMaxTrackSize As POINTAPI
    End Structure


    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If Me.IsMdiChild Then
            Select Case m.Msg
                Case WM_GETMINMAXINFO
                    Dim mmi As MINMAXINFO = CType(m.GetLParam(GetType(MINMAXINFO)), MINMAXINFO)
                    mmi.ptMinTrackSize.x = Me.MinimumSize.Width
                    mmi.ptMinTrackSize.y = Me.MinimumSize.Height

                    If Not (Me.MaximumSize.Width = 0 And Me.MaximumSize.Height = 0) Then
                        mmi.ptMaxTrackSize.x = Me.MaximumSize.Width
                        mmi.ptMaxTrackSize.y = Me.MaximumSize.Height
                    End If
                    System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, m.LParam, True)
            End Select
        End If
        MyBase.WndProc(m)
    End Sub
				
Visual C# .NET
    private const long WM_GETMINMAXINFO = 0x24;

    public struct POINTAPI
    {
	    public int x;
    	    public int y;
    }

    public struct MINMAXINFO
    {
	    public POINTAPI ptReserved; 
	    public POINTAPI ptMaxSize; 
	    public POINTAPI ptMaxPosition;
	    public POINTAPI ptMinTrackSize; 
	    public POINTAPI ptMaxTrackSize;
    }

    protected override void WndProc(ref System.Windows.Forms.Message m )
    {
	    if (this.IsMdiChild)
	    {
		    if (m.Msg == WM_GETMINMAXINFO)
		    {
			    MINMAXINFO mmi = (MINMAXINFO)m.GetLParam(typeof(MINMAXINFO));
			    mmi.ptMinTrackSize.x = this.MinimumSize.Width;
			    mmi.ptMinTrackSize.y = this.MinimumSize.Height;
			    if (this.MaximumSize.Width != 0 || this.MaximumSize.Height != 0)
			    {
			    	mmi.ptMaxTrackSize.x = this.MaximumSize.Width;
			    	mmi.ptMaxTrackSize.y = this.MaximumSize.Height;
			    }
			    System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, m.LParam, true);
		    }
	    }
	    base.WndProc(ref m);
    }
				

STATUS

This bug was corrected in .NET Framework, Visual Basic .NET (2003), and Visual C# .NET (2003).

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Visual Studio .NET Windows Forms project in the language of your choice.
  2. On the Project menu, click Add Windows Form. Accept the default name (Form2).
  3. Select Form2, and then press the F4 key to open the properties of Form2.
  4. Set MaximumSize to 300,300.
  5. Set MinimumSize to 150,150.
  6. Try to resize Form2. Notice that the size is are constrained.
  7. Open Form1.
  8. Double-click Form1 to access the code for the Load event.
  9. Add the following code to the form Load event:Visual Basic .NET
            Me.IsMdiContainer = True
            Dim newForm As New Form2()
            newForm.MdiParent = Me
            newForm.Show()
    					
    Visual C# .NET
    	this.IsMdiContainer = true;
    	Form2 newForm = new Form2();
    	newForm.MdiParent = this;
    	newForm.Show();
    					
  10. Press F5 to run the application.
  11. Try to resize the MDI child (Form2) and notice that no limits take effect.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

185733 HOWTO: Limit a Window's Minimum and Maximum Size


Modification Type:MinorLast Reviewed:2/3/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbfix kbWindowsForms KB327824