控件在刷新后不再透明

本文关键字:不再 透明 刷新 控件 | 更新日期: 2023-09-27 18:03:13

我已经创建了一个透明面板。

 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 ();或. validate();然后控件和父控件的颜色是一样的。我已经尝试覆盖BackgroundOnPaint-Event,但它不起作用。

 protected override void OnPaintBackground(PaintEventArgs pevent)
{
  Application.DoEvents();
}

有人能帮我吗?

控件在刷新后不再透明

我现在找到解决办法了。只需将Opaque设置为true。

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