Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VS2010中的真正透明Picturebox_C#_Winforms_Visual Studio_Transparency_Picturebox - Fatal编程技术网

C# VS2010中的真正透明Picturebox

C# VS2010中的真正透明Picturebox,c#,winforms,visual-studio,transparency,picturebox,C#,Winforms,Visual Studio,Transparency,Picturebox,在我的WinForm应用程序中,我需要对一些图像进行分层。但是,我在获取透明控件以放置图像时遇到问题。我做了一些研究,得出了以下结论: public class TransparentPicture : PictureBox { protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams;

在我的WinForm应用程序中,我需要对一些图像进行分层。但是,我在获取透明控件以放置图像时遇到问题。我做了一些研究,得出了以下结论:

public class TransparentPicture : PictureBox
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // do nothing
    }

    protected override void OnMove(EventArgs e)
    {
        RecreateHandle();
    }

}
在我关闭visualstudios并重新打开解决方案之前,这似乎可以正常工作。然后我的控件在设计器中消失。它们在我运行程序时显示,但我也需要它们在designer中显示,在那里我可以继续设计我的应用程序

我知道这不是我需要做的全部,因为这些控件总是导致我的程序冻结几秒钟之类的事情


所以我的问题是..有人知道我在哪里可以找到透明控件的代码,或者如何修复我拼凑的代码吗?

将透明图片设置为常规图片框,直到IsTransparent属性设置为true

在设计时将属性设置为false,在FormLoad事件中将属性设置为true(只有在实际运行应用程序时才会发生)

这样,在设计时,它们将像普通的图片框一样工作,但当您运行应用程序时,它们将变得透明

public class TransparentPicture : PictureBox
{
    public bool IsTransparent { get; set; }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;

            if (this.IsTransparent)
            {
                cp.ExStyle |= 0x20;
            }

            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        if (!this.IsTransparent)
        {
            base.OnPaintBackground(e);
        }
    }

    protected override void OnMove(EventArgs e)
    {
        if (this.IsTransparent)
        {
            RecreateHandle();
        }
        else
        {
            base.OnMove(e);
        }
    }
}
然后,在FormLoad事件中,您应该执行以下操作:

for (var i = 0; i < this.Controls.Count; i++)
{
    var tp = this.Controls[i] as TransparentPicture;

    if (tp != null)
    {
        tp.IsTransparent = true;
    }
}

WinForms并不真正支持真正的透明性。使用WPF。您可以通过代码检查您是否处于设计模式,并且不运行
|=0x20
,如果可以,还可以运行
base.OnPaintBackground
,等等。PictureBox的可能副本已支持透明,无需任何特殊破解。只需将BackColor设置为Color.Transparent。查看上一个链接中的屏幕截图。
tp1.IsTransparent = tp2.IsTransparent = tp3.IsTransparent = true;