如何使用“玻璃”背景进行控制?

时间:2012-01-17 07:49:08

标签: c# winforms containers opacity aero-glass

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

容器必须具有以下内容:

Form.Opacity = 0;

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

我看到许多具有透明背景的示例,但在我的方式中,我需要在此Container上移动元素。我找到的更好:

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();
}

2 个答案:

答案 0 :(得分:0)

尝试添加

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

所以不要重新绘制背景,至少应该删除闪烁。

希望这有帮助。

答案 1 :(得分:0)

如果使面板的BackColor和TransparancyKey属性相同,则面板将是透明的。但请尝试选择一种不常用的颜色。