C# 如何去除 MDI 父窗体上的灰色背景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1190395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove gray background on MDI parent form?
提问by Dylan
What I'm trying to do is draw some glass on a form marked as an mdi container. However as soon as the IsMdiContainer is set, the form adds an MdiClient to it's list of Controls. At this point something happens to the parent form - almost like a dark gray panel is being docked to the entire form onto which the MdiClient is being placed on.
我想要做的是在标记为 mdi 容器的表单上画一些玻璃。但是,一旦设置了 IsMdiContainer,表单就会将 MdiClient 添加到它的控件列表中。在这一点上,父窗体发生了一些事情 - 几乎就像一个深灰色面板被停靠在整个窗体上,MdiClient 被放置在这个窗体上。
I then do is the following to move the MdiClient control out of the way a bit:
然后我要做的是将 MdiClient 控件移开一点:
foreach(var c in Controls)
{
if(c is MdiClient)
{
var client = (MdiClient)c;
client.BackColor = Color.Red;
client.Dock = DockStyle.None;
client.Size = new Size(this.Width-100, this.Height);
break;
}
}
This then makes the actual MdiClient area smaller so we can see what is behind it (the bit which hosts the children forms) and it is blatantly obvious that the parent form is not painting or something.
然后,这会使实际的 MdiClient 区域变小,以便我们可以看到它后面的内容(承载子窗体的位),并且很明显父窗体没有绘画或其他东西。
As can be seen here: http://img525.imageshack.us/img525/8605/mdiglassproblem.png
可以在这里看到:http: //img525.imageshack.us/img525/8605/mdiglassproblem.png
I now need to somehow get the area behind the MdiClient (dark gray part which is rendered white on the glass section) to go away.
我现在需要以某种方式使 MdiClient 后面的区域(在玻璃部分呈现白色的深灰色部分)消失。
Any ideas?
有任何想法吗?
PS - Glass is being rendered using DwmExtendFrameIntoClientArea method in Vista.
PS - 在 Vista 中使用 DwmExtendFrameIntoClientArea 方法渲染 Glass。
采纳答案by Dylan
I managed to get it working. That dark gray area I was talking about, which gets painted over everything was occuring in the OnPaint method of the form. Obviously when there is an MdiContainer present the form is preprogrammed to paint the dark gray area which was obstructing the glass.
我设法让它工作。我正在谈论的那个深灰色区域被绘制在表单的 OnPaint 方法中。显然,当存在 MdiContainer 时,表单会被预编程以绘制阻碍玻璃的深灰色区域。
So just override the OnPaint method without calling it's base and then take the code that was used to draw the glass in the normal Paint method and stick it in the OnPaint method.
因此,只需覆盖 OnPaint 方法而不调用它的基础,然后获取用于在普通 Paint 方法中绘制玻璃的代码并将其粘贴到 OnPaint 方法中。
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
bool glassEnabled = IsGlassEnabled();
if (glassEnabled) // draw glass if enabled
{
Rectangle rc = picPlaceHolder.ClientRectangle;
IntPtr destdc = e.Graphics.GetHdc(); // hwnd must be the handle of form, not control
IntPtr Memdc = CreateCompatibleDC(destdc);
IntPtr bitmapOld = IntPtr.Zero;
BITMAPINFO dib = new BITMAPINFO();
dib.bmiHeader.biHeight = -(rc.Bottom - rc.Top);
dib.bmiHeader.biWidth = rc.Right - rc.Left;
dib.bmiHeader.biPlanes = 1;
dib.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
dib.bmiHeader.biBitCount = 32;
dib.bmiHeader.biCompression = BI_RGB;
if (!(SaveDC(Memdc) == 0))
{
IntPtr bitmap = CreateDIBSection(Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0);
if (!(bitmap == IntPtr.Zero))
{
bitmapOld = SelectObject(Memdc, bitmap);
BitBlt(destdc, rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top, Memdc, 0, 0, SRCCOPY);
}
// remember to clean up
SelectObject(Memdc, bitmapOld);
DeleteObject(bitmap);
ReleaseDC(Memdc, -1);
DeleteDC(Memdc);
}
e.Graphics.ReleaseHdc();
}
}
Then just ensure that the MdiContainer is not in the way of the glass and it should draw perfectly.
然后只需确保 MdiContainer 没有挡住玻璃,它应该可以完美绘制。
回答by Wolverine
I think this is perfect enough.
我认为这已经足够完美了。
foreach (Control ctrl in this.Controls)
{
if (ctrl is MdiClient)
{
ctrl.BackColor = Color.LightGray;
}
}
回答by Alp Altunel
This should do the trick
这应该可以解决问题
Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.FromArgb(14, 16, 62);