C#
form
snippet

[C#] フォームの幅や高さを固定する

More than 1 year has passed since last update.
//参考
//  フォームのサイズを制限する .NET Tips C#, VB.NET
//  http://dobon.net/vb/dotnet/form/minimumsize.html

//============================================================
//フォームの Load イベントハンドラ

this.MinimumSize = this.Size;


//============================================================
//フォームの幅や高さ固定

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Flags = System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
    const int WM_SIZING = 0x214;

    if (m.Msg == WM_SIZING)
    {
        //フォームの範囲を取得
        RECT rect = (RECT)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(RECT));

        //幅を固定
        if (rect.Right - rect.Left > this.MinimumSize.Width)
        {
            rect.Right = rect.Left + this.MinimumSize.Width;
        }
        //高さを固定
        //if (rect.Bottom - rect.Top > this.MinimumSize.Height)
        //{
        //  rect.Bottom = rect.Top + this.MinimumSize.Height;
        //}

        //フォームの範囲を設定
        System.Runtime.InteropServices.Marshal.StructureToPtr(rect, m.LParam, true);
    }

    base.WndProc(ref m);
}