简体   繁体   English

如何去除MDI父窗体的灰色背景?

[英]How to remove gray background on MDI parent form?

What I'm trying to do is draw some glass on a form marked as an mdi container.我想要做的是在标记为 mdi 容器的表单上绘制一些玻璃。 However as soon as the IsMdiContainer is set, the form adds an MdiClient to it's list of Controls.但是,一旦设置了 IsMdiContainer,表单就会将 MdiClient 添加到它的控件列表中。 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.此时父窗体发生了一些变化——几乎就像一个深灰色面板停靠到放置 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 方法渲染玻璃。

I think this is perfect enough.我认为这已经足够完美了。

foreach (Control ctrl in this.Controls)  
{    
    if (ctrl is MdiClient)  
    {  
        ctrl.BackColor = Color.LightGray;  
    }
}

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.我正在谈论的那个深灰色区域,它被绘制在窗体的 OnPaint 方法中发生的所有内容上。 Obviously when there is an MdiContainer present the form is preprogrammed to paint the dark gray area which was obstructing the glass.显然,当存在 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 不挡住玻璃,它应该完美绘制。

This should do the trick这应该可以解决问题

Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.FromArgb(14, 16, 62);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM