1
votes

I have being searching a lot, seen a few examples but they don't work at least for me. This is what I need: in my application I need to use transparent PNG icons for the toolbars and also for draggable visual objects representations, ie, 72x72 "page" icon which can be dragged around and possibly over all elements in the client area. For the first I was thinking about using a button, set its BackImage to the transparent PNG and put BackColor as "transparent": it won't work, the button always show a solid color behind. As for the panel, the same problem: I can put a transparent PNG as background image but the control never looks "transparent" where the PNG has transparent areas. I think the same with a picturebox and any other control allowing image backgrounds. So I guess, it is really about making a control's background transparent...Any ideas?

I don't care if I need to create some sort of custom "image button" or "image panel" --whatever-- to have truely PNG transparent buttons, panels, etc! Also, please note, it is about PNG transparency, using the alpha channel, not transparent pixels, which at this ages, sucks IMHO for decent GUIs.

Cheers

litium

1

1 Answers

1
votes

Ok I found the following code whith works not only for panels but also for buttons and I guess other controls --except PictureBox:

public class TransparentPanel : Panel <==change to Button for instance, and works
    {
        Timer Wriggler = new Timer();
        public TransparentPanel()
        {
            Wriggler.Tick += new EventHandler(TickHandler);
            this.Wriggler.Interval = 500;
            this.Wriggler.Enabled = true;
        }
        protected void TickHandler(object sender, EventArgs e)
        {
            this.InvalidateEx();
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT 
                return cp;
            }
        }
        protected void InvalidateEx()
        {
            if (Parent == null)
            {
                return;
            }
            Rectangle rc = new Rectangle(this.Location, this.Size);
            Parent.Invalidate(rc, true);
        }
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            // Do not allow the background to be painted  
        }
    }

Works for me 100%! Doesn't seems to work for PictureBoxes.