2

当我们单击父窗体的客户区时,如何强制完全聚焦子窗体?[像 MessageBox 或 Error message 有这样的焦点,所以我们必须点击 messagebox 的 Ok 按钮。]

这是我的代码:

form = new Form2(); 
form.MdiParent = this; 
form.ShowDialog(ParentForm);

但这给了我错误:

不是顶级表单的表单不能显示为模式对话框。在调用 showDialog 之前从任何父窗体中删除窗体。

4

2 回答 2

1

原始答案 - 对于非 MDIChild 使用 ShowDialog 方法使子窗体模态。

ChildForm.ShowDialog (ParentForm);

要将 MDI 子窗体保持在顶部:处理父窗体上的 MDIChildActivate 事件,并在其中将要保持可见的子窗体设置为活动:。在本例中,Form2 是模态窗体,Form3 是另一个用于测试的窗体。

private Form2 frm2;
private Form3 frm3;
private void Form1_Load(object sender, EventArgs e)
{
    frm2=new Form2();
    frm2.MdiParent=this ;
    frm2.Show();

    frm3= new Form3() ;
    frm3.MdiParent=this;
    frm3.Show();
}

private void Form1_MdiChildActivate(object sender, EventArgs e)
{
        frm2.Activate();
}
于 2013-06-22T11:50:36.580 回答
0

我想你想MessageBoxMDI Parent Formwith中加入一些东西TopLevel = false。为了使效果看起来像窗口显示的真实效果ShowDialog(),我认为这是唯一需要您为您创建自定义表单的解决方案MDI child form

public class ChildForm : Form
{
    [DllImport("user32")]
    private static extern int FlashWindowEx(FLASHWINFO flashInfo);
    struct FLASHWINFO
    {
        public int cbSize;
        public IntPtr hwnd;
        public uint flags;
        public int count;
        public int timeOut;
    }
    protected override void WndProc(ref Message m)
    {            
        if (ParentForm != null)
        {                
            ChildForm dialog = ((Form1)ParentForm).Dialog;
            if (dialog != this && dialog!=null)
            {                    
                if (m.Msg == 0x84) //WM_NCHITTEST
                {
                    if (MouseButtons == MouseButtons.Left)
                    {
                        Flash(dialog.Handle);
                    }
                    return;
                }                    
            }
        }
        base.WndProc(ref m);
    }
    private void Flash(IntPtr handle)
    {
        FLASHWINFO flashInfo = new FLASHWINFO()
        {
            hwnd = handle,
            count = 9,
            cbSize = Marshal.SizeOf(typeof(FLASHWINFO)),
            flags = 3,
            timeOut = 50
        };
        FlashWindowEx(flashInfo); 
    }
    public void Flash()
    {
        Flash(Handle);
    }
    //This is to disable Tab when the Dialog is shown
    protected override bool ProcessTabKey(bool forward)
    {
        if(((Form1)ParentForm).Dialog == this) return true;
        return base.ProcessTabKey(forward);
    }
}
//Here is your MDI parent form
public class Form1 : Form {
   public Form1(){
       InitializeComponent();
       IsMdiContainer = true;
       f1 = new ChildForm();
       f2 = new ChildForm();
       f1.MdiParent = this;
       f2.MdiParent = this;
       //Get MDI Client
       foreach(Control c in Controls){
          if(c is MdiClient){
               client = c;
               break;
          }
       }
       client.Click += ClientClicked;           
   }
   ChildForm f1, f2; 
   MdiClient client;
   public ChildForm Dialog {get;set;}
   private void ClientClicked(object sender, EventArgs e){
       if(Dialog != null) Dialog.Flash();
       else {//Show dialog, you can show dialog in another event handler, this is for demonstrative purpose
           Dialog = new ChildForm(){MdiParent = this, Visible = true};
           ActivateMdiChild(Dialog);
           //Suppose clicking on the Dialog client area will close the dialog
           Dialog.Click += (s,ev) => {
              Dialog.Close();
           };
           Dialog.FormClosed += (s,ev) => {
              Dialog = null;
           };
       }
   }
}
于 2013-06-22T14:37:37.093 回答