I'm trying to create a semi-transparent panel that can sit on top of
an image (picturebox, say) and allow the underlying image to partially
show through.

What I've done here is working so far, but I'm getting some weirdness
when I partially cover up my panel and it redraws.  It sometimes
doesn't redraw properly - areas of the panel are pure white.  Further,
if I change the opacity at runtime, the control sometimes totally
whites out until I cover it up, uncover it and it redraws (only the
newly-uncovered area redraws properly).

I was hoping someone with control authoring experience might point me
in the right direction.

The easiest way to use this is to drop a panel on a form and change it
to a TranparentPanel in code.  Put an image in a picturebox and put
the panel on top of it (panel is topmost).

Thanks!

using System;
using System.Drawing;
using System.Windows.Forms;


public class TransparentPanel : Panel
{

    private int _opacity = 50;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20; // Turn on WS_EX_TRANSPARENT
            return cp;
        }
    }

    public int Opacity
    {
        get
        {
            return _opacity;
        }
        set
        {
            if (value < 0 || value > 255)
            {
                throw new ArgumentException("Invalid value.  Opacity
must be between 0 and 100 percent.");
            }

            _opacity = value;
            this.Refresh();
        }
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        int newOpacity = 0;
        newOpacity = (int)(_opacity * 2.55);
        pe.Graphics.FillRectangle(new SolidBrush(Color.FromArgb
(newOpacity, SystemColors.Control)), this.ClientRectangle);
    }
}



Reply via email to