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

📄 form1.cs

📁 simple 2D data generator
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace _2D_SetGenerator
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form {
		private System.Windows.Forms.Panel m_panel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem MenuOpenItem;
		private System.Windows.Forms.MenuItem MenuSaveItem;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.MenuItem MenuExitItem;
		private System.Collections.ArrayList m_PointArrayList;

		public MainForm() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			m_PointArrayList	=	new System.Collections.ArrayList();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.m_panel = new System.Windows.Forms.Panel();
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.MenuOpenItem = new System.Windows.Forms.MenuItem();
			this.MenuSaveItem = new System.Windows.Forms.MenuItem();
			this.MenuExitItem = new System.Windows.Forms.MenuItem();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.SuspendLayout();
			// 
			// m_panel
			// 
			this.m_panel.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.m_panel.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.m_panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.m_panel.Location = new System.Drawing.Point(8, 8);
			this.m_panel.Name = "m_panel";
			this.m_panel.Size = new System.Drawing.Size(496, 344);
			this.m_panel.TabIndex = 0;
			this.m_panel.Paint += new System.Windows.Forms.PaintEventHandler(this.m_panel_Paint);
			this.m_panel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.m_panel_MouseClick);
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.MenuOpenItem,
																					  this.MenuSaveItem,
																					  this.MenuExitItem});
			this.menuItem1.Text = "&File";
			// 
			// MenuOpenItem
			// 
			this.MenuOpenItem.Index = 0;
			this.MenuOpenItem.Text = "&Open";
			this.MenuOpenItem.Click += new System.EventHandler(this.MenuOpenItem_Click);
			// 
			// MenuSaveItem
			// 
			this.MenuSaveItem.Index = 1;
			this.MenuSaveItem.Text = "&Save";
			this.MenuSaveItem.Click += new System.EventHandler(this.MenuSaveItem_Click);
			// 
			// MenuExitItem
			// 
			this.MenuExitItem.Index = 2;
			this.MenuExitItem.Text = "E&xit";
			this.MenuExitItem.Click += new System.EventHandler(this.MenuExitItem_Click);
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.FileName = "doc1";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.Color.SteelBlue;
			this.ClientSize = new System.Drawing.Size(512, 357);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.m_panel});
			this.Menu = this.mainMenu;
			this.Name = "MainForm";
			this.ShowInTaskbar = false;
			this.Text = "2Dimensional Set Generator";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() {
			Application.Run(new MainForm());
		}

		private void m_panel_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
			int		width	=	this.Width;
			int		height	=	this.Height;
			System.Drawing.Graphics graphics	=	e.Graphics;

			foreach ( Point pt in m_PointArrayList ){
				graphics.FillEllipse( SystemBrushes.ControlDarkDark, pt.X, pt.Y, 3, 3 );
			}
		}

		private void m_panel_MouseClick( object sender, System.Windows.Forms.MouseEventArgs me ){
			int x	=	me.X;
			int	y	=	me.Y;
			Point	pt	=	new Point( x, y );
			m_PointArrayList.Add( pt );
			Rectangle rc	=	new Rectangle( x, y, 3, 3 );
			m_panel.Invalidate( rc, false );
		}

		private void MenuSaveItem_Click(object sender, System.EventArgs e) {
			if ( saveFileDialog1.ShowDialog() != DialogResult.OK )
				return;
			Stream 	file	=	saveFileDialog1.OpenFile();
			StreamWriter	writer	=	new StreamWriter( file );
			foreach ( Point pt in m_PointArrayList ){
				writer.Write( pt.X + "," + pt.Y + "\t" ); 
			}
			writer.Close();
			file.Close();
		}

		private void MenuOpenItem_Click(object sender, System.EventArgs e) {
			if ( openFileDialog1.ShowDialog() != DialogResult.OK )
				return;

			m_panel.Invalidate();
			m_PointArrayList.Clear();

			Stream		file	=	openFileDialog1.OpenFile();
			StreamReader	reader	=	new StreamReader( file );
			string			line	=	reader.ReadLine();
			string[]		tokens	=	line.Split( '\t' );
			foreach ( string strPt in tokens ){
				string[]	strPosition		=	strPt.Split( ',' );
				if ( strPosition.GetLength(0) < 2 || strPosition[0] == null || strPosition[1] == null )
					break;
				int			x	=	Convert.ToInt32( strPosition[0] );
				int			y	=	Convert.ToInt32( strPosition[1] );
				Point	pt	=	new Point( x, y );
				m_PointArrayList.Add( pt );
				Rectangle rc	=	new Rectangle( x, y, 3, 3 );
				m_panel.Invalidate( rc, false );
			}
		}

		private void MenuExitItem_Click(object sender, System.EventArgs e) {
			this.Close();
		}
	}
}

⌨️ 快捷键说明

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