2013-06-22 60 views
2

當我們點擊父窗體的客戶區時,如何強制完全關注子窗體? [喜歡的MessageBox或錯誤消息有這樣的焦點,所以我們必須點擊信息框的確定按鈕。]如何強制關注子表單?

這是我的代碼:

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

但它給我的錯誤:

Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.

+0

您能不能告訴你嘗試的代碼示例完成這個? – Sparko

+0

我使用TopMost = true,但那不是我想要的。 –

回答

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(); 
} 
+0

我試試這個,但是這隻在我刪除父窗體時才起作用。 –

+0

它不工作的方式? –

+0

當與父窗體一起使用時,它給我錯誤:不是頂層窗體的窗體不能顯示爲模態對話框。在調用showDialog之前,從任何父窗體中刪除窗體。 –

0

我想你想擁有在MDI Parent FormTopLevel = false某種MessageBox。爲了讓效果看起來像真正的作用: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; 
      }; 
     } 
    } 
}