本文介绍了用于按钮,面板的C#透明PNG ...如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索很多东西,看到了一些例子,但它们至少对我没有用.这就是我所需要的:在我的应用程序中,我需要为工具栏和可拖动的可视对象表示使用透明的PNG图标,即72x72的页面"图标,可以将其拖动到客户区域的所有元素上,也可以在其上方拖动.第一次,我在考虑使用按钮,将其BackImage设置为透明PNG并将BackColor设置为透明":它将不起作用,按钮后面始终显示纯色.对于面板,存在相同的问题:我可以将透明的PNG放置为背景图像,但是在PNG具有透明区域的情况下,控件永远不会看起来是透明的".我认为图片框和其他任何允许图像背景的控件都一样.所以我想,这真的是要使控件的背景透明...有什么想法吗?

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?

我不在乎是否需要创建某种自定义的图像按钮"或图像面板"(无论如何)以具有真正的PNG透明按钮,面板等!另外,请注意,这与PNG透明度有关,它使用Alpha通道而不是透明像素,在这个年龄段中,IMHO对于不错的GUI来说很糟糕.

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.

欢呼

litium

推荐答案

好吧,我发现以下代码不仅适用于面板,而且适用于按钮,而且我猜想其他控件-PictureBox除外:

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

为我工作100%!似乎不适用于PictureBoxes.

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

这篇关于用于按钮,面板的C#透明PNG ...如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:51
查看更多