绘制一个透明的按钮 [英] Drawing a transparent button

查看:115
本文介绍了绘制一个透明的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建在C#中(.NET 3.5 SP1)的透明按钮,在我的WinForms应用程序中使用。我已经竭尽所能,以获得按钮是透明的(它应该显示在按钮下方的渐变背景),但它只是不工作。

下面是code我使用的是:

 公共类的ImageButton:ButtonBase,IButtonControl
{
    公众的ImageButton()
    {
        this.SetStyle(
            ControlStyles.SupportsTransparentBackColor |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.ResizeRedraw |
            ControlStyles.UserPaint,真正的);
        this.BackColor = Color.Transparent;
    }    保护覆盖无效的OnPaint(PaintEventArgs的pevent)
    {
        图形G = pevent.Graphics;
        g.FillRectangle(Brushes.Transparent,this.ClientRectangle);
        g.DrawRectangle(Pens.Black,this.ClientRectangle);
    }
    //类的其余部分在这里...}

的问题是,该按钮似乎是从某处敛随机UI存储器,并与从Visual Studio的UI(在设计模式时)某些缓冲器填充本身。在运行时,它抓住了一些zero'd缓冲区,并完全黑了。

我的最终目标是要在一个看不见的按钮,而不是矩形绘制图片。然而,概念应该保持不变。当用户将鼠标悬停按钮即可一键式形状绘制。

任何想法?

编辑:谢谢大家,以下为我工作:

 公共类的ImageButton:控制,IButtonControl
{
    公众的ImageButton()
    {
        的SetStyle(ControlStyles.SupportsTransparentBackColor,真);
        的SetStyle(ControlStyles.Opaque,真);
        的SetStyle(ControlStyles.ResizeRedraw,真);
        this.BackColor = Color.Transparent;    }    保护覆盖无效的OnPaint(PaintEventArgs的pevent)
    {
        图形G = pevent.Graphics;
        g.DrawRectangle(Pens.Black,this.ClientRectangle);
    }
    保护覆盖无效OnPaintBackground(PaintEventArgs的pevent)
    {
        //不调用基类
        //base.OnPaintBackground(pevent);
    }
    保护覆盖的CreateParams的CreateParams
    {
        得到
        {
            const int的WS_EX_TRANSPARENT = 0x20的;
            的CreateParams CP = base.CreateParams;
            cp.ExStyle | = WS_EX_TRANSPARENT;
            返回CP;
        }
    }    //类的其余部分在这里...
}


解决方案

的WinForms(和基本USER32)不支持透明度。然而WinForms的可以通过控件样式模拟透明度您提供 - SupportsTransparentBackColor,但在这种情况下,所有的透明的控制呢,它允许绘图父背景

ButtonBase使用某些窗口样式prevent工作这种机制。我看到了两个解决方案:一是从得到控制(而不是ButtonBase)你的控制,二是利用家长DrawToBitmap,让您的按钮的背景下,然后在OnPaint中得出这样的图片

I'm trying to create a transparent button in C# (.NET 3.5 SP1) to use in my WinForms application. I've tried everything to get the button to be transparent (it should show the gradient background underneath the button) but it's just not working.

Here is the code I'm using:

public class ImageButton : ButtonBase, IButtonControl
{
    public ImageButton()
    {
        this.SetStyle(
            ControlStyles.SupportsTransparentBackColor | 
            ControlStyles.OptimizedDoubleBuffer | 
            ControlStyles.AllPaintingInWmPaint | 
            ControlStyles.ResizeRedraw | 
            ControlStyles.UserPaint, true);
        this.BackColor = Color.Transparent;
    }

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


    // rest of class here...

}

The problem is that the button seems to be grabbing random UI memory from somewhere and filling itself with some buffer from Visual Studio's UI (when in design mode). At runtime it's grabbing some zero'd buffer and is completely black.

My ultimate goal is to paint an image on an invisible button instead of the rectangle. The concept should stay the same however. When the user hovers over the button then a button-type shape is drawn.

Any ideas?

EDIT: Thanks everybody, the following worked for me:

public class ImageButton : Control, IButtonControl
{
    public ImageButton()
    {
        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...
}

解决方案

WinForms (and underlying User32) does not support transparency at all. WinForms however can simulate transparency by using control style you provide - SupportsTransparentBackColor, but in this case all that "transparent" control does, it to allow drawing parent its background.

ButtonBase uses some windows styles that prevent working this mechanism. I see two solutions: one is to derive your control from Control (instead of ButtonBase), and second is to use Parent's DrawToBitmap to get background under your button, and then draw this image in OnPaint.

这篇关于绘制一个透明的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆