1

This is hard for me to actually explain so I created a mockup for what I want.

Mockuk example

Can someone here explain how I could make this? Maybe some code could help but I think a general idea or direction can be sufficient enough.

I want to darken the parent background whenever a new window is opened in front of it.

Danny Hoeve
  • 692
  • 13
  • 30
  • are the controls sitting on a `Panel` if so then just change the panel background color.. can you at least show some code to better express what you have – MethodMan Dec 04 '14 at 22:00
  • SO is about specific coding problems, Your question is exactly which doesn't fit here. `Can someone here explain how I could make this? Maybe some code could help but I think a general idea or direction can be sufficient enough.` – L.B Dec 04 '14 at 22:20
  • You can show a [semi-transparent panel](https://stackoverflow.com/a/32402532/3110834) over other controls. – Reza Aghaei May 29 '18 at 17:06

3 Answers3

11

Take a screenshot of the form and paint a semi-transparent rectangle over it. Add that image to a panel the size of the form and bring it to the front. Display your dialog. Get rid of the panel:

    private void button1_Click(object sender, EventArgs e)
    {
        // take a screenshot of the form and darken it:
        Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
        using (Graphics G = Graphics.FromImage(bmp))
        {
            G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            G.CopyFromScreen(this.PointToScreen(new Point(0, 0)), new Point(0, 0), this.ClientRectangle.Size);
            double percent = 0.60;
            Color darken = Color.FromArgb((int)(255 * percent), Color.Black);
            using (Brush brsh = new SolidBrush(darken))
            {
                G.FillRectangle(brsh, this.ClientRectangle);
            }
        }

        // put the darkened screenshot into a Panel and bring it to the front:
        using (Panel p = new Panel())
        {
            p.Location = new Point(0, 0);
            p.Size = this.ClientRectangle.Size;
            p.BackgroundImage = bmp;
            this.Controls.Add(p);
            p.BringToFront();

            // display your dialog somehow:
            Form frm = new Form();
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.ShowDialog(this);
        } // panel will be disposed and the form will "lighten" again...
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
2

I don't know of any way to make Controls darken themselves. And since semi-transparent controls are a mess, too, here is a way that gets the effect by overlaying the Form by another, empty Form, which is semi-transparent:

Form fff;

fff = new Form();
fff.ControlBox = false;
fff.MinimizeBox = false;
fff.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
fff.Text = "";
fff.Size = Size;
fff.BackColor = Color.DarkSlateBlue;
fff.Opacity = 0.2f;
fff.Show();
fff.Location = this.Location;

If you want only the ClientRectangle to appear darkened change these lines:

fff.Size = ClientSize;
fff.Location = PointToScreen(Point.Empty);

After this you open the secondary Form and when you close it you hide this overlay Form again..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • This could be the way to do it. I do think it's a bit of a dirty way though, but I can't think of any other possible way to achieve something like that. Thanks! – Danny Hoeve Dec 04 '14 at 22:59
0

Looks like you need to create an event (maybe include an enumerate for Start, Completed or make it a bool so it can signal Start/Completed in the same event). Have your large window subscribe to this event. When it is time to show/create the new form, fire the event (with the event arg set to Start). When the big window sees the "Start" event, it does whatever it needs and darkens itself. When the new form goes away, fire the Completed event. When the big form sees the "Completed" event, it restores itself.

StarPilot
  • 2,246
  • 1
  • 16
  • 18
  • `...it does whatever it needs and darkens itself.` I *think* that's the part the OP is trying to figure out. – LarsTech Dec 04 '14 at 22:16
  • @LarsTech - If that is what he wanted to ask, then he should have simply asked that. It seems to me he is asking about the activity sequence, not "how do I darken a client area in winforms by 60%" or "Can I use an overlay to darken my client area". – StarPilot Dec 04 '14 at 22:23
  • 1
    As far as darkening the screen, there's several ways. Just off the top of my head, he could grab his client area, create a new panel whose size is set to match that, and set its transparency to something like 40% or 60%. When he wants to go back to normal, he just deletes the panel or sets its size to 0,0,1,1 and sets it out of view. Lots of ways to make the visual effect. And that's why he should ask about the effect, without showing the other form. – StarPilot Dec 04 '14 at 22:25
  • I understand the confusion, should've been more specific. I was indeed asking how to darken the client area, by x% but I couldn't come up with the right words. Thanks for your input! – Danny Hoeve Dec 04 '14 at 23:01
  • 1
    Sorry about that. Just being a bit more literal and focused today. Good luck with finding a good solution! – StarPilot Dec 04 '14 at 23:02