Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

c# - Creating a dark background when a new form appears

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...