Form1.cs
上传用户:hueiseng
上传日期:2022-07-26
资源大小:156k
文件大小:16k
源码类别:

C#编程

开发平台:

Visual 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. namespace 笔记本
  9. {
  10.     
  11.     public partial class Form1 : Form
  12.     {
  13.         public string check;
  14.         
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             toolStripButton1.Tag = 新建ToolStripMenuItem;
  19.             toolStripButton2.Tag = 打开ToolStripMenuItem;
  20.          
  21.         }
  22.         private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
  23.         {
  24.             if (text != richTextBox1.Text)
  25.             {
  26.                 if (richTextBox1.Text.Length > 0)
  27.                 {
  28.                     DialogResult result = MessageBox.Show("文本内容己改变,想保存文件吗?", "提示信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
  29.                     if (result == DialogResult.Yes)
  30.                     {
  31.                         if (path == null || openFileDialog1.ReadOnlyChecked)
  32.                         {
  33.                             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  34.                             {
  35.                                 richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  36.                                 richTextBox1.Text = "";
  37.                             }
  38.                         }
  39.                         else
  40.                         {
  41.                             richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
  42.                             richTextBox1.Text = "";
  43.                         }
  44.                     }
  45.                     if (result == DialogResult.No)
  46.                         richTextBox1.Text = "";
  47.                 }
  48.             }
  49.         }
  50.         private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
  51.         {
  52.             openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  53.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  54.             {
  55.                 richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  56.             }
  57.         }
  58.         private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
  59.         {
  60.             saveFileDialog1.DefaultExt = "*.rtf";
  61.             saveFileDialog1.Filter = "RTF Files|*.rtf";
  62.             if (path == null || openFileDialog1.ReadOnlyChecked)
  63.             {
  64.                 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  65.                 {
  66.                     richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  67.                  
  68.                 }
  69.             }
  70.             else
  71.             {
  72.                 richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
  73.                 
  74.             }
  75.             
  76.         }
  77.         private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
  78.         {
  79.             saveFileDialog1.ShowDialog();
  80.             // saveFileDialog1.DefaultExt = "*.rtf";
  81.             //saveFileDialog1.Filter = "RTF Files|*.rtf";
  82.             if (richTextBox1.Text.Length == 0)
  83.             {
  84.                 DialogResult result = MessageBox.Show("文本内为空想保存文件吗?", "提示信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
  85.                 if (result == DialogResult.Yes)
  86.                 {
  87.                     if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  88.                     {
  89.                         richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  90.                         richTextBox1.Text = "";
  91.                     }
  92.                 }
  93.             }
  94.             else
  95.             {
  96.                 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  97.                 {
  98.                     richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  99.                     richTextBox1.Text = "";
  100.                 }
  101.             }
  102.         }
  103.         private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
  104.         {
  105.             pageSetupDialog1.Document = printDocument1;
  106.             pageSetupDialog1.ShowDialog();       
  107.         }
  108.         private void 打印ToolStripMenuItem1_Click(object sender, EventArgs e)
  109.         {
  110.             pageSetupDialog1.Document = printDocument1;
  111.             printDocument1.Print();
  112.         }
  113.         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  114.         {
  115.             if (richTextBox1.Text!= null)
  116.             {
  117.                 string now = richTextBox1.Text;
  118.                 if (path != null)
  119.                 {
  120.                     richTextBox1.Clear();
  121.                     richTextBox1.LoadFile(path, RichTextBoxStreamType.PlainText);
  122.                 }
  123.                 if (path == null ||openFileDialog1.ReadOnlyChecked || (path != null && richTextBox1.Text != now))
  124.                 {
  125.                     if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  126.                     {
  127.                         richTextBox1.Clear();
  128.                         richTextBox1.Text = now;
  129.                         richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  130.                         richTextBox1.Text = "";
  131.                     }
  132.                 }
  133.             }
  134.             Close();
  135.         }
  136.         private void 撤ToolStripMenuItem_Click(object sender, EventArgs e)
  137.         {
  138.             richTextBox1.Undo();
  139.         }
  140.         private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
  141.         {
  142.             if (richTextBox1.SelectedText != null)
  143.             {
  144.                 剪切ToolStripMenuItem.Enabled = true;
  145.                 richTextBox1.Cut();
  146.             }
  147.         }
  148.         private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
  149.         {
  150.             if (richTextBox1.SelectedText != null)
  151.             {
  152.                 richTextBox1.Copy();
  153.             }
  154.             
  155.         }
  156.         private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
  157.         {
  158.             if (richTextBox1.SelectedText != null)
  159.             {
  160.                 richTextBox1.Copy();
  161.             }
  162.         }
  163.         private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  164.         {
  165.             richTextBox1.SelectedText = "";
  166.         }
  167.         private void 编辑ToolStripMenuItem_Click_1(object sender, EventArgs e)
  168.         {
  169.             剪切ToolStripMenuItem.Enabled = false;
  170.             if (richTextBox1.SelectedText.Length > 0)
  171.             {
  172.                 剪切ToolStripMenuItem.Enabled = true;
  173.             }
  174.             else
  175.             {
  176.                 剪切ToolStripMenuItem.Enabled = false;
  177.             }
  178.             复制ToolStripMenuItem.Enabled = false;
  179.             if (richTextBox1.SelectedText.Length > 0)
  180.             {
  181.                 复制ToolStripMenuItem.Enabled = true;
  182.             }
  183.             else
  184.             {
  185.                 复制ToolStripMenuItem.Enabled = false;
  186.             }
  187.             删除ToolStripMenuItem.Enabled = false;
  188.             if (richTextBox1.SelectedText.Length > 0)
  189.             {
  190.                 删除ToolStripMenuItem.Enabled = true;
  191.             }
  192.             else
  193.             {
  194.                 删除ToolStripMenuItem.Enabled = false;
  195.             }
  196.             查找ToolStripMenuItem1.Enabled = false;
  197.             if (richTextBox1.Text.Length > 0)
  198.             {
  199.                 查找ToolStripMenuItem1.Enabled = true;
  200.             }
  201.             else
  202.             {
  203.                 查找ToolStripMenuItem1.Enabled = false;
  204.             }
  205.          
  206.          
  207.         }
  208.        
  209.         private void 打印预览ToolStripMenuItem_Click_1(object sender, EventArgs e)
  210.         {
  211.             pageSetupDialog1.Document = printDocument1;
  212.             printPreviewDialog1.ShowDialog();
  213.         }
  214.        
  215.         private void 查找下一个ToolStripMenuItem_Click(object sender, EventArgs e)
  216.         {
  217.             if (richTextBox1.SelectedText != null)
  218.             {
  219.                 int findplace=richTextBox1.SelectionStart+richTextBox1.SelectedText.Length;
  220.                 findplace = richTextBox1.Text.IndexOf(richTextBox1.SelectedText,findplace);
  221.                 if (findplace == -1)
  222.                     MessageBox.Show("没有搜索项");
  223.                 else
  224.                 richTextBox1.Select(findplace, richTextBox1.SelectedText.Length);
  225.                 
  226.                
  227.             }
  228.             else
  229.             {
  230.                 查找ToolStripMenuItem1_Click(sender, e);
  231.             }
  232.         }
  233.         private void 查找ToolStripMenuItem1_Click(object sender, EventArgs e)
  234.         {
  235.             if (richTextBox1.Text.Length > 0)
  236.             {
  237.                 查找ToolStripMenuItem1.Enabled = true;
  238.                 查找 show = new 查找();
  239.                 show.Owner = this;
  240.                 show.ShowDialog();
  241.             }
  242.             else
  243.                 查找ToolStripMenuItem1.Enabled = false;
  244.         }
  245.       
  246.         
  247.         private void timer1_Tick(object sender, EventArgs e)
  248.         {
  249.             toolStripStatusLabel1.Text = DateTime.Now.ToString();
  250.             int line = this.richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
  251.             int col;
  252.             int start=0;
  253.             int cursor = richTextBox1.SelectionStart;
  254.             while (start < cursor)
  255.             {
  256.                 if (line == this.richTextBox1.GetLineFromCharIndex(start))
  257.                 {
  258.                     break;
  259.                 }
  260.                 else
  261.                     start++;
  262.             }
  263.             col = cursor - start;
  264.             line++;
  265.             col++;
  266.             toolStripStatusLabel2.Text = "行" + line.ToString() + "列" + col.ToString();
  267.         }
  268.         private void Form1_Load(object sender, EventArgs e)
  269.         {
  270.             timer1.Start();
  271.             
  272.         }
  273.       
  274.         private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
  275.         {
  276.             if (richTextBox1.Text.Length > 0)
  277.             {
  278.                 替换ToolStripMenuItem.Enabled = true;
  279.                 替换 show = new 替换();
  280.                 show.Owner = this;
  281.                 show.ShowDialog();
  282.             }
  283.         }
  284.         private void 转到ToolStripMenuItem_Click(object sender, EventArgs e)
  285.         {
  286.             转到ToolStripMenuItem.Enabled = true;
  287.             转到 show = new 转到();
  288.             show.Owner = this;
  289.             show.ShowDialog(); 
  290.         }
  291.         private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
  292.         {
  293.             if (richTextBox1.Text != null)
  294.                 richTextBox1.SelectAll();
  295.              全选ToolStripMenuItem.Enabled=false;
  296.         }
  297.         private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
  298.         {
  299.             richTextBox1.Text += toolStripStatusLabel1.Text;
  300.         }
  301.         private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
  302.         {
  303.             if(richTextBox1 .WordWrap==false)
  304.             {
  305.                 richTextBox1.WordWrap = true;
  306.                 自动换行ToolStripMenuItem.Text="√自动换行";
  307.                 }
  308.             else
  309.                 {
  310.                     richTextBox1.WordWrap =false;
  311.                     自动换行ToolStripMenuItem.Text ="自动换行";
  312.                 }
  313.         }
  314.         private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
  315.         {
  316.             fontDialog1.ShowColor = true;
  317.             if (fontDialog1.ShowDialog() != DialogResult.Cancel)
  318.             {
  319.                 richTextBox1.Font = fontDialog1.Font;
  320.                 richTextBox1.ForeColor = fontDialog1.Color;
  321.                
  322.             }
  323.         }
  324.         private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
  325.         {
  326.             if (statusStrip1.Visible == false)
  327.             {
  328.                 statusStrip1.Visible = true;
  329.                 状态栏ToolStripMenuItem.Text = "√状态栏";
  330.             }
  331.             else
  332.             {
  333.                 statusStrip1.Visible = false;
  334.                 状态栏ToolStripMenuItem.Text = "状态栏";
  335.             }
  336.         }
  337.         private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
  338.         {
  339.             主题 show = new 主题();
  340.             show.Show();
  341.         }
  342.        
  343.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  344.         {
  345.         }
  346.         private void 撤消撤消ToolStripMenuItem_Click(object sender, EventArgs e)
  347.         {
  348.             richTextBox1.Undo();
  349.         }
  350.         private void 剪切ToolStripMenuItem1_Click(object sender, EventArgs e)
  351.         {
  352.             剪切ToolStripMenuItem_Click(sender, e);
  353.         }
  354.         private void 复制ToolStripMenuItem1_Click(object sender, EventArgs e)
  355.         {
  356.              复制ToolStripMenuItem_Click(sender,e);
  357.         }
  358.         private void 粘贴ToolStripMenuItem1_Click(object sender, EventArgs e)
  359.         {
  360.             粘贴ToolStripMenuItem_Click( sender,e);
  361.         }
  362.         private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
  363.         {
  364.             删除ToolStripMenuItem_Click(sender, e);
  365.         }
  366.         private void 全选ToolStripMenuItem1_Click(object sender, EventArgs e)
  367.         {
  368.             全选ToolStripMenuItem_Click(sender, e);
  369.         }
  370.        
  371.         private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
  372.         {
  373.             if (MouseButtons == MouseButtons.Right)
  374.             {
  375.                 int x = MousePosition.X;
  376.                 int y = MousePosition.Y;
  377.                 contextMenuStrip1.Show(x, y);
  378.             }
  379.         }
  380.         private void toolStripButton1_Click(object sender, EventArgs e)
  381.         {
  382.             新建ToolStripMenuItem_Click(sender,e);
  383.         }
  384.         private void toolStripButton2_Click(object sender, EventArgs e)
  385.         {
  386.             打开ToolStripMenuItem_Click(sender, e);
  387.         }
  388.         private void toolStripButton3_Click(object sender, EventArgs e)
  389.         {
  390.             保存ToolStripMenuItem_Click(sender, e);
  391.         }
  392.         private void toolStripButton7_Click(object sender, EventArgs e)
  393.         {
  394.             打印ToolStripMenuItem1_Click(sender, e);
  395.         }
  396.         private void toolStripButton8_Click(object sender, EventArgs e)
  397.         {
  398.             查找ToolStripMenuItem1_Click(sender, e);
  399.         }
  400.         private void toolStripButton4_Click(object sender, EventArgs e)
  401.         {
  402.             复制ToolStripMenuItem_Click(sender, e);
  403.         }
  404.         private void toolStripButton5_Click(object sender, EventArgs e)
  405.         {
  406.             剪切ToolStripMenuItem_Click(sender, e);
  407.         }
  408.         private void toolStripButton6_Click(object sender, EventArgs e)
  409.         {
  410.             粘贴ToolStripMenuItem_Click(sender, e);
  411.         }
  412.         private void 关于记事本ToolStripMenuItem1_Click(object sender, EventArgs e)
  413.         {
  414.             HELP show = new HELP();
  415.             show.Show();
  416.         }
  417.        
  418.         
  419.        private void 图片ToolStripMenuItem_Click(object sender, EventArgs e)
  420.         {
  421.             openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  422.             if (openFileDialog2.ShowDialog ()== DialogResult.OK)
  423.             {
  424.                 pictureBox1.Load(openFileDialog2.FileName);
  425.                 Point a = new Point();
  426.                 a.X = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart).X;
  427.                 a.Y= richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart).Y+49;
  428.                 pictureBox1.Location = a;
  429.                     
  430.            
  431.                 pictureBox1.Visible = true;
  432.                 richTextBox1.Focus();
  433.                 richTextBox1.SelectionStart = richTextBox1.GetCharFromPosition(a);
  434.             }
  435.         }
  436.     
  437.         
  438.       
  439.         
  440.        
  441.     }
  442. }