【问题标题】:Transparent Control on Transparent control?透明控制透明控制?
【发布时间】:2011-12-14 00:28:11
【问题描述】:

我创建了一个透明表布局面板:

class TransTablePanel : TableLayoutPanel
{
    public TransTablePanel()
    {

    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do not paint background.
    }
}

还有一个透明的图片框(我试过只使用 BackColor = Transparent 但没用)

class TransPicBox : PictureBox
{
    public TransPicBox()
    {

    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do not paint background.
    }
}

结果如下:

第一个单元格是带有此绘制事件的 PictureBox:

private void picBoxCompass_Paint(object sender, PaintEventArgs e)
    {
        Bitmap b = Properties.Resources.Compass_Rose;
        float rot = PitControl.GetPitbullRotation();
        e.Graphics.DrawImage(rotateImage(b, rot), 0, 0, picBoxCompass.Width, picBoxCompass.Height);
        e.Graphics.DrawLine(LinePen, picBoxCompass.Width / 2, 0, picBoxCompass.Width / 2, picBoxCompass.Height / 2);
        e.Graphics.FillPie(Brushes.Green, picBoxCompass.Width / 2 - 10, picBoxCompass.Height / 2 - 10, 20, 20, 0, 360);
    }

你可以看到它不是透明的(黑色背景),第二个单元格是透明的(你可以看到我的表单的背景图片)。

如何使 PictureBox 透明?

【问题讨论】:

  • 也许您的 Compass_Rose 缺少一些透明度?
  • Compass_Rose 是具有全透明背景的 PNG :)
  • 我不确定,但我认为你不应该重写 OnPaintBackground 方法,如果你指定透明度作为背景,你将不得不绘制它
  • @Tokk 我尝试使用具有透明背景的普通 PictureBox 也没有成功,并且 TranPanel 工作完美。 (比使用透明颜色的普通面板更快)

标签: c# .net winforms transparency picturebox


【解决方案1】:

好的,我正在添加对我有用的代码。它不是很好(我想听听如何做得更好),但也许它会帮助你

public class TransPicBox : Control
{
    public Image Image
    {
        get;
        set;
    }

    public TransPicBox()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.SupportsTransparentBackColor, true);
        base.BackColor = Color.FromArgb(0, 0, 0, 0);//Added this because image wasnt redrawn when resizing form
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (Image != null)
        {
            e.Graphics.DrawImage(Image, 0, 0, Image.Width, Image.Height);
        }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }
}

您需要设置Image 属性。

编辑:注意:图片应该是透明背景(用*.png格式测试)

结果:

【讨论】:

  • 调整表单大小时项目中没有闪烁?当我测试时,我有闪烁,可能是因为高分辨率表单背景图像(图像布局样式 = strech)
  • 我以前会闪烁,但后来我创建了 TransTablePanel 类,现在没有闪烁了 :)
猜你喜欢
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 2022-01-17
  • 2011-01-31
  • 1970-01-01
相关资源
最近更新 更多