刷新后控制不再透明

时间:2013-03-11 10:39:44

标签: c#

我创建了一个透明的面板。

 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();

}

有人能帮助我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我现在找到了解决方案。只需将Opaque设置为true即可。

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

        }
    }
相关问题