C# winforms 弹出背景褪色 [英] C# winforms pop up with background faded

查看:23
本文介绍了C# winforms 弹出背景褪色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主窗体,当用户单击主窗体中的按钮时,我需要在主窗体中心显示一个子窗体作为弹出窗口.在这个阶段,子窗体上的所有空闲空间(在附加图像中的内框周围)都应该是透明的(变灰),以便主窗体中的控件可见.

如何实现?我尝试在子窗体上使用不透明度,但不透明度被应用于表单中的所有控件.

I have a mainform, on user clicking a button in the mainform, I need to show a child form as popup in center of mainform. At this stage, all the free spaces(around the inner box in the attached image) on the child form should be transparent(greyed out) so that controls in main forms are visible.

How to achieve this ? I tried using Opacity on child form, but opacity is being applied to all the controls in the form.

Make a form's background transparent: Solution provided in this link just makes fully transparent. I want the opacity effect.

解决方案

Here is how I've solved this problem:

I've created a form, and override it's OnPaintBackground method like this:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(70, 0, 0, 0)))
        {
            e.Graphics.FillRectangle(brush, e.ClipRectangle);
        }
    }

Inside this form I'm hosting a user control that's basically a panel with a label and a two buttons (OK, Cancel). Once the user clicks a button I set this form's DialogResult to OK or Cancel (depending on the button clicked).

This form is shown from the main form as a dialog (frm.ShowDialog(this)). Also, it takes in it's constructor the main form and set it's own display rectangle to cover the main form completly, and then resize the user control to a third of the height and half of the width of the form, and centers it.

 public  FrmThickBox(Form owner, string message)
 {
    this.Owner = owner;
    this.Width = owner.Width;
    this.Height = owner.Height;
    this.Top = owner.Top;
    this.Left = owner.Left;
    this.thickBoxControl.Text = message;
    this.thickBoxControl.Size = new Size((int)this.Width / 2, (int)this.Height / 3);
    this.thickBoxControl.Top = (int)((this.Height - thickBoxControl.Height) / 2);
    this.thickBoxControl.Left = (int)((this.Width - thickBoxControl.Width) / 2);
 }

And here is how it looks like (of course, the rounded corners are an entire different story):

这篇关于C# winforms 弹出背景褪色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆