views:

7119

answers:

7

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.

+1  A: 

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

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.

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.

amdfan
Thanks, amdfan. I'll see if I can get this to work.
Sean
A: 

Thanks for the response, amdfan. I've tried setting the following

this.leftbutton.Image = Image.FromFile(@"C:\foo\images\LeftCornerCircle.png");

and the PNG file definitely has transparency in Photoshop but the transparent area shows up as white (making a rectangle around the button) instead of showing the pixels underneath the button.

Is that what you meant?

Sean
A: 

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

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

A: 

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;
A: 

hi

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.

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

A: 

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...

}

Hasan
A: 

Microsoft Expression Blend 2

gt