views:

2788

answers:

3

I have a WebBrowser control with a transparent panel over the top of it, what I am trying to do is draw a rectangle on the transparent panel around the element in the page that the mouse is hovering over.

So far I have everything working except the panel is not being cleared before drawing the next rectangle so I end up with rectangles everywhere.

heres the code Im using.

paneGraphics = drawingPane.CreateGraphics();

Rectangle inspectorRectangle;

inspectorRectangle = controller.inspectElement();

paneGraphics.DrawRectangle(new Pen(Color.Blue, 1), inspectorRectangle);     

drawingPane.Invalidate();

I've tried using drawingPane.clear() but that just turns the screen white.

+1  A: 

Did you try:

graphics.Clear(Color.Transparent);
Meta-Knight
A: 

I have tried graphics.clear(Color.Transparent); graphics.clear(Color.Empty); and graphics.clear(Color.White);

White was the closest to what I want as it does clear everything and draw the one rectangle in the correct place but the panel is no longer transparent.

Morgeh
A: 

Take a look at the OpenPandora project's code

public class TransparentPanel : Panel
{
    Timer Wriggler = new Timer();

    public TransparentPanel()
    {
        Wriggler.Tick += new EventHandler(TickHandler);
        this.Wriggler.Interval = 500;
        this.Wriggler.Enabled = true;
    }

    protected void TickHandler(object sender, EventArgs e)
    {
        this.InvalidateEx();
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;

            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT 

            return cp;
        }
    }

    protected void InvalidateEx()
    {
        if (Parent == null)
        {
            return;
        }

        Rectangle rc = new Rectangle(this.Location, this.Size);

        Parent.Invalidate(rc, true);
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // Do not allow the background to be painted  
    }
}
Nik