无法在 C# 中创建按钮渐变

帕特里克·朔姆伯格

我试图通过扩展 System.Windows.Forms.button 类在 c# 中添加渐变。我遇到的问题是它不会在设计或运行时向按钮添加渐变,尽管设计器确实选择了颜色 1、颜色 2 和角度字段,因此它肯定会被正确覆盖和实例化。我错过了什么吗?

class ps_button : System.Windows.Forms.Button
{
    private Color _Color1 = Color.FromArgb(255, 224, 138, 25);
    private Color _Color2 = Color.FromArgb(255, 245, 202, 134);
    private float _ColorAngle = 45f;

    public Color Color1
    {
        get { return _Color1; }
        set
        {
            _Color1 = value;
            this.Invalidate(); // Tell the Form to repaint itself
        }
    }

    public Color Color2
    {
        get { return _Color2; }
        set
        {
            _Color2 = value;
            this.Invalidate(); // Tell the Form to repaint itself
        }
    }

    public float ColorAngle
    {
        get { return _ColorAngle; }
        set
        {
            _ColorAngle = value;
            this.Invalidate(); // Tell the Form to repaint itself
        }

    }
    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // Getting the graphics object
        Graphics g = pevent.Graphics;

        // Creating the rectangle for the gradient
        Rectangle rBackground = new Rectangle(0, 0,
                                  this.Width, this.Height);

        // Creating the lineargradient
        System.Drawing.Drawing2D.LinearGradientBrush bBackground
            = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground,
                                              _Color1, _Color2, _ColorAngle);

        // Draw the gradient onto the form
        g.FillRectangle(bBackground, rBackground);

        // Disposing of the resources held by the brush
        bBackground.Dispose();
    }
}
网络

您需要覆盖该OnPaint()函数并使用 GDI+ 绘制渐变。

以下几行内容:

protected override void OnPaint(PaintEventArgs pevent)
{
  base.OnPaint(pevent);

  pevent.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(
    new PointF(0, this.Height / 2), new PointF(this.Width, this.Height / 2),
    Color.Red, Color.White), this.ClientRectangle);
}

这将从按钮的左边缘到右边缘绘制一个水平线性渐变(红色到白色)。请注意,我在上面的代码中使用了常量颜色。你应该用你的属性替换它们。同样,如果您想支持渐变角度,请使用简单的数学运算来计算渐变的起点和终点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法创建渐变材质设计按钮

来自分类Dev

无法在 C 中创建目录?

来自分类Dev

无法在C#中创建用户控件

来自分类Dev

无法从表单中删除动态创建的按钮

来自分类Dev

无法为按钮设置渐变。安卓

来自分类Dev

无法在VS 2015中创建C ++ ConsoleApplication或C ++ EmptyProject

来自分类Dev

无法递归创建C ++数组

来自分类Dev

C ++ CreateWindowEx()无法创建窗口

来自分类Dev

Selenium C#WebDriver中无法获取“编辑”按钮

来自分类Dev

无法修改C#ASP.NET中的“表单”按钮

来自分类Dev

片段着色器无法在OpenGL GLSL中创建像光一样的渐变

来自分类Dev

无法专注于C#中动态创建的列表框

来自分类Dev

无法在C中创建fuilscreen控制台窗口

来自分类Dev

无法在C ++类中创建构造函数

来自分类Dev

无法在Android Studio中创建新的C ++类

来自分类Dev

无法在C#中创建快捷方式

来自分类Dev

无法在C中创建fuilscreen控制台窗口

来自分类Dev

无法在C中创建包含结构数组的结构

来自分类Dev

无法在C ++中创建二进制搜索树

来自分类Dev

无法使用C#在SQL Server中创建虚拟表

来自分类Dev

在 c 中创建的 minishell 无法按预期工作,与管道相关

来自分类Dev

在 C 中创建 Set ADT 时无法删除重复值

来自分类Dev

在筛选GridView后无法检测GridView是否为空,因此无法在C#中隐藏按钮

来自分类Dev

恐惧无法在C中工作

来自分类Dev

无法在C中释放内存

来自分类Dev

无法在C ++中索引矩阵?

来自分类Dev

在C中无法打开文件

来自分类Dev

无法在C ++中打印变量a

来自分类Dev

无法在C中释放内存