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

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

问题描述

我正在尝试在 C# (.NET 3.5 SP1) 中创建一个透明按钮,以便在我的 WinForms 应用程序中使用.我已经尝试了所有方法来使按钮透明(它应该在按钮下方显示渐变背景),但它不起作用.

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

}

问题是按钮似乎从某处获取随机 UI 内存并用 Visual Studio 的 UI 中的一些缓冲区填充自身(在设计模式下).在运行时,它会抓取一些归零的缓冲区,并且完全是黑色的.

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.

有什么想法吗?

谢谢大家,以下对我有用:

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(和底层 User32)根本不支持透明度.然而,WinForms 可以通过使用您提供的控件样式来模拟透明度 - SupportsTransparentBackColor,但在这种情况下,所有透明"控件所做的都是允许绘制父控件的背景.

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 使用了一些阻止此机制工作的窗口样式.我看到了两种解决方案:一种是从 Control 派生控件(而不是 ButtonBase),第二种是使用 Parent 的 DrawToBitmap 获取按钮下的背景,然后在 OnPaint 中绘制此图像.

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天全站免登陆