【问题标题】:Clearing the graphics of a transparent panel C#清除透明面板C#的图形
【发布时间】:2009-04-28 14:00:17
【问题描述】:

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

到目前为止,除了在绘制下一个矩形之前没有清除面板之外,我一切正常,所以我最终到处都是矩形。

这是我正在使用的代码。

paneGraphics = drawingPane.CreateGraphics();

Rectangle inspectorRectangle;

inspectorRectangle = controller.inspectElement();

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

drawingPane.Invalidate();

我尝试过使用 drawingPane.clear() 但这只会使屏幕变白。

【问题讨论】:

    标签: c# graphics panel transparent


    【解决方案1】:

    看看 OpenPandora 项目的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】:

      你试过了吗:

      graphics.Clear(Color.Transparent);
      

      【讨论】:

      • 我试过 graphics.clear(Color.Transparent); graphics.clear(Color.Empty);和 graphics.clear(Color.White);白色是最接近我想要的,因为它确实清除了所有内容并在正确的位置绘制了一个矩形,但面板不再透明。
      猜你喜欢
      • 2012-10-09
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      相关资源
      最近更新 更多