Érik Desjardins
Érik Desjardins

Reputation: 993

Drawing black rectangle on top of all controls in a c# winform

I am currently working on a TileEditor software for game making. Almost everything is going smoothly. Here's the problem.

I tried so hard to achieve this without any succes. Let's begin with a screenshot of the WinForm.

Screenshot of the application without the black square You probably already figured the way the app works.

So, I'd like to draw a semi-transparent black rectangle over the entire form except the Tile Selection Panel (the righthandside panel). I already have the function to find the correct points and location of the rectangle so that it does not appear over the right panel.

Although when I try to draw it, it is drawn underneath the controls. Is there a way to draw the rectangle on top of everything in the form like this?

(Photoshoped) Black rectangle over form Thank you so much for your time, it is truly appreciated. Sorry for poor english, it is not my first language.

Upvotes: 3

Views: 2649

Answers (2)

IntelOrca
IntelOrca

Reputation: 108

You can create the control below which creates an image of the underlying controls and then paints it with a semi-transparent black rectangle over it. You can place this on your form over the controls you want to darken and underneath the controls you want to stay lit up. You can then set the visibility of it as appropriate.

    class OverlayShadow : Control
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            if (Parent != null) {
                Bitmap behind = new Bitmap(Parent.Width, Parent.Height);
                foreach (Control c in Parent.Controls)
                    if (c.Bounds.IntersectsWith(this.Bounds) & c != this)
                        c.DrawToBitmap(behind, c.Bounds);
                e.Graphics.DrawImage(behind, -Left, -Top);
                behind.Dispose();
            }
            e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, 0, 0, 0)), Bounds);
        }
    }

Upvotes: 1

The easiest way to do this will probably be to have a stand-alone, border-less, semi-transparent form with the smoky glass effect that floats on top of your design form. Disable it so that mouse clicks and so on fall through to the underlying form.

(Your English is fine).

Upvotes: 3

Related Questions