MDI 子窗体似乎让父窗体的控件显示为透明
这是一个小例子:
带有图片框和按钮的常规旧表单。没什么花哨的。在按钮的单击事件中:
private void button1_Click(object sender, EventArgs e)
{
Form form = new Form();
form.MdiParent = this;
form.BackColor = Color.Red;
form.WindowState = FormWindowState.Maximized;
form.Show();
}
但是,当我单击按钮时会发生这种情况:
图片框和按钮仍然显示,就好像子窗体是透明的一样。关于如何改变这个有什么建议吗?我希望子表单能够像常规表单一样涵盖所有内容。
Here's a small example:
A regular old form with a pictureBox and a button. Nothing fancy. In the button's click event:
private void button1_Click(object sender, EventArgs e)
{
Form form = new Form();
form.MdiParent = this;
form.BackColor = Color.Red;
form.WindowState = FormWindowState.Maximized;
form.Show();
}
However, when I click on the button this happens:
The picturebox and the button are still showing, as if the child form were transparent. Any suggestions on how to change this? I want the child form to cover everything like a regular form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于 MDI 子窗口是 MDI 客户端窗口的子窗口。深灰色背景的那一张。您放置在 MDI 父级上的任何控件都将具有更高的 Z 顺序并与 MDI 客户端窗口重叠。因此任何 MDI 子窗口。您可以将控件停靠到边缘,Winforms 将自动缩小 MDI 客户端窗口以适应剩余的空间。对于按钮来说,正确的做法是将其放在面板上并停靠面板。但这对形象没有帮助。
Winforms 使得获取对 MDI 客户端窗口的引用有点棘手,您必须迭代 MDI 父级的 Controls 集合才能找到它。像这样:
注意评论,解决这个问题比我想象的要困难得多。为窗口实现 Paint 事件是一种选择,但它像廉价汽车旅馆一样闪烁。
The problem is that the MDI child windows are a child of the MDI client window. The one with the dark gray background. Any control you put on the MDI parent will have a higher Z-order and overlap the MDI client window. And thus any MDI child window. You can dock a control to an edge and Winforms will automatically shrink the MDI client window to fit the space that's left. Which is the proper thing to do for the button, put it on a panel and dock the panel. But that won't help for the image.
Winforms makes it a bit tricky to get a reference to the MDI client window, you have to iterate the MDI parent's Controls collection to find it back. Like this:
Note the comment, fixing this is a lot harder then I counted on. Implementing the Paint event for the window is an option but it flickers like a cheap motel.