using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace 记事本 { public partial class form1 : Form { public form1() { InitializeComponent(); } private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) { this.Close();//关闭窗口! } private void textBox1_TextChanged(object sender, EventArgs e) { string shu = textBox1.TextLength.ToString();//获取文件里的数据长度 shuzi1.Text = shu;//输出数 } private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo();//撤销文本框中上一个编辑操作。 } private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut();//将文本框中选中的文本移动到剪贴板中 } private void 复制CToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Copy();//将文本框中选中的文本复制到剪贴板中 } private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Paste();//将剪贴板中的内容替换文本框中的选定内容 } private void 全选AToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.SelectAll();//选中文本框中所有的内容 } private void 清空ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Clear();//清空文本狂中所有的内容 } private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult dr = colorDialog1.ShowDialog();//shoudialog 显示对话框 if (dr == DialogResult.OK) { textBox1.ForeColor = colorDialog1.Color; } } private void 字体设计ToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.ShowColor = true; DialogResult cr = fontDialog1.ShowDialog(); if (cr == DialogResult.OK) { textBox1.ForeColor = fontDialog1.Color; textBox1.Font = fontDialog1.Font; } } private void 选择文件夹ToolStripMenuItem_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); textBox1.Text = folderBrowserDialog1.SelectedPath;//在文本框中显示文件的绝对路径 } private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "文本文件|*.txt";//确定打开的文件类型。要是还要别的类型继续在引号里加|文本文件|*.*| 这是所有文件类型 *代表所有 DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { label1.Text = openFileDialog1.FileName;//用户选择的要打开文件的绝对路径 StreamReader sr = new StreamReader(openFileDialog1.FileName); textBox1.Text= sr.ReadToEnd(); sr.Close();//通道只有一个,必须关闭。@!! } } private void form1_Load(object sender, EventArgs e) { } string path = "";//定义全局保存路径为空 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (path == "") { saveFileDialog1.FileName = "新建文本文件.txt";//设置保存类型和保存名称 saveFileDialog1.ShowDialog(); path = saveFileDialog1.FileName;//用户选择的要保存的路径 } StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); sw.Write(textBox1.Text); sw.Close(); } private void 打印设计ToolStripMenuItem_Click(object sender, EventArgs e) { pageSetupDialog1.Document = printDocument1;//打印设置所要操作的对象就是PrintDocument1! pageSetupDialog1.ShowDialog(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//类似于画板 { Font f = new Font("宋体",14);//定义打印字体 Brush b = new SolidBrush(Color.Black);//画刷对象 确定颜色 PointF p = new PointF(10,10);//确定二维画面中的坐表。 e.Graphics.DrawString(textBox1.Text, f, b, p); } private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e) { //printPreviewControl1.Document = printDocument1;//你要展现的对象 printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); } private void printPreviewDialog1_Load(object sender, EventArgs e) { } private void 打印PToolStripMenuItem_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1; printDialog1.ShowDialog(); } } }