简体   繁体   English

用于按钮,面板的C#透明PNG ...如何?

[英]C# transparent PNG for buttons, panels…how?

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. 这就是我所需要的:在我的应用程序中,我需要为工具栏和可拖动的可视对象表示使用透明的PNG图标,即72x72的“页面”图标,可以将其拖动到客户区域的所有元素上,也可以在其上方拖动。 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. 第一次我在考虑使用按钮,将其BackImage设置为透明PNG并将BackColor设置为“透明”:它将不起作用,按钮后面始终显示纯色。 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. 至于面板,同样的问题:我可以将透明的PNG放置为背景图像,但是在PNG具有透明区域的情况下,控件从不显得“透明”。 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! 我不在乎是否需要创建某种自定义的“图像按钮”或“图像面板”(无论如何)以具有真正的PNG透明按钮,面板等! Also, please note, it is about PNG transparency, using the alpha channel, not transparent pixels, which at this ages, sucks IMHO for decent GUIs. 另外,请注意,这与PNG透明度有关,它使用Alpha通道而不是透明像素,在这个年龄段中,IMHO对于不错的GUI来说很糟糕。

Cheers 干杯

litium ium

Ok I found the following code whith works not only for panels but also for buttons and I guess other controls --except PictureBox: 好的,我发现以下代码不仅适用于面板,而且适用于按钮,而且我猜想其他控件-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%! 为我工作100%! Doesn't seems to work for PictureBoxes. 似乎不适用于PictureBoxes。

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

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