winforms上的透明背景?

Pri*_*rix 39 c# background transparent winforms

我想让我的窗体透明,所以删除边框,控件和一切只留下表格框,然后我尝试BackColor和TransparencyKey透明但它没有工作,因为BackColor不接受透明的颜色.搜索后我在msdn找到了这个:

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.TransparencyKey = BackColor;
Run Code Online (Sandbox Code Playgroud)

不高兴它也没用.我仍然得到灰色或任何其他选定的颜色背景.

我想做的就是让窗体透明,这样我就可以使用一个背景图像,就好像它是我的窗体一样.

我在这里搜索并看到很多关于不透明度的主题,这不是我正在寻找的,也看到了一些关于这个方法我正在尝试但尚未找到答案.

希望有人能照亮我的道路.

更新:

问题解决后,图像被删除

Joe*_*ton 66

我之前使用的方式是使用BackColor的野性颜色(右脑中没有人会使用的颜色),然后将透明度键设置为.

this.BackColor = Color.LimeGreen;
this.TransparencyKey = Color.LimeGreen;
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用,当我这样做时,我简单得到了Lime背景.我在其他主题中似乎有这个答案并尝试但是谢谢. (2认同)

Ale*_*man 21

在Windows窗体中获取透明背景的简单解决方案是覆盖这样的OnPaintBackground方法:

protected override void OnPaintBackground(PaintEventArgs e)
{
    //empty implementation
}
Run Code Online (Sandbox Code Playgroud)

(请注意,该base.OnpaintBackground(e)功能已从功能中删除)

  • 这个答案更好,因为它不会引起像这样的奇怪的事情:http://i.imgur.com/lYFunRT.png(注意奇怪的“柠檬绿”阴影) (3认同)

Ayr*_*yrA 7

这是我的解决方案:

在构造函数中添加以下两行:

        this.BackColor = Color.LimeGreen;
        this.TransparencyKey = Color.LimeGreen;
Run Code Online (Sandbox Code Playgroud)

在表单中,添加以下方法:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.LimeGreen, e.ClipRectangle);
    }
Run Code Online (Sandbox Code Playgroud)

请注意,此框架不仅在框架内完全透明,而且您还可以单击它.但是,在其上绘制图像并使表单能够在任何地方拖动以创建自定义形状的表单可能会很酷.


Joe*_*oel 6

我已经尝试了上面的解决方案(以及其他帖子中的许多其他解决方案).

就我而言,我通过以下设置完成了它:

public partial class WaitingDialog : Form
{
    public WaitingDialog()
    {
        InitializeComponent();

        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;

        // Other stuff
    }

    protected override void OnPaintBackground(PaintEventArgs e) { /* Ignore */ }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,这是以前给出的答案的混合.


ron*_*cli 5

我的解决方案非常接近Joel的解决方案(不是Etherton,只是普通的Joel):

public partial class WaitingDialog : Form
{
    public WaitingDialog()
    {
        InitializeComponent();

        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        this.BackColor = Color.Transparent;
        this.TransparencyKey = Color.Transparent; // I had to add this to get it to work.

        // Other stuff
    }

    protected override void OnPaintBackground(PaintEventArgs e) { /* Ignore */ }
}
Run Code Online (Sandbox Code Playgroud)