0

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

容器必须具有以下内容:

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

2 回答 2

0

尝试添加

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

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

希望这可以帮助。

于 2012-01-17T07:54:00.747 回答
0

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

于 2012-01-17T08:00:57.167 回答