用于 MDI winforms 的 ShowDialog 属性

ShowDialog property for MDI winforms

问题:

如何以 ShowDialog() 格式显示 MDI 子窗体?

我试过的:

private void Add()
        {

            ModuleAddPopUp map = new ModuleAddPopUp();
            map.StartPosition = FormStartPosition.CenterScreen;
            map.ShowDialog();          
        }

执行上述操作后,窗体将中心屏幕显示为弹出窗口,但是当 MDI 未最大化时,我可以将窗体拖到 MDI 之外。

private void Add()
        {
            ModuleAddPopUp map = new ModuleAddPopUp();
            FormFunctions.OpenMdiDataForm(App.Program.GetMainMdiParent(), map);

        }

执行上述操作后,表单显示中心屏幕,不允许将表单拖到 MDI 之外,但充当 map.Show() ,而不是 map.ShowDialog ();

将此代码添加到您的 ModuleAddPopup class:

protected override void WndProc(ref Message message)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MOVE = 0xF010;
    //SC_SIZE = 0XF000 if you also want to prevent them from resizing the form.
    //Add it to the 'if' condition.
    switch (message.Msg)
    {
        case WM_SYSCOMMAND:
            int command = message.WParam.ToInt32() & 0xfff0;
            if (command == SC_MOVE)
                return;
            break;
    }

    base.WndProc(ref message);
}

这是封装在 C# 代码中的本机代码,如 here 中所示。 但是,这将阻止用户将对话框表单移动到任何地方。

然后,在你的主表单中:

private void Add()
{
    ModuleAddPopUp map = new ModuleAddPopUp();
    map.StartPosition = FormStartPosition.CenterParent;
    map.ShowDialog();          
}