using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace WebApplication{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void OpenFile() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { //MyRTBox //MyRTBox.LoadFile (openFileDialog1.FileName ,RichTextBoxStreamType.PlainText ); //MyStatus.Text = "打开文件"; using (StreamReader ts = new StreamReader(openFileDialog.OpenFile())) { //MyRTBox.Text = ts.ReadToEnd(); MyRTBox.LoadFile(openFileDialog.FileName, RichTextBoxStreamType.PlainText); } } //MyRTBox.LoadFile(openFileDialog.FileName, RichTextBoxStreamType.PlainText); ; } private void menuItem2_Click(object sender, EventArgs e) { OpenFile(); } private void New() { if (MyRTBox.Text != "") { if (MessageBox.Show("是否保存当前文件?", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK) { SaveFile(); } MyRTBox.Clear(); } } private void menuItem3_Click(object sender, EventArgs e) { //this.menuItem4.Click+=new EventHandler(menuItem4_Click); New(); } private void menuItem4_Click(object sender, EventArgs e) { SaveFile(); } private void SaveFile() { SaveFileDialog saveFileDialog = new SaveFileDialog(); //saveFileDialog.DefaultExt = "*.txt"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { //saveFileDialog. if (saveFileDialog.FileName.Length > 0) { MyRTBox.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText); } } } private void menuItem5_Click(object sender, EventArgs e) { SaveFile(); } private void menuItem8_Click(object sender, EventArgs e) { if (MyRTBox.Text != "") { if (MessageBox.Show("是否保存当前文件?", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK) { SaveFile(); } } //(MessageBoxButtons.OKCancel) == DialogResult.OK); Application.Exit(); } private void menuItem6_Click(object sender, EventArgs e) { /* Graphics MyGraphics = e.Graphics; Font MyPrintFont = MyRTBox.Font; //每一页的行数 float iLinePerPage = e.MarginBounds.Height / MyPrintFont.GetHeight(MyGraphics); int iLineNumber = 0; //打印时的行计数器 float fYPosition = 0;//打印时的纵坐标 float fMarginLeft = e.MarginBounds.Left; float fMarginTop = e.MarginBounds.Top; string strLine = ""; //每一行要打印的文本 while ((iLineNumber < iLinePerPage) && ((strLine = MySReader.ReadLine()) != null)) { fYPosition = fMarginTop + iLineNumber * MyPrintFont.GetHeight(MyGraphics); MyGraphics.DrawString(strLine, MyPrintFont, new SolidBrush(Color.Black), fMarginLeft, fYPosition, new StringFormat()); iLineNumber++; } if (strLine != null) { e.HasMorePages = true;//没有打印完,出发下一次PrintPage事件 } else { e.HasMorePages = false; } */ } private void PrintDlog() { StringReader MySReader = new StringReader(MyRTBox.Text); PrintDialog printDialog = new PrintDialog(); //创建打印对话框 // PrintDialog printDialog = new PrintDialog(); System.Drawing.Printing.PrintDocument MyPrintDC = new System.Drawing.Printing.PrintDocument(); printDialog.Document = MyPrintDC; MySReader = new StringReader(MyRTBox.Text); if (printDialog.ShowDialog() == DialogResult.OK) { try { MyPrintDC.Print(); } catch { //停止打印 MyPrintDC.PrintController.OnEndPrint(MyPrintDC, new System.Drawing.Printing.PrintEventArgs()); } } } private void menuItem7_Click(object sender, EventArgs e) { PrintDlog(); } private void menuItem21_Click(object sender, EventArgs e) { FontDialog fontDialog = new FontDialog(); fontDialog.Font = MyRTBox.Font; if (fontDialog.ShowDialog() == DialogResult.OK) { MyRTBox.SelectionBackColor = fontDialog.Color; MyRTBox.Font = fontDialog.Font; } } private void menuItem22_Click(object sender, EventArgs e) { menuItem22.Checked = true; MyRTBox.WordWrap = true; } private void menuItem15_Click(object sender, EventArgs e) { //MyRTBox.Find(MyRTBox.SelectedText); //查找 // // string strTarget = } private void menuItem10_Click(object sender, EventArgs e) { //撤消 // this.menuItem10.Enabled = false; if (MyRTBox.CanUndo == true) { MyRTBox.Undo(); MyRTBox.Redo(); } } private void menuItem12_Click(object sender, EventArgs e) { //StreamReader read = new StreamReader(MyRTBox.SelectedText); //复制 if (MyRTBox.SelectionLength.ToString() != "") { menuItem12.Enabled = true; MyRTBox.Copy(); } } private void menuItem18_Click(object sender, EventArgs e) { //StreamReader streadReader = new StreamReader(MyRTBox.Text); // MyRTBox.SelectedText; //选择选定文本 //全选择 MyRTBox.SelectAll(); } private void menuItem13_Click(object sender, EventArgs e) { //粘贴 MyRTBox.Paste(); } private void menuItem11_Click(object sender, EventArgs e) { //剪切 if (MyRTBox.SelectedText!= "") { menuItem12.Enabled = true; MyRTBox.Cut(); } } private void menuItem14_Click(object sender, EventArgs e) { //删除 if (MyRTBox.SelectionLength > 0) { int i = MyRTBox.SelectedText.Length; for (int j = 0; j <= i; j++) { } } } private void menuItem19_Click(object sender, EventArgs e) { //时间与日期 //MyRTBox.Text.Insert(0, DateTime.Now.ToString()); MessageBox.Show( DateTime.Now.ToString(),"当前时间是:"); } private void menuItem25_Click(object sender, EventArgs e) { //关于记事本 } private void menuItem16_Click(object sender, EventArgs e) { //替换 } private void toolStripSplitButton2_ButtonClick(object sender, EventArgs e) { //显示比例 } private void toolStripMenuItem1_Click(object sender, EventArgs e) { //25%显示比便 MyRTBox.ZoomFactor = 0.25f; } private void toolStripMenuItem3_Click(object sender, EventArgs e) { //50%显示比例 MyRTBox.ZoomFactor = 0.5f; } private void toolStripMenuItem4_Click(object sender, EventArgs e) { //75%显示比例 MyRTBox.ZoomFactor = 0.75f; } private void toolStripMenuItem5_Click(object sender, EventArgs e) { //100%显示比例 MyRTBox.ZoomFactor = 1f; } private void toolStripMenuItem6_Click(object sender, EventArgs e) { //110%显示比例 MyRTBox.ZoomFactor = 1.1f; } private void toolStripMenuItem7_Click(object sender, EventArgs e) { //120%显示比例 MyRTBox.ZoomFactor = 1.2f; } private void toolStripMenuItem8_Click(object sender, EventArgs e) { //150%显示比例 MyRTBox.ZoomFactor = 1.5f; } private void toolStripButton1_Click(object sender, EventArgs e) { //插入图片 } private void menuItem17_Click(object sender, EventArgs e) { //转到 } private void toolStripTextBox1_Click_1(object sender, EventArgs e) { } private int searchStartPosition = 0; private string strOriginalSearch = ""; int indexOfText = 0; private void toolStripButton2_Click(object sender, EventArgs e) { ///查找 string strTarget = toolStripTextBox1.Text; if (strTarget != strOriginalSearch) { searchStartPosition = 0; } if (strTarget.Length >0) { indexOfText= MyRTBox.Find(strTarget, searchStartPosition, RichTextBoxFinds.None); } if(indexOfText>=0) { searchStartPosition = indexOfText + MyRTBox.SelectionLength; } strOriginalSearch = strTarget; } private void MyRTBox_SelectionChanged(object sender, EventArgs e) //在当前选择项改变是发生 { if (MyRTBox.SelectedText!="") { menuItem12.Enabled = true; menuItem11.Enabled = true; menuItem14.Enabled = true; } else { menuItem12.Enabled = false; menuItem11.Enabled = false; } } private void 新建NToolStripButton_Click(object sender, EventArgs e) { //新建 New(); } private void 打开OToolStripButton_Click(object sender, EventArgs e) { //打开 OpenFile(); } private void 保存SToolStripButton_Click(object sender, EventArgs e) { //保存 SaveFile(); } private void 打印PToolStripButton_Click(object sender, EventArgs e) { //打印 PrintDlog(); } private void 复制CToolStripButton_Click(object sender, EventArgs e) { //复制 if (MyRTBox.SelectionLength.ToString() != "") { menuItem12.Enabled = true; MyRTBox.Copy(); } } private void 粘贴PToolStripButton_Click(object sender, EventArgs e) { //粘贴 MyRTBox.Paste(); } private void 剪切UToolStripButton_Click(object sender, EventArgs e) { //剪切 if (MyRTBox.SelectedText != "") { menuItem12.Enabled = true; MyRTBox.Cut(); } } private void MyRTBox_TextChanged(object sender, EventArgs e) { //richTextBox1.SelectionBullet = true; MyRTBox.SelectionBullet = true; } private void Form1_Load(object sender, EventArgs e) { ContextMenu MyMenu = new ContextMenu(); MyMenu.MenuItems.Add(this.menuItem18.CloneMenu()); MyMenu.MenuItems.Add(this.menuItem11.CloneMenu()); MyMenu.MenuItems.Add(this.menuItem13.CloneMenu()); MyMenu.MenuItems.Add(this.menuItem12.CloneMenu()); MyMenu.MenuItems.Add(this.menuItem8.CloneMenu()); this.MyRTBox.ContextMenu = MyMenu; } }}