How to make a control with a "glass" background?

814 views Asked by At

I have a project where I need to do a layers of Containers.

Container must have something like:

Form.Opacity = 0;

User can see elements under top layer, but can't use them.

I saw many examples with transparent backgrounds, but in my way I'll need to move elements on this Container. Better what I found:

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

But there are trails while drag elements, or blinking on redrawing.

I don't know how to solve this problem. Have ideas?

I use InvalidateEx(), in:

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

There are 2 answers

3
Tigran On

Try to add

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

So do not repaint a background, should, at least, remove blinking.

Hope this helps.

1
Ozgur Dogus On

If you make the BackColor and TransparancyKey properties of the panel same , the panel will be transparent. But try to select a color that is not commonly used.