问题
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:
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
}
}
回答2:
Did you try:
graphics.Clear(Color.Transparent);
来源:https://stackoverflow.com/questions/798105/clearing-the-graphics-of-a-transparent-panel-c-sharp