通常我們不任何子控件添加到Mdi Form
。當Form
用作Mdi Form
,它應該包含唯一的孩子是MdiClient
。那MdiClient
將包含您的child forms
。所有的控件應該放在Child forms
上。但是,如果您需要,我們仍然可以使其工作
Mdi Form
中包含默認MdiClient
。我們可以在Mdi Form
的Controls
集合中找到它。它是MdiClient
的類型。這將通過您的Mdi Form
的所有其他控件被覆蓋,這就是爲什麼你Child forms
不能在上面默認情況下帶來的。爲了解決這個問題,我們只需要訪問MdiClient
並撥打電話BringToFont()
,並且只要沒有任何Child form
爲Visible
,我們將調用SendToBack()
MdiClient
來顯示您的其他控件(按鈕,圖像,標籤,下拉等) 。這裏是你的測試代碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
IsMdiContainer = true;
//Find the MdiClient and hold it by a variable
client = Controls.OfType<MdiClient>().First();
//This will check whenever client gets focused and there aren't any
//child forms opened, Send the client to back so that the other controls can be shown back.
client.GotFocus += (s, e) => {
if (!MdiChildren.Any(x => x.Visible)) client.SendToBack();
};
}
MdiClient client;
//This is used to show a child form
//Note that we have to call client.BringToFront();
private void ShowForm(Form childForm)
{
client.BringToFront();//This will make your child form shown on top.
childForm.Show();
}
//button1 is a button on your Form1 (Mdi container)
//clicking it to show a child form and see it in action
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2 { MdiParent = this };
ShowForm(f);
}
}
你問什麼,據我所知,是不是可能並且可行。雖然可以將控件放在MDI容器上,但主要是可以使用工具欄和狀態欄。 MDIChildren總是出現在表格的主要元素下。 – Adrian
如果OP選擇去與該問題的答案所描述的方法,我建議使用Form.Closing事件通知家長形成孩子正在關閉,因此它可以,如果它打算讓菜單面板評估出現或消失,而不是在一個定時器上浪費CPU週期,當100個循環中有99個循環時,它不會執行任何操作。 – Adrian
@阿德里安這個問題的接受答案不一定是我想要的答案。在這個問題的其他答案和鏈接中還有其他好主意。其中之一應該爲OP –