清除透明面板C#的图形

我有一个WebBrowser控件,顶部有一个透明面板,我要做的是在透明面板上围绕鼠标hover在页面中的元素上绘制一个矩形。

到目前为止,我已经完成了所有工作,除了在绘制下一个矩形之前没有清除面板,所以我到处都是矩形。

inheritance人使用的代码。

paneGraphics = drawingPane.CreateGraphics(); Rectangle inspectorRectangle; inspectorRectangle = controller.inspectElement(); paneGraphics.DrawRectangle(new Pen(Color.Blue, 1), inspectorRectangle); drawingPane.Invalidate(); 

我尝试过使用drawingPane.clear(),但只是将屏幕变为白色。

看一下OpenPandora项目的代码

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

你试过了吗:

 graphics.Clear(Color.Transparent);