相关疑难解决方法(0)

绘制透明按钮

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

这是我正在使用的代码:

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

}
Run Code Online (Sandbox Code Playgroud)

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

我的最终目标是在隐形按钮而不是矩形上绘制图像.然而,这个概念应该保持不变.当用户将鼠标悬停在按钮上时,则绘制按钮类型的形状.

有任何想法吗?

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

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) …
Run Code Online (Sandbox Code Playgroud)

c# gdi+ button winforms

21
推荐指数
2
解决办法
5万
查看次数

标签 统计

button ×1

c# ×1

gdi+ ×1

winforms ×1