Rachel
Rachel

Reputation: 132618

How to prevent a dialog from leaving parent bounds?

I have a window that shows a draggable dialog, however I don't want the dialog to leave the bounds of the parent window.

using (var dialog = new MyDialog())
{
    if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
    {
        // do something
    }
}

How can I accomplish this?

Upvotes: 1

Views: 518

Answers (1)

Dmitry
Dmitry

Reputation: 14059

You could try to overwrite the dialog form's WndProc method and monitor the WM_WINDOWPOSCHANGING system message for a new position and size:

public class DialogForm : Form
{
    private struct WindowPos
    {
        public IntPtr hwnd;
        public IntPtr hwndInsertAfter;
        public int x;
        public int y;
        public int cx;
        public int cy;
        public uint flags;
    }

    public DialogForm()
    {
    }

    protected override void WndProc(ref Message m)
    {
        const int WM_WINDOWPOSCHANGING = 0x0046;
        if (m.Msg == WM_WINDOWPOSCHANGING)
        {
            WindowPos mwp = (WindowPos)Marshal.PtrToStructure(m.LParam, typeof(WindowPos));
            if (mwp.hwnd == Handle)
            {
                const int SWP_NOMOVE = 0x0002;
                const int SWP_NOSIZE = 0x0001;
                int x = mwp.x;
                int y = mwp.y;
                if (x < Owner.Left || y < Owner.Top || x + mwp.cx > Owner.Right || y + mwp.cy > Owner.Bottom)
                {
                    m.Result = IntPtr.Zero;
                    mwp.flags |= SWP_NOMOVE | SWP_NOSIZE;
                    Marshal.StructureToPtr(mwp, m.LParam, true);
                }
            }
        }
        base.WndProc(ref m);
    }
}

EDIT. For more smoothness use this method:

protected override void WndProc(ref Message m)
{
    const int WM_WINDOWPOSCHANGING = 0x0046;
    if (m.Msg == WM_WINDOWPOSCHANGING)
    {
        WindowPos mwp = (WindowPos)Marshal.PtrToStructure(m.LParam, typeof (WindowPos));
        if (mwp.hwnd == Handle)
        {
            int x = mwp.x;
            int y = mwp.y;
            if (x < Owner.Left || y < Owner.Top || x + mwp.cx > Owner.Right || y + mwp.cy > Owner.Bottom)
            {
                bool resizing = mwp.cx != Width || mwp.cy != Height;
                if (resizing)
                {
                    if (x < Owner.Left)
                    {
                        mwp.x = x = Owner.Left;
                        mwp.cx = Width;
                    }
                    if (y < Owner.Top)
                    {
                        mwp.y = y = Owner.Top;
                        mwp.cy = Height;
                    }

                    if (mwp.cx > Owner.Right - x)
                        mwp.cx = Owner.Right - x;
                    if (mwp.cy > Owner.Bottom - y)
                        mwp.cy = Owner.Bottom - y;
                }
                else
                {
                    if (x < Owner.Left)
                        mwp.x = Owner.Left;
                    else if (x > Owner.Right - mwp.cx)
                        mwp.x = Owner.Right - mwp.cx;

                    if (y < Owner.Top)
                        mwp.y = Owner.Top;
                    else if (y > Owner.Bottom - mwp.cy)
                        mwp.y = Owner.Bottom - mwp.cy;
                }
                m.Result = IntPtr.Zero;
                Marshal.StructureToPtr(mwp, m.LParam, true);
            }
        }
    }
    base.WndProc(ref m);
}

Upvotes: 2

Related Questions