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

📄 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.IO;
using System.Runtime.Serialization.Formatters.Binary ; 

namespace Serialize
{
	/// <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.btnSave = new System.Windows.Forms.Button();
			this.btnClear = new System.Windows.Forms.Button();
			this.btnShow = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// btnSave
			// 
			this.btnSave.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
			this.btnSave.Location = new System.Drawing.Point(32, 216);
			this.btnSave.Name = "btnSave";
			this.btnSave.TabIndex = 0;
			this.btnSave.Text = "保  存";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click_1);
			// 
			// btnClear
			// 
			this.btnClear.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
			this.btnClear.Location = new System.Drawing.Point(128, 216);
			this.btnClear.Name = "btnClear";
			this.btnClear.TabIndex = 1;
			this.btnClear.Text = "清  除";
			this.btnClear.Click += new System.EventHandler(this.btnClear_Click_1);
			// 
			// btnShow
			// 
			this.btnShow.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
			this.btnShow.Location = new System.Drawing.Point(224, 216);
			this.btnShow.Name = "btnShow";
			this.btnShow.TabIndex = 2;
			this.btnShow.Text = "显  示";
			this.btnShow.Click += new System.EventHandler(this.btnShow_Click_1);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.White;
			this.ClientSize = new System.Drawing.Size(336, 270);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnShow,
																		  this.btnClear,
																		  this.btnSave});
			this.Name = "Form1";
			this.Text = "序列化和反序列化";
			this.Load += new System.EventHandler(this.Form1_Load_1);
			this.ResumeLayout(false);

		}
		#endregion

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

		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnClear;
		private System.Windows.Forms.Button btnShow;

		private ArrayList points=new ArrayList();
		private string FileName = Application.StartupPath + "\\Points.Pnt";

		private void AddPoints()
		{
			System.Random rp=new System.Random();
			for (int i = 0;i<=49;i++)
			{
				float ValueX=rp.Next(500);
				float ValueY=rp.Next(150);
				Console.WriteLine(ValueX);
				CPosition p=new CPosition(ValueX, ValueY);
				points.Add(p);
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			Graphics g= e.Graphics;
			Draw(g);
		}

		private void Draw(Graphics g)
		{
			for (int i = 0;i<=points.Count - 1;i++)
			{
				((CPosition)(points[i])).Draw(g);
			}
		}

		private void Form1_Load_1(object sender, System.EventArgs e)
		{
			AddPoints();
			Console.WriteLine(points.Count );
		}

		private void btnSave_Click_1(object sender, System.EventArgs e)
		{
			FileStream stream=new System.IO.FileStream(FileName, System.IO.FileMode.Create);
			BinaryFormatter binary=new BinaryFormatter();
			binary.Serialize(stream, points);
			stream.Close();
		}

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

		private void btnShow_Click_1(object sender, System.EventArgs e)
		{
			FileStream stream=new FileStream(FileName, 
							  System.IO.FileMode.Open);
			BinaryFormatter binary=new BinaryFormatter();
			points = checked((ArrayList)(binary.Deserialize(stream)));
			stream.Close();
			Graphics g = this.CreateGraphics();
			Draw(g);		
		}
	}
}

⌨️ 快捷键说明

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