【问题标题】:How to make a control with a "glass" background?如何制作带有“玻璃”背景的控件?
【发布时间】:2012-02-12 01:20:40
【问题描述】:

我有一个项目需要做一层容器。

容器必须有类似:

Form.Opacity = 0;

用户可以看到顶层下的元素,但不能使用它们。

我看到了许多具有透明背景的示例,但以我的方式,我需要在这个容器上移动元素。比我发现的更好:

class xPanel : Panel
{
  public xPanel()
  {
    SetStyle(ControlStyles.Opaque, true);
  }

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

  protected override void OnPaint(PaintEventArgs e)
  {
    //PaintParentBackground(e);
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, Color.White)),
        0, 0, Width, Height);
  }

  public void InvalidateEx()
  {
    if (Parent == null)
      return;
    Rectangle rc = new Rectangle(this.Location, this.Size);
    Parent.Invalidate(rc, true);
  }
}

但是在拖动元素时会有轨迹,或者在重绘时会闪烁。

我不知道如何解决这个问题。有想法吗?

我使用 InvalidateEx(),在:

protected override void OnLocationChanged(EventArgs e)
{
  if (Parent != null)
    ((xPanel)Parent).InvalidateEx();
}

【问题讨论】:

    标签: c# winforms containers opacity aero-glass


    【解决方案1】:

    尝试添加

    protected override OnPaintBackground(...)
    {
        //base.OnPaintBackground(...);
    }
    

    所以不要重绘背景,至少应该去掉闪烁。

    希望这会有所帮助。

    【讨论】:

    • 抱歉,它在闪烁,但运行速度更快。我使用 InvalidateEx(),在: private void element_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ((DrawElement)sender).Left += eX - SL.X + SP.X ; ((DrawElement)sender).Top += e.Y - SL.Y + SP.Y; ((xPanel)Parent).InvalidateEx(); } } 对不起,我不知道如何在这里写代码 =)
    • @justAuser:你启用双缓冲了吗?
    • 是的,它运行良好,但仍在闪烁。我在重绘时遇到了另一个问题:所有元素都在背景层下绘制...截图:(clip2net.com/s/1uDZY) 有趣的错误:(clip2net.com/s/1uE5n) 鼠标抬起后我需要更多更新吗?有什么更好的更新?
    【解决方案2】:

    如果您使面板的 BackColor 和 TransparancyKey 属性相同,则面板将是透明的。但尽量选择不常用的颜色。

    【讨论】:

    • 使用 TransparancyKey 我会看到我电脑的背景...我需要看到后面的元素...并且用户可以选择某一天这种颜色(它会像一个错误)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2019-05-05
    相关资源
    最近更新 更多