简体   繁体   English

winform 上的透明背景?

[英]Transparent background on winforms?

I wanted to make my windows form transparent so removed the borders, controls and everything leaving only the forms box, then I tried to the BackColor and TransparencyKey to transparent but it didnt work out as BackColor would not accept transparent color.我想让我的窗口窗体透明,所以删除了边框、控件和所有只留下窗体框的东西,然后我尝试将 BackColor 和 TransparencyKey 设置为透明,但没有成功,因为 BackColor 不接受透明颜色。 After searching around I found this at msdn:四处搜索后,我在 msdn 上找到了这个:

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

Unhappyly it did not work either.不幸的是它也没有用。 I still get the grey or any other selected color background.我仍然得到灰色或任何其他选定的颜色背景。

All I wanted to do is to have the windows form transparent so I could use a background image that would act as if it was my windows form.我想要做的就是让窗体透明,这样我就可以使用背景图像,就像我的窗体一样。

I searched around here and saw many topics in regards opacity which is not what I am looking for and also saw some in regards this method I was trying but have not found an answer yet.我在这里搜索并看到许多关于不透明度的主题,这不是我正在寻找的,并且还看到了一些关于我正在尝试但尚未找到答案的方法。

Hope anyone can light my path.希望任何人都能照亮我的道路。

UPDATE:更新:

image removed as problem is solved问题解决后图像被删除

The manner I have used before is to use a wild color (a color no one in their right mind would use) for the BackColor and then set the transparency key to that.我之前使用的方式是为 BackColor 使用一种百搭色(一种没有正常头脑的人会使用的颜色),然后将透明度键设置为该颜色。

this.BackColor = Color.LimeGreen;
this.TransparencyKey = Color.LimeGreen;

A simple solution to get a transparent background in a windows form is to overwrite the OnPaintBackground method like this:在 Windows 窗体中获得透明背景的一个简单解决方案是像这样覆盖OnPaintBackground方法:

protected override void OnPaintBackground(PaintEventArgs e)
{
    //empty implementation
}

(Notice that the base.OnpaintBackground(e) is removed from the function) (注意base.OnpaintBackground(e)已从函数中移除)

I've tried the solutions above (and also) many other solutions from other posts.我已经尝试了上述(以及)其他帖子中的许多其他解决方案。

In my case, I did it with the following setup:就我而言,我使用以下设置进行了操作:

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 */ }
}

As you can see, this is a mix of previously given answers.如您所见,这是先前给出的答案的混合。

Here was my solution:这是我的解决方案:

In the constructors add these two lines:在构造函数中添加这两行:

this.BackColor = Color.LimeGreen;
this.TransparencyKey = Color.LimeGreen;

In your form, add this method:在您的表单中,添加此方法:

protected override void OnPaintBackground(PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.LimeGreen, e.ClipRectangle);
}

Be warned, not only is this form fully transparent inside the frame, but you can also click through it.请注意,此表单不仅在框架内完全透明,而且您还可以单击它。 However, it might be cool to draw an image onto it and make the form able to be dragged everywhere to create a custom shaped form.但是,在其上绘制图像并使表单能够随处拖动以创建自定义形状的表单可能很酷。

My solution was extremely close to Joel's (Not Etherton, just plain 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 */ }
}

I had drawn a splash screen (32bpp BGRA) with "transparent" background color in VS2013 and put a pictureBox in a form for display.我在 VS2013 中绘制了一个带有“透明”背景颜色的闪屏(32bpp BGRA),并将一个图片框放在一个表单中进行显示。 For me a combination of above answers worked:对我来说,上述答案的组合有效:

public Form1()
{
    InitializeComponent();

    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    this.BackColor = this.pictureBox1.BackColor;
    this.TransparencyKey = this.pictureBox1.BackColor;
}

So make sure you use the same BackColor everywhere and set that color as the TransparencyKey.因此,请确保在任何地方都使用相同的 BackColor,并将该颜色设置为 TransparencyKey。

I tried almost all of this.我几乎尝试了所有这些。 but still couldn't work.但仍然无法工作。 Finally I found it was because of 24bitmap problems.最后我发现这是因为24位图问题。 If you tried some bitmap which less than 24bit.如果您尝试了一些小于 24 位的位图。 Most of those above methods should work.上述大多数方法都应该有效。

What works for me is using a specific color instead of the real ability of .png to represent transparency.对我有用的是使用特定颜色而不是 .png 的真正能力来表示透明度。

So, what you can do is take your background image, and paint the transparent area with a specific color (Magenta always seemed appropriate to me...).因此,您可以做的是获取背景图像,并用特定颜色绘制透明区域(洋红色对我来说似乎总是合适的......)。

Set the image as the Form's BackgrounImage property, and set the color as the Form's TransparencyKey .将图像设置为 Form 的BackgrounImage属性,并将颜色设置为 Form 的TransparencyKey No need for changes in the Control's style, and no need for BackColor.不需要更改控件的样式,也不需要 BackColor。

I've tryed it right now and it worked for me...我现在已经尝试过了,它对我有用......

Should it have anything to do with "opacity" of the form / its background ? 它应该与表格/背景的“不透明度”有关吗? Did you try opacity = 0 你尝试过opacity = 0吗?

Also see if this CP article helps: 另请参阅此CP文章是否有帮助:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM