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

📄 form1.cs

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

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.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.line = new System.Windows.Forms.Button();
			this.ellipse = new System.Windows.Forms.Button();
			this.arc = new System.Windows.Forms.Button();
			this.poly = new System.Windows.Forms.Button();
			this.curve = new System.Windows.Forms.Button();
			this.clear = new System.Windows.Forms.Button();
			this.close = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.Color.White;
			this.pictureBox1.Location = new System.Drawing.Point(8, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(408, 264);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// line
			// 
			this.line.Location = new System.Drawing.Point(16, 296);
			this.line.Name = "line";
			this.line.Size = new System.Drawing.Size(40, 24);
			this.line.TabIndex = 1;
			this.line.Text = "直线";
			this.line.Click += new System.EventHandler(this.line_Click);
			// 
			// ellipse
			// 
			this.ellipse.Location = new System.Drawing.Point(72, 296);
			this.ellipse.Name = "ellipse";
			this.ellipse.Size = new System.Drawing.Size(40, 24);
			this.ellipse.TabIndex = 2;
			this.ellipse.Text = "椭圆";
			this.ellipse.Click += new System.EventHandler(this.ellipse_Click);
			// 
			// arc
			// 
			this.arc.Location = new System.Drawing.Point(128, 296);
			this.arc.Name = "arc";
			this.arc.Size = new System.Drawing.Size(40, 24);
			this.arc.TabIndex = 3;
			this.arc.Text = "圆弧";
			this.arc.Click += new System.EventHandler(this.arc_Click);
			// 
			// poly
			// 
			this.poly.Font = new System.Drawing.Font("宋体", 7.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.poly.Location = new System.Drawing.Point(184, 296);
			this.poly.Name = "poly";
			this.poly.Size = new System.Drawing.Size(48, 24);
			this.poly.TabIndex = 4;
			this.poly.Text = "多义线";
			this.poly.Click += new System.EventHandler(this.poly_Click);
			// 
			// curve
			// 
			this.curve.Location = new System.Drawing.Point(248, 296);
			this.curve.Name = "curve";
			this.curve.Size = new System.Drawing.Size(40, 24);
			this.curve.TabIndex = 5;
			this.curve.Text = "曲线";
			this.curve.Click += new System.EventHandler(this.curve_Click);
			// 
			// clear
			// 
			this.clear.Location = new System.Drawing.Point(304, 296);
			this.clear.Name = "clear";
			this.clear.Size = new System.Drawing.Size(40, 24);
			this.clear.TabIndex = 6;
			this.clear.Text = "清除";
			this.clear.Click += new System.EventHandler(this.clear_Click);
			// 
			// close
			// 
			this.close.Location = new System.Drawing.Point(360, 296);
			this.close.Name = "close";
			this.close.Size = new System.Drawing.Size(40, 24);
			this.close.TabIndex = 7;
			this.close.Text = "关闭";
			this.close.Click += new System.EventHandler(this.close_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(424, 342);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.close,
																		  this.clear,
																		  this.curve,
																		  this.poly,
																		  this.arc,
																		  this.ellipse,
																		  this.line,
																		  this.pictureBox1});
			this.Name = "Form1";
			this.Text = "GDI+绘图";
			this.ResumeLayout(false);

		}
		#endregion

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

		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button line;
		private System.Windows.Forms.Button ellipse;
		private System.Windows.Forms.Button arc;
		private System.Windows.Forms.Button poly;
		private System.Windows.Forms.Button curve;
		private System.Windows.Forms.Button clear;
		private System.Windows.Forms.Button close; 
		
		private Graphics g;

		private void line_Click(object sender, System.EventArgs e)
		{
			g = pictureBox1.CreateGraphics();
			Pen pen1=new Pen(Color.Blue, 3);
			pen1.DashStyle = DashStyle.DashDot;
			g.DrawLine(pen1, 10, 10, 200, 200);
			Pen pen2=new Pen(Color.Red);
			pen2.EndCap = LineCap.ArrowAnchor;
			g.DrawLine(pen2, 200, 50, 20, 150);
		}

		private void ellipse_Click(object sender, System.EventArgs e)
		{
			g = pictureBox1.CreateGraphics();
			g.DrawEllipse(Pens.Red, 30, 50, 300, 200);
		}

		private void arc_Click(object sender, System.EventArgs e)
		{
			g = pictureBox1.CreateGraphics();
			Pen pen=new Pen(Color.FromArgb(100, 200, 0, 200), 2);
			g.DrawArc(pen, 100, 40, 100, 100, 30, 290);
		}

		private void poly_Click(object sender, System.EventArgs e)
		{
			g = pictureBox1.CreateGraphics();
			PointF[] points= new PointF[] { 
						new PointF(90, 10), new PointF(300, 100), 
						new PointF(150, 200), new PointF(100, 80)};
			g.DrawPolygon(Pens.BlueViolet, points);
		}

		private void curve_Click(object sender, System.EventArgs e)
		{
			g = pictureBox1.CreateGraphics();
			Pen pen=new Pen(Color.Cyan, 2);
			pen.DashStyle =DashStyle.DashDotDot;
			g.DrawBezier(pen, new PointF(90, 10), new PointF(300, 100),
						new PointF(150, 200), new PointF(100, 80));
			PointF[] points = new PointF[] { 
					new PointF(90, 10), new PointF(300, 100), 
					new PointF(150, 200), new PointF(100, 80)};
			g.DrawCurve(Pens.Blue, points);
		}

		private void clear_Click(object sender, System.EventArgs e)
		{
			g = pictureBox1.CreateGraphics();
			g.Clear(Color.White);
		}

		private void close_Click(object sender, System.EventArgs e)
		{
			this.Dispose();
		}


	}
}

⌨️ 快捷键说明

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