⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 苏金明编写的《用VB.NET和VC#.NET开发交互式CAD系统》一书的源代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace 渐变
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.Size = new System.Drawing.Size(300,300);
			this.Text = "Form1";
		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			Graphics g=e.Graphics;
			//定义一个线性梯度刷
			LinearGradientBrush lGBrush=new LinearGradientBrush( 
				new Point(0, 10), 
				new Point(150, 10),
				Color.FromArgb(255, 255, 0, 0),
				Color.FromArgb(255, 0, 255, 0));
			Pen pen=new Pen(lGBrush);
			//用线性梯度刷效果的笔绘制一条直线段,填充一个矩形
			g.DrawLine(pen, 10, 130, 500, 130);
			g.FillRectangle(lGBrush, 50, 150, 370, 30);

			//定义路径并添加一个椭圆
			GraphicsPath gp=new GraphicsPath();
			gp.AddEllipse(10, 10, 200, 100);
			//用该路径定义路径梯度刷
			PathGradientBrush brush=new PathGradientBrush(gp);
			//颜色数组
			Color[] colors= {
					Color.FromArgb(255, 255, 0, 0),
					Color.FromArgb(255, 100, 100, 100),
					Color.FromArgb(255, 0, 255, 0),
					Color.FromArgb(255, 0, 0, 255)};
			//定义颜色渐变比率
			float[] r = {
					0.0F,
					0.3F,
					0.6F,
					1.0F};
			ColorBlend blend=new ColorBlend();
			blend.Colors = colors;
			blend.Positions = r;
			brush.InterpolationColors = blend;
			//在椭圆外填充一个矩形
			g.FillRectangle(brush, 0, 0, 210, 110);

			//用添加了椭圆的路径定义第二个路径梯度刷
			GraphicsPath gp2=new GraphicsPath();
			gp2.AddEllipse(300, 0, 200, 100);
			PathGradientBrush brush2=new PathGradientBrush(gp2);
			//设置中心点的位置和颜色
			brush2.CenterPoint = new PointF(450, 50);
			brush2.CenterColor = Color.FromArgb(255, 0, 255, 0);
			//设置边界颜色
			Color[] colors2 = {Color.FromArgb(255, 255, 0, 0)};
			brush2.SurroundColors = colors2;
			//用第二个梯度刷填充椭圆
			g.FillEllipse(brush2, 300, 0, 200, 100);
	    }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -