1

在此处输入图像描述

“人”是 MDI 父窗体 “新建”是 MDI 子窗体

如何阻止“新”被拖到“人”边缘之外?

我发现该代码运行良好

    protected override void OnMove(EventArgs e)
    {
        //
        // Get the MDI Client window reference
        //
        MdiClient mdiClient = null;
        foreach (Control ctl in MdiParent.Controls)
        {
            mdiClient = ctl as MdiClient;
            if (mdiClient != null)
                break;
        }
        //
        // Don't allow moving form outside of MDI client bounds
        //
        if (Left < mdiClient.ClientRectangle.Left)
            Left = mdiClient.ClientRectangle.Left;
        if (Top < mdiClient.ClientRectangle.Top)
            Top = mdiClient.ClientRectangle.Top;
        if (Top + Height > mdiClient.ClientRectangle.Height)
            Top = mdiClient.ClientRectangle.Height - Height;
        if (Left + Width > mdiClient.ClientRectangle.Width)
            Left = mdiClient.ClientRectangle.Width - Width;
        base.OnMove(e);
    }
4

4 回答 4

0

将子窗体 StartPosition 设置为 CenterParent

于 2012-07-06T12:36:51.840 回答
0

隐藏 mdi 容器的滚动条,示例代码:

internal sealed class NonScrollableWindow : NativeWindow
{
    private readonly MdiClient _mdiClient;

    public NonScrollableWindow(MdiClient parent)
    {
        _mdiClient = parent;
        ReleaseHandle();
        AssignHandle(_mdiClient.Handle);
    }
    internal void OnHandleDestroyed(object sender, EventArgs e)
    {
        ReleaseHandle();
    }
    private const int SB_BOTH = 3;
    [DllImport("user32.dll")]
    private static extern int ShowScrollBar(IntPtr hWnd, int wBar, int bShow);
    protected override void WndProc(ref Message m)
    {
        ShowScrollBar(m.HWnd, SB_BOTH, 0);
        base.WndProc(ref m);
    }
}

用法(在 mdi 父加载事件中),

 foreach (MdiClient control in Controls.OfType<MdiClient>())
        {
            if (control != null)
            {
                new NonScrollableWindow(control);
                break;
            }
        }
于 2012-07-06T13:03:15.900 回答
0

MDI 父窗体重新定位新窗体,类似于显示多个窗体级联。要重新定位窗口,您必须在Form_Loading事件处理程序中设置位置。

于 2012-07-06T12:35:33.667 回答
0

在孩子的移动事件中,您可以使用代码来检测当前位置是否超出您想要的位置。您需要事先建立父窗口的顶部、左侧、底部和右侧边缘。

代码如:

BufferWidth = 10; // 10 pixel buffer to edge
// ParentTop is the top Y co-ordinate of the parent window

if (this.location.Y > (ParentTop-BufferWidth))
{
    int LocX = this.Location.X;
    this.location = new Point(LocX, (ParentTop-BufferWidth));
}

您需要对每一面重复此操作。通过提前计算带有缓冲区的边缘,可以更简化代码,因为它们只会在父窗口移动时发生变化。

于 2012-07-06T13:18:41.170 回答