0

我为自己创建了一个透明面板。

 public TransPanel()
{
}

protected override CreateParams CreateParams
{
    get
    {

        CreateParams cp = base.CreateParams;

        cp.ExStyle |= 0x00000020;

        return cp;

    }

}

protected override void OnPaint(PaintEventArgs e)
{
    if (ImageForBackGround != null)
    {
        e.Graphics.DrawImage(ImageForBackGround, new Point(0, 0));
    }
}

它工作正常,但我有一个问题,如果我执行 .Refresh(); 控件不再透明 或 .Invalidate();。然后控件只有与他的父级相同的颜色。我已经尝试过覆盖 BackgroundOnPaint-Event,但它不起作用。

 protected override void OnPaintBackground(PaintEventArgs pevent)
{

  Application.DoEvents();

}

有人可以帮助我吗?提前致谢。

4

1 回答 1

0

我现在找到了解决方案。只需将不透明设置为真。

 protected override void OnPaint(PaintEventArgs e)
    {
        if (ImageForBackGround != null)
        {
           e.Graphics.DrawImage(ImageForBackGround, new Point(0, 0));
           this.SetStyle(ControlStyles.Opaque, true);

        }
    }
于 2013-03-11T12:43:21.050 回答