c# 記事本

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace WindowsApplication2
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private string text = string.Empty;
  18.         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  19.         {
  20.             e.Graphics.DrawString(richTextBox1.Text, new Font("Arial", 10), Brushes.Black, 20, 20);    //將要打印的文本輸出到printDocument
  21.         }
  22.         private void Form1_Load(object sender, EventArgs e)
  23.         {
  24.             toolStripStatusLabel4.Text = "Copyright © 2008 xiaomeng Inc. All Rights Reserved";
  25.             richTextBox1.EnableAutoDragDrop = true//設置允許拖放對象 
  26.             richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop); //註冊拖放事件 
  27.             //if (e.KeyCode == Keys.F1) { MessageBox.Show("該軟件由孟德軍編寫,版權所有,侵權必究!", "關於"); }
  28.            
  29.         }
  30.         public void loadinfo(String path) {
  31.             StreamReader fw = new StreamReader(path, System.Text.Encoding.GetEncoding("gb2312"), true); 
  32.            // StreamReader fw = new StreamReader(path,System.Text.Encoding.Default,true);
  33.             richTextBox1.Text = fw.ReadToEnd();
  34.             fw.Close();
  35.             //創建一個輸入流。並將輸出流存入到richtextbox中。
  36.         }
  37.         public void saveinfo(String path)
  38.         {
  39.             StreamWriter fi = new StreamWriter(path);
  40.             fi.WriteLine(richTextBox1.Text);
  41.             fi.Close();
  42.             //創建一個輸出流。並將richtextbox保存在文件中。輸出到文件。
  43.         }
  44.         private void 打開ToolStripMenuItem_Click(object sender, EventArgs e)
  45.         {
  46.             OpenFileDialog dlg = new OpenFileDialog();
  47.             
  48.             dlg.InitialDirectory = Application.StartupPath;//啓動應用程序的路徑。
  49.             dlg.Filter = "文本文件|*.txt";//文件類型中出現的內容。
  50.             if (dlg.ShowDialog() == DialogResult.OK) {
  51.                 loadinfo(dlg.FileName);//挑用方法。
  52.                 
  53.         //--------------------------------------------------------------------------------
  54.                /* string strName = dlg.FileName;
  55.                 string str1;
  56.                 str1 = Path.GetFileNameWithoutExtension(dlg.FileName) + "/n";
  57.                 richTextBox1.Name = str1;*/
  58.                 //
  59.                 
  60.                 
  61.                
  62.             }
  63.         }
  64.         private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
  65.         {
  66.             SaveFileDialog dlm = new SaveFileDialog();
  67.             dlm.InitialDirectory = Application.StartupPath;
  68.             dlm.Filter = "文本文件|*.txt";
  69.             if (dlm.ShowDialog() == DialogResult.OK) { saveinfo(dlm.FileName); }
  70.         }
  71.         private void timer1_Tick(object sender, EventArgs e)
  72.         {
  73.             toolStripStatusLabel1.Text ="本地座標::"+Cursor.Position.X.ToString() +" "+ Cursor.Position.Y.ToString();
  74.             
  75.         }
  76.         private void 關閉ToolStripMenuItem_Click(object sender, EventArgs e)
  77.         {
  78.             if (richTextBox1.Text=="")
  79.             {
  80.                 this.Close();
  81.             }
  82.             else
  83.             { //MessageBox.Show("文件尚未保存,請確定要退出嗎?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
  84.                 if (MessageBox.Show("文件尚未保存,請確定要退出嗎?""提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
  85.                 {
  86.                     this.Close();
  87.                 }
  88.             }
  89.         }
  90.         private void copyToolStripMenuItem_Click(object sender, EventArgs e)
  91.         {
  92.             Clipboard.SetDataObject(richTextBox1.Text);
  93.         }
  94.         private void clipToolStripMenuItem_Click(object sender, EventArgs e)
  95.         {
  96.             IDataObject idata = Clipboard.GetDataObject();
  97.             if (idata.GetDataPresent(DataFormats.Text))
  98.             {
  99.                 this.richTextBox1.Text = this.richTextBox1.Text + (String)idata.GetData(DataFormats.Text);
  100.             }
  101.             else { MessageBox.Show("您沒有複製任何數據!""aboout", MessageBoxButtons.OK, MessageBoxIcon.Error); }
  102.         }
  103.         private void dateToolStripMenuItem_Click(object sender, EventArgs e)
  104.         {
  105.             richTextBox1.Text+=richTextBox1.Text.Insert(richTextBox1.TextLength,System.DateTime.Now.ToString());
  106.         }
  107.         private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  108.         {
  109.             new Form2().Show();
  110.             
  111.         }
  112.         private void 字體FToolStripMenuItem_Click(object sender, EventArgs e)
  113.         {
  114.             FontDialog font = new FontDialog();
  115.             if (font.ShowDialog() == DialogResult.OK)
  116.                 richTextBox1.Font = font.Font;
  117.            
  118.         }
  119.         private void 顏色CToolStripMenuItem_Click(object sender, EventArgs e)
  120.         {
  121.             ColorDialog color = new ColorDialog();
  122.             if (color.ShowDialog() == DialogResult.OK)
  123.             {
  124.                 richTextBox1.ForeColor = color.Color;
  125.             }
  126.             
  127.         }
  128.         private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  129.         {
  130.             Form1 newform = new Form1();
  131.             newform.SetDisplayRectLocation(0,0);
  132.             newform.Show();
  133.             newform.Text = "未命名文件";
  134.             
  135.         }
  136.         private void 返回RToolStripMenuItem_Click(object sender, EventArgs e)
  137.         {
  138.             richTextBox1.Copy();
  139.         }
  140.         private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  141.         {
  142.         }
  143.         
  144.         private void toolStripStatusLabel2_Click_1(object sender, EventArgs e)
  145.         {
  146.         
  147.         }
  148.         private void timer2_Tick(object sender, EventArgs e)
  149.         {
  150.             toolStripStatusLabel2.Text = "系統時間:" + System.DateTime.Now.ToString();
  151.            
  152.         }
  153.         private void 預覽VToolStripMenuItem_Click(object sender, EventArgs e)
  154.         {
  155.             //設於打印預覽窗口所要關聯的printDocument
  156.             printPreviewDialog1.Document = printDocument1;
  157.             try
  158.             {
  159.                 printPreviewDialog1.ShowDialog();   //顯示打印預覽窗口
  160.             }
  161.             catch (Exception pr)
  162.             {
  163.                String message = pr.Message;
  164.                 MessageBox.Show("對不起,您可能還沒有添加打印機!" + message, "打印錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  165.                 if (printDialog1.ShowDialog() == DialogResult.OK) { this.Close(); }
  166.             }
  167.         }
  168.         private void printPreviewDialog1_Load(object sender, EventArgs e)
  169.         {
  170.         }
  171.         private void editToolStripMenuItem_Click(object sender, EventArgs e)
  172.         {
  173.         }
  174.         private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  175.         {
  176.             if (printDialog1.ShowDialog() == DialogResult.Cancel)
  177.             {
  178.                 return;     //返回
  179.             }
  180.             else    //否則
  181.             {
  182.                 printDocument1.Print();    //開始打印文檔
  183.             }
  184.         }
  185.         private void toolStripStatusLabel4_Click(object sender, EventArgs e)
  186.         {
  187.             toolStripStatusLabel4.Text = "Copyright © 2008 xiaomeng Inc. All Rights Reserved";
  188.             System.Diagnostics.Process.Start("mailto:[email protected]");
  189.         }
  190.         private void toolStripStatusLabel3_Click(object sender, EventArgs e)
  191.         {
  192.             
  193.         }
  194.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  195.         {
  196.         }
  197.         private void richTextBox1_DragDrop(object sender, DragEventArgs e)
  198.         {
  199.             //獲取拖放到控件上的文件路徑,因爲支持拖放多個,輸出是一個數組! 
  200.            // richTextBox1.Text = ((string[])e.Data.GetData("FileName"))[0].ToString();
  201.             
  202.             MessageBox.Show(((string[])e.Data.GetData("FileName"))[0].ToString());
  203.            // e.Effect = DragDropEffects.None;//設置拖放後的顯示效果,這裏設置無 */
  204.             e.Effect = DragDropEffects.Copy;
  205.         }
  206.         private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
  207.         {
  208.             richTextBox1.Clear();
  209.            
  210.         }
  211.         private void 剪切XToolStripMenuItem_Click(object sender, EventArgs e)
  212.         {
  213.             richTextBox1.Cut();
  214.             
  215.         }
  216.         private void toolStripStatusLabel3_Click_1(object sender, EventArgs e)
  217.         {
  218.            
  219.             
  220.         }
  221.         private void 目錄IToolStripMenuItem_Click(object sender, EventArgs e)
  222.         {
  223.             folderBrowserDialog1.SelectedPath="C://";
  224.             folderBrowserDialog1.ShowNewFolderButton = true;
  225.             folderBrowserDialog1.Description = "請選擇目錄:";
  226.             folderBrowserDialog1.ShowDialog();
  227.             Text = folderBrowserDialog1.SelectedPath;
  228.         }
  229.        
  230.   
  231.         //僅僅顯示文件名,實現打開功能就比較簡單了
  232.        
  233. #warning 這是一個測試各種對話框的軟件。
  234. //#error 使用#error指令產生的信息
  235. #line 100 "Form1.cs"
  236. //author:xiaomeng
  237.     }
  238. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章