清除透明面板的图形 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/798105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing the graphics of a transparent panel C#
提问by
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.
我有一个 WebBrowser 控件,它的顶部有一个透明面板,我想要做的是在鼠标悬停在页面上的元素周围的透明面板上绘制一个矩形。
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.
我试过使用 drawingPane.clear() 但这只会使屏幕变白。
回答by Meta-Knight
Did you try:
你试过了吗:
graphics.Clear(Color.Transparent);
回答by Nik
Take a look at the OpenPandora project's code
看一下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
}
}