C# 表单绘制问题

发布于 2024-10-30 21:15:04 字数 166 浏览 0 评论 0原文

在表单中的元素上制作透明覆盖层的最简单方法是什么?

我希望为我的表单制作一个简单的黑色(不透明度= 0.5)覆盖层,并在我的应用程序执行某些操作时激活它(例如淡入淡出屏幕)。< /strong>

谢谢。

What is the easiest way to make a transparent overlay over the elements in my form?

I wish to make a simple black (with opacity = 0.5) overlay for my form and activate it if my application is doing something (like a fadescreen).

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟稳重的好男人 2024-11-06 21:15:04

控件来创建透明控件

您可以通过继承您想要使用透明面板示例的

class TransparentPanel : Panel 
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        SolidBrush brush = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
        e.Graphics.FillRectangle(brush,0,0,this.Width,this.Height);
    }

}

:并在表单laded.s之后使用此:

void Form1_Load(object sender, EventArgs e)
        {
            TransparentPanel overlay = new TransparentPanel();
            overlay.BackColor = Color.FromArgb(50, Color.Black);
            overlay.Width = this.Width;
            overlay.Height = this.Height;

            this.Controls.Add(overlay);
            overlay.BringToFront();

        }

You can create a transparent control by inherit a control you want use

a Tranparent Panel example :

class TransparentPanel : Panel 
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        SolidBrush brush = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
        e.Graphics.FillRectangle(brush,0,0,this.Width,this.Height);
    }

}

And Use this after form laded.s:

void Form1_Load(object sender, EventArgs e)
        {
            TransparentPanel overlay = new TransparentPanel();
            overlay.BackColor = Color.FromArgb(50, Color.Black);
            overlay.Width = this.Width;
            overlay.Height = this.Height;

            this.Controls.Add(overlay);
            overlay.BringToFront();

        }
杀お生予夺 2024-11-06 21:15:04

最简单的方法是重写应用程序的 OnPaint 方法,并在其中添加以下行:

if( doingSomething )
{
  using( SolidBrush brush = new SolidBrush( Color.FromArgb(128, 0, 0, 0)))
  {
      e.Graphics.FillRectangle( brush, 0, 0, width, height );
  }
}

然后,在执行某些操作时的代码位置,将 doingSomething 设置为 true,然后调用Invalidate。工作完成后,将 doingSomething 设置为 false,并再次调用 Invalidate

The easiest is to override the application OnPaint method, and inside it add the following lines:

if( doingSomething )
{
  using( SolidBrush brush = new SolidBrush( Color.FromArgb(128, 0, 0, 0)))
  {
      e.Graphics.FillRectangle( brush, 0, 0, width, height );
  }
}

Then, at the place in code when your doing something, set doingSomething to true, and call Invalidate. When the work is complete, set doingSomething to false, and call Invalidate again.

束缚m 2024-11-06 21:15:04

您是否尝试过向表单添加一个覆盖整个表单区域的半透明控件?将控件停靠到整个窗体,以便它随窗体调整大小。确保它位于 Z 顺序的最上面,以便所有其他控件都呈现在它的下方。

Have you tried adding a semi-transparent control to your form that covers the entire form area? Have the control dock to the entire form, so that it resizes with the form. Make sure it is topmost in the Z-order, so that all the other controls are rendered below it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文