C#winform窗体背景渐变色

  1. 从一个方向到另一个方向的渐变色,例如从左到右,从左上到右下,在窗体代码中添加如下代码,改一下颜色和渐变方向即可
protected override void OnPaint(PaintEventArgs e)
{
   
    // 创建线性渐变画刷
    LinearGradientBrush brush = new LinearGradientBrush(
        ClientRectangle, Color.Blue, Color.Red, 
        LinearGradientMode.Vertical);
    // 填充窗体背景
    e.Graphics.FillRectangle(brush, ClientRectangle);
    base.OnPaint(e);
}
  1. 以某一点为圆心的渐变色,这是我用的方法的原理
    在这里插入图片描述
    代码如下:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DisplayBoard.Method
{
   
    public class SetBGColor
    {
   
        //设置渐变色
        public static void SetGradientBackground(Form form,float circleX, float circleY, Color outSide, Color inSide)
        {
   
            // 创建 GraphicsPath 以定义椭圆的形状
            GraphicsPath path = new GraphicsPath();
            float x = form.ClientRectangle.Left - form.ClientRectangle.Width / 2; 
            float y = form.ClientRectangle.Top - form.ClientRectangle.Height * 0.3f; 
            float width = form.ClientRectangle.Width * 2; 
            float height = form.ClientRectangle.Height * 1.5f;
            RectangleF rectangle = new RectangleF(//横纵坐标、宽高
                x,
                y,
                width,
                height);
            path.AddEllipse(rectangle);
            PathGradientBrush brush = new PathGradientBrush(path);
            // 设置渐变颜色
            Color[] gradientColors = new Color[]
            {
   
                outSide
            };
            brush.CenterColor = inSide;
            // 设置中心颜色(浅色)
            brush.SurroundColors = gradientColors;
            // 设置中心点
            PointF centerPoint = new PointF(circleX, circleY);
            brush.CenterPoint = centerPoint;
            // 绘制渐变背景
            form.BackgroundImage = new Bitmap(form.Width, form.Height);
            using (Graphics graphics = Graphics.FromImage(form.BackgroundImage))
            {
   
                graphics.FillRectangle(brush, form.ClientRectangle);
            }
        }
    }
}

调用:

namespace DisplayBoard
{
   
    public partial class FrmHome : Form
    {
   
        public FrmHome()
        {
   
            InitializeComponent();
            DoubleBuffered = true;//启用双缓冲
            SetBGColor.SetGradientBackground1(this,0,0,Color.Red,Color.Green);
        }
    }
}

效果:
在这里插入图片描述
改一下色彩中心位置
在这里插入图片描述
在这里插入图片描述
这方法会导致窗体缩放比较卡,可能是因为缩放窗体背景频繁重画,目前还不知道怎么解决。

相关推荐

  1. android xml 定义变色背景

    2023-12-28 11:42:02       7 阅读
  2. CSS中背景设置——变色和放射渐变

    2023-12-28 11:42:02       28 阅读
  3. wpf背景添加径向渐变效果实现

    2023-12-28 11:42:02       6 阅读
  4. el-progress变色

    2023-12-28 11:42:02       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-28 11:42:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-28 11:42:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-28 11:42:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-28 11:42:02       18 阅读

热门阅读

  1. 贪心算法的运用

    2023-12-28 11:42:02       34 阅读
  2. el-select可输入下拉框限制长度

    2023-12-28 11:42:02       31 阅读
  3. vue 预览 pdf、word、excel

    2023-12-28 11:42:02       41 阅读
  4. Vue 3 中安装并使用 Axios 详细步骤+样例代码详解

    2023-12-28 11:42:02       31 阅读
  5. Ajax

    2023-12-28 11:42:02       28 阅读
  6. PDF.js介绍以及使用

    2023-12-28 11:42:02       38 阅读
  7. PDF是什么格式的文件

    2023-12-28 11:42:02       37 阅读
  8. Unity相机跟随角色移动

    2023-12-28 11:42:02       33 阅读