Microsoft KB Archive/327824

= FIX: MinimumSize and MaximumSize properties are not obeyed when window is an MDI child =

Article ID: 327824

Article Last Modified on 2/3/2006

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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).



Steps to Reproduce the Behavior
 Create a new Visual Studio .NET Windows Forms project in the language of your choice. On the Project menu, click Add Windows Form. Accept the default name (Form2). Select Form2, and then press the F4 key to open the properties of Form2. Set MaximumSize to 300,300. Set MinimumSize to 150,150. Try to resize Form2. Notice that the size is are constrained.</li> Open Form1.</li> Double-click Form1 to access the code for the Load event.</li>  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; </li> Press F5 to run the application.</li> Try to resize the MDI child (Form2) and notice that no limits take effect.</li></ol>

<div class="references_section">