使用LinearGradientBrush类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
                Color.White, Color.Black,
                LinearGradientMode.Vertical);
            g.FillRectangle(brush, this.ClientRectangle);


        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
除了 Color 和 SystemColors,C# 中还有其他表示颜色的,比如 Brush、Pen 等。它们都是 System.Drawing 命名空间中的,用于在界面中绘制图形或填充颜色等。 1. Brush Brush 用于填充颜色或图案。它是一个抽象,不能直接实例化,需要使用其子来创建具体的 Brush 对象,比如 SolidBrushLinearGradientBrush、TextureBrush 等。 使用 Brush 填充颜色很简单,只需创建一个具体的 Brush 对象,然后调用 Graphics 对象的 FillRectangle、FillEllipse 等方法即可。例如: ``` // 创建一个红色的 SolidBrush 对象 SolidBrush redBrush = new SolidBrush(Color.Red); // 在界面上绘制一个矩形,并填充为红色 Graphics g = this.CreateGraphics(); g.FillRectangle(redBrush, new Rectangle(0, 0, 100, 100)); ``` 2. Pen Pen 用于绘制线条或边框。它也是一个抽象,需要使用其子来创建具体的 Pen 对象,比如 SolidPen、DashPen、DotPen 等。 使用 Pen 绘制线条或边框也很简单,只需创建一个具体的 Pen 对象,然后调用 Graphics 对象的 DrawLine、DrawRectangle、DrawEllipse 等方法即可。例如: ``` // 创建一个蓝色的 SolidPen 对象 Pen bluePen = new Pen(Color.Blue); // 在界面上绘制一个矩形的边框 Graphics g = this.CreateGraphics(); g.DrawRectangle(bluePen, new Rectangle(0, 0, 100, 100)); ``` 需要注意的是,使用 Brush 或 Pen 绘制图形或填充颜色时,需要及时释放资源,避免内存泄漏。可以使用 using 语句来自动释放资源,例如: ``` using (SolidBrush redBrush = new SolidBrush(Color.Red)) { // 在界面上绘制一个矩形,并填充为红色 Graphics g = this.CreateGraphics(); g.FillRectangle(redBrush, new Rectangle(0, 0, 100, 100)); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值