我可以使用C#在Visual Studio 2005中创建透明按钮吗?

时间:2021-08-09 16:37:45

I have a user interface that requires placing some round buttons in a C# project with some data behind them. The buttons are System.Windows.Forms.buttons and I have used a GIF image with transparency to create them. However, the transparent areas aren't transparent. I've looked for references online but haven't found any suggestions for how to do this properly. There's some mention of doing it in Visual Studio 2008 but I need to keep this project in 2005. Any help or suggestion is appreciated.

我有一个用户界面,需要在C#项目中放置一些圆形按钮,后面有一些数据。按钮是System.Windows.Forms.buttons,我使用了透明的GIF图像来创建它们。但是,透明区域不透明。我在线寻找参考文献,但没有找到任何关于如何正确执行此操作的建议。有一些提到在Visual Studio 2008中这样做但我需要在2005年保留这个项目。任何帮助或建议都表示赞赏。

8 个解决方案

#1


1  

I'm pretty sure you need to use PNGs with WinForms to get image transparency. I know I've used them successfully.

我很确定你需要使用带有WinForms的PNG来获得图像透明度。我知道我已成功使用它们。

EDIT: When I used the PNGs, I was overlaying them with the Image control onto the Form1.BackgroundImage; I wasn't using them in buttons.

编辑:当我使用PNG时,我将它们与Image控件叠加到Form1.BackgroundImage上;我没有在按钮中使用它们。

I think your best bet is to switch from using a button control to using an image control. You might also try changing the button style to flat (I think it was flat, maybe one of the other styles) and seeing if you can get the effect you want that way.

我认为你最好的选择是从使用按钮控件切换到使用图像控件。您也可以尝试将按钮样式更改为扁平(我认为它是扁平的,也许是其他样式之一)并且看看您是否可以获得您想要的效果。

#2


1  

Try this:

        Bitmap temp = new Bitmap(button1.Image);

        temp.MakeTransparent(Color.Black); //your transparent color, in this case black

        button1.Image = (Image) Logo;

        temp.Dispose();

        temp = null;

#3


1  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

public class TransparentControl : Control
{
    public TransparentControl()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        SetStyle(ControlStyles.Opaque, true);
        SetStyle(ControlStyles.ResizeRedraw, true);
        this.BackColor = Color.Transparent;
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        Graphics g = pevent.Graphics;
        g.DrawRectangle(Pens.Black, this.ClientRectangle);
    }


    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // don't call the base class
        //base.OnPaintBackground(pevent);
    }


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

    // rest of class here...
}

#4


1  

Just set the Button's FlatStyle property to "Flat"…and voilà! I know this works with PNGs, but I haven't tested it with GIFs.

只需将Button的FlatStyle属性设置为“Flat”......即可!我知道这适用于PNG,但我没有用GIF测试它。

#5


0  

You need to set BackColor property of a button to "Transparent".

您需要将按钮的BackColor属性设置为“透明”。

Button1.BackColor = System.Drawing.Color.Transparent;

Button1.BackColor = System.Drawing.Color.Transparent;

#6


0  

create a class derived from button and put this code.

创建一个派生自按钮的类并放入此代码。

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

and in create method

并在创建方法

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

#7


0  

I am using Visual Studio 2005 and tried following your example above but it did not work (the snippet in create method). I have a background for my form and am trying to create bevelled transpared buttons.

我正在使用Visual Studio 2005并尝试按照上面的示例进行操作,但它不起作用(create方法中的代码段)。我有一个表格的背景,我正在尝试创建斜面透明按钮。

currently inserting the above code makes my background transparent and my buttons all white in color.

目前插入上面的代码使我的背景透明,我的按钮全白色。

here is a snippet of my code

这是我的代码片段

       Bitmap mainFormBackground = (Bitmap)Image.FromFile("background.jpg");
        this.BackgroundImage = mainFormBackground;
        this.BackColor = Color.Black;
        this.ForeColor = Color.White;

Add some buttons

添加一些按钮

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

I even tried

我甚至试过了

         btnName.BackColor = Color.Transpare;

this too did not work

这也行不通

#8


0  

The following answer is verified in Visual Studio 2012 and works with backgroundImage format BMP, GIF, PNG as well as JPG; created by MS Paint.

以下答案在Visual Studio 2012中得到验证,并与backgroundImage格式BMP,GIF,PNG以及JPG一起使用;由MS Paint创建。

To make your background visible through your button:

要通过按钮显示背景:

Go to the properties window of the button. Then change the FlatStyle and BackColor as shown in the image below:

转到按钮的属性窗口。然后更改FlatStyle和BackColor,如下图所示:

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

Note: The color is to be chosen from Web Tab and The FlatStyle is to be chosen as Flat.

注意:颜色将从Web Tab中选择,FlatStyle将选择为Flat。

But after this, the button will be transparent till mouse do not hover or it is selected or pressed. On that condition, it will be in some non-transparent color. If you want to make it always transparent, follow the following images and change the corresponding colors to transparent as before. The colors to be changed are: 'MouseOverBackColor' & 'MouseDownBackColor'.

但在此之后,按钮将是透明的,直到鼠标不悬停或选择或按下它。在这种情况下,它会呈现一些不透明的颜色。如果要使其始终透明,请按照以下图像将相应的颜色更改为透明,如前所述。要更改的颜色是:'MouseOverBackColor'和'MouseDownBackColor'。

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

Note: If you choose always transparent, as shown, there will be no change in the appearance of the button while it will be in action!

注意:如果您选择始终透明,如图所示,按钮的外观将不会发生变化!

Hope it will help you.

希望它会对你有所帮助。

Best of luck!

祝你好运!

#1


1  

I'm pretty sure you need to use PNGs with WinForms to get image transparency. I know I've used them successfully.

我很确定你需要使用带有WinForms的PNG来获得图像透明度。我知道我已成功使用它们。

EDIT: When I used the PNGs, I was overlaying them with the Image control onto the Form1.BackgroundImage; I wasn't using them in buttons.

编辑:当我使用PNG时,我将它们与Image控件叠加到Form1.BackgroundImage上;我没有在按钮中使用它们。

I think your best bet is to switch from using a button control to using an image control. You might also try changing the button style to flat (I think it was flat, maybe one of the other styles) and seeing if you can get the effect you want that way.

我认为你最好的选择是从使用按钮控件切换到使用图像控件。您也可以尝试将按钮样式更改为扁平(我认为它是扁平的,也许是其他样式之一)并且看看您是否可以获得您想要的效果。

#2


1  

Try this:

        Bitmap temp = new Bitmap(button1.Image);

        temp.MakeTransparent(Color.Black); //your transparent color, in this case black

        button1.Image = (Image) Logo;

        temp.Dispose();

        temp = null;

#3


1  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

public class TransparentControl : Control
{
    public TransparentControl()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        SetStyle(ControlStyles.Opaque, true);
        SetStyle(ControlStyles.ResizeRedraw, true);
        this.BackColor = Color.Transparent;
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        Graphics g = pevent.Graphics;
        g.DrawRectangle(Pens.Black, this.ClientRectangle);
    }


    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // don't call the base class
        //base.OnPaintBackground(pevent);
    }


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

    // rest of class here...
}

#4


1  

Just set the Button's FlatStyle property to "Flat"…and voilà! I know this works with PNGs, but I haven't tested it with GIFs.

只需将Button的FlatStyle属性设置为“Flat”......即可!我知道这适用于PNG,但我没有用GIF测试它。

#5


0  

You need to set BackColor property of a button to "Transparent".

您需要将按钮的BackColor属性设置为“透明”。

Button1.BackColor = System.Drawing.Color.Transparent;

Button1.BackColor = System.Drawing.Color.Transparent;

#6


0  

create a class derived from button and put this code.

创建一个派生自按钮的类并放入此代码。

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

and in create method

并在创建方法

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

#7


0  

I am using Visual Studio 2005 and tried following your example above but it did not work (the snippet in create method). I have a background for my form and am trying to create bevelled transpared buttons.

我正在使用Visual Studio 2005并尝试按照上面的示例进行操作,但它不起作用(create方法中的代码段)。我有一个表格的背景,我正在尝试创建斜面透明按钮。

currently inserting the above code makes my background transparent and my buttons all white in color.

目前插入上面的代码使我的背景透明,我的按钮全白色。

here is a snippet of my code

这是我的代码片段

       Bitmap mainFormBackground = (Bitmap)Image.FromFile("background.jpg");
        this.BackgroundImage = mainFormBackground;
        this.BackColor = Color.Black;
        this.ForeColor = Color.White;

Add some buttons

添加一些按钮

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

I even tried

我甚至试过了

         btnName.BackColor = Color.Transpare;

this too did not work

这也行不通

#8


0  

The following answer is verified in Visual Studio 2012 and works with backgroundImage format BMP, GIF, PNG as well as JPG; created by MS Paint.

以下答案在Visual Studio 2012中得到验证,并与backgroundImage格式BMP,GIF,PNG以及JPG一起使用;由MS Paint创建。

To make your background visible through your button:

要通过按钮显示背景:

Go to the properties window of the button. Then change the FlatStyle and BackColor as shown in the image below:

转到按钮的属性窗口。然后更改FlatStyle和BackColor,如下图所示:

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

Note: The color is to be chosen from Web Tab and The FlatStyle is to be chosen as Flat.

注意:颜色将从Web Tab中选择,FlatStyle将选择为Flat。

But after this, the button will be transparent till mouse do not hover or it is selected or pressed. On that condition, it will be in some non-transparent color. If you want to make it always transparent, follow the following images and change the corresponding colors to transparent as before. The colors to be changed are: 'MouseOverBackColor' & 'MouseDownBackColor'.

但在此之后,按钮将是透明的,直到鼠标不悬停或选择或按下它。在这种情况下,它会呈现一些不透明的颜色。如果要使其始终透明,请按照以下图像将相应的颜色更改为透明,如前所述。要更改的颜色是:'MouseOverBackColor'和'MouseDownBackColor'。

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

我可以使用C#在Visual Studio 2005中创建透明按钮吗?

Note: If you choose always transparent, as shown, there will be no change in the appearance of the button while it will be in action!

注意:如果您选择始终透明,如图所示,按钮的外观将不会发生变化!

Hope it will help you.

希望它会对你有所帮助。

Best of luck!

祝你好运!