通过最小化窗体绘制透明面板而不会变黑 [英] Draw Transparent Panel without Being Black by Form Minimization

查看:153
本文介绍了通过最小化窗体绘制透明面板而不会变黑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#在图片框的位图上绘制一个面板.我使用了下面的代码,当我不最小化Form时,这很好. 当我最小化Form并再次将其最大化到第一个尺寸时,此类绘制的所有面板均显示黑色背景. 我发现,当我将ControlStyles.Opaque更改为诸如"SupportsTransparentBackColor"之类的其他问题时,该问题将得到解决,但面板将不再透明.

I'm drawing a panel on a bitmap in picturebox using C#. I used below code, which is fine when I don't minimize the Form. When I minimize the Form and again maximize it to the first size, all panels which were drawn by this class, show black background. I found that when I change ControlStyles.Opaque to something else such as "SupportsTransparentBackColor" the problem would be fixed but the panels would not be transparent anymore.

public class ExtendedPanel : Panel
{
    private const int WS_EX_TRANSPARENT = 0x00;
    public ExtendedPanel()
    {
         SetStyle(ControlStyles.Opaque, true);
    }

private int opacity = 1;
[DefaultValue(1)]
public int Opacity
{
    get
    {
        return this.opacity;
    }
    set
    {
        if (value < 0 || value > 100)
            throw new ArgumentException("value must be between 0 and 100");
        this.opacity = value;
    }
}
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
        return cp;
    }
}
protected override void OnPaint(PaintEventArgs e)
{
    using (var brush = new SolidBrush(Color.FromArgb(this.opacity * 1 / 100, this.BackColor)))
    {
        e.Graphics.FillRectangle(brush, this.ClientRectangle);
    }
    base.OnPaint(e);
}

}

推荐答案

Reza Aghaei already told you what was actually preventing the Panel transparency to work at all:
WS_EX_TRANSPARENT was set to 0x00 instead of 0x20.

一些改善半透明面板外观的建议.

Some suggestions to improve the appearace of the translucent Panel.

  • 测试面板以设置以下样式:

这将防止在Desing-Time和Run-Time上移动面板上的任何工件.

This will prevent any artifact on the Panel when you're moving it both at Desing-Time and Run-Time.

this.SetStyle(ControlStyles.AllPaintingInWmPaint |
              ControlStyles.UserPaint |
              ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
this.DoubleBuffered = false;
this.UpdateStyles();

  • 设置不透明度值:
    • When setting the opacity value:
    • 如果使用的是 Refresh() ,这将立即更新新的 Opacity 视觉效果.否则,您必须单击表格"以查看更改.在运行时, Invalidate() (通常)就足够了.

      Use Refresh() if it's Desing-Time, this will update the new Opacity visual immediately. Othrerwise, you'll have to click on the Form to see the changes. At Run-Time, Invalidate() is enough (usually).

      set {
          if (value < 0 || value > 255) throw new ArgumentException("value must be between 0 and 255");
          this.opacity = value;
          if (this.DesignMode) this.FindForm().Refresh();
          this.Invalidate();
      }
      

      修改后的测试类:

      public class ExtendedPanel : Panel
      {
          private const int WS_EX_TRANSPARENT = 0x20;
          public ExtendedPanel()
          {
              this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                            ControlStyles.UserPaint |
                            ControlStyles.Opaque, true);
              this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
              this.DoubleBuffered = false;
              this.UpdateStyles();
          }
      
          private int opacity = 1;
      
          [DefaultValue(1)]
          public int Opacity
          {
              get => this.opacity;
              set {
                  if (value < 0 || value > 255) throw new ArgumentException("value must be between 0 and 255");
                  this.opacity = value;
                  if (this.DesignMode) this.FindForm().Refresh();
                  this.Invalidate();
              }
          }
      
          protected override CreateParams CreateParams
          {
              get
              {
                  CreateParams cp = base.CreateParams;
                  cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
                  return cp;
              }
          }
      
          protected override void OnPaint(PaintEventArgs e)
          {
              base.OnPaint(e);
              using (SolidBrush bgbrush = new SolidBrush(Color.FromArgb(this.opacity, this.BackColor)))
              {
                  e.Graphics.FillRectangle(bgbrush, this.ClientRectangle);
              }
          }
      }
      

      这篇关于通过最小化窗体绘制透明面板而不会变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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