Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

I then do is the following to move the MdiClient control out of the way a bit:

    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.

As can be seen here: 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.

Any ideas?

PS - Glass is being rendered using DwmExtendFrameIntoClientArea method in Vista.

like image 272
Dylan Avatar asked Jul 27 '09 20:07

Dylan


People also ask

How to change background color of Mdi form?

Create a new Visual C# Windows application. Form1 is created by default. Click the form, and then, on the View menu, select Properties Window to view the properties for the form. Set the BackColor property to the color that you want (such as Blue).

How do you change the background image of a parent in MDI?

Two options: Set Background image and change the BackgroundImageLayout property to Stretch (as suggested by Guster_Q) Set your image in a imagecontrol and set it docking property to fill.


2 Answers

I think this is perfect enough.

foreach (Control ctrl in this.Controls)  
{    
    if (ctrl is MdiClient)  
    {  
        ctrl.BackColor = Color.LightGray;  
    }
}
like image 64
Wolverine Avatar answered Oct 21 '22 21:10

Wolverine


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.

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.

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.

like image 44
Dylan Avatar answered Oct 21 '22 21:10

Dylan