文件讀取實例

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace StreamDemo1
{
 /// <summary>
 /// Form1 的摘要描述。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private ArrayList pointList;
  /// <summary>
  /// 設計工具所需的變數。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows Form 設計工具支援的必要項
   //
   InitializeComponent();
   pointList = new ArrayList();

   //
   // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
   //
  }

  /// <summary>
  /// 清除任何使用中的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form 設計工具產生的程式碼
  /// <summary>
  /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
  /// 這個方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.label1 = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(8, 248);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(64, 16);
   this.label1.TabIndex = 0;
   this.label1.Text = "線條寬度";
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(64, 240);
   this.textBox1.Name = "textBox1";
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "1";
   this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(168, 240);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(75, 24);
   this.button1.TabIndex = 2;
   this.button1.Text = "線條顏色";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(264, 240);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(75, 24);
   this.button2.TabIndex = 3;
   this.button2.Text = "保存";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(352, 240);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(75, 24);
   this.button3.TabIndex = 4;
   this.button3.Text = "打開";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
   this.ClientSize = new System.Drawing.Size(472, 273);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.label1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程式的主進入點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  protected void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  {
    if(e.KeyChar == Int32.Parse(Keys.Enter.ToString()))
    {
       this.Invalidate(this.ClientRectangle);
    }
  }

  protected void button1_Click(object sender, System.EventArgs e)
  {
   ColorDialog cdlg = new ColorDialog();
   if(cdlg.ShowDialog() == DialogResult.OK)
   {
      this.button1.ForeColor = cdlg.Color;
    this.Invalidate(this.ClientRectangle);
   }
  }

  protected void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   Point pt = new Point(e.X,e.Y);
   pointList.Add(pt);
   this.Invalidate(this.ClientRectangle);
  }

  protected void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   Graphics g = e.Graphics;
   Pen pen = new Pen(button1.ForeColor,Single.Parse(this.textBox1.Text));
   for(int i = 0; i < pointList.Count - 1; i++)
   {
      g.DrawLine(pen,(Point)pointList[i],(Point)pointList[i+1]);
   }
  }

  protected void button2_Click(object sender, System.EventArgs e)
  {
     SaveFileDialog sfDlg = new SaveFileDialog();
   sfDlg.Filter = "Point Connect File(*.ptc)|*ptc|All Files(*.*)|*.*";
   sfDlg.FilterIndex = 1;
   FileStream fs = null;
   BinaryWriter bw = null;
   if(sfDlg.ShowDialog() == DialogResult.OK)
   {
    try
    {
      fs = new FileStream(sfDlg.FileName,FileMode.Create,FileAccess.Write);

     bw = new BinaryWriter(fs);
     bw.Write(Single.Parse(this.textBox1.Text));
     bw.Write(button1.ForeColor.ToArgb());
     bw.Write(pointList.Count);
     for(int i = 0; i < pointList.Count; i++)
     {
       bw.Write(((Point)pointList[i]).X);
       bw.Write(((Point)pointList[i]).Y);
     }
    }
    catch(Exception ex)
    {
      MessageBox.Show(ex.Message,"Information",MessageBoxButtons.OK,MessageBoxIcon.Error );
    }
    finally
    {
      if(fs != null)
       fs.Close();
    }
   }
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
    OpenFileDialog ofDlg = new OpenFileDialog();
   ofDlg.Filter = "Point connector File(*.ptc)|*.ptc|All Files(*.*)|*.*";
   ofDlg.FilterIndex = 1;
   FileStream fs = null;
   BinaryReader br = null;

   if(ofDlg.ShowDialog() == DialogResult.OK)
   {
    try
    {
      fs = new FileStream(ofDlg.FileName,FileMode.Open,FileAccess.Read);
     br = new BinaryReader(fs);
     this.textBox1.Text = br.ReadSingle().ToString();
     button1.ForeColor = Color.FromArgb(br.ReadInt32());
     pointList.Clear();
     int count = br.ReadInt32();
     for(int i= 0 ; i < count; i++)
     {
        pointList.Add(new Point(br.ReadInt32(),br.ReadInt32()));

     }
     this.Invalidate(this.ClientRectangle);
    }
    catch(Exception ex)
    {
                    MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);        
    }
    finally
    {
      br.Close();
     fs.Close();
    }
   }
  }
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值