Clearing the graphics of a transparent panel C#

跟風遠走 提交于 2019-12-20 06:09:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!