C# 记事本 源代码


C# 记事本 源代码

记事本

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace CSNotePad
{
    public partial class Form1 : Form
    {
        private string fn = "无标题";
        private StringReader myReader;
        public Form1()
        {
            InitializeComponent();
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result;
            StreamReader sr;
            StreamWriter sw;
            try
            {
                if (textBox1.Modified)
                {
                    result = MessageBox.Show("文件 " + fn + " 的文字已经改变。\r\n\r\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
                    if (result == DialogResult.Cancel)
                        return;
                    if (result == DialogResult.Yes)
                    {
                        saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt; *.cs";
                        saveFileDialog1.FilterIndex = 2;
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            fn = saveFileDialog1.FileName;
                            sw = new StreamWriter(fn, false, System.Text.Encoding.Default);
                            sw.Write(textBox1.Text);
                            this.Text = Path.GetFileName(fn) + " - 记事本";
                            textBox1.Modified = false;
                            sw.Flush();
                            sw.Close();
                        }
                        else
                            return;
                    }
                }

                openFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt; *.cs";
                openFileDialog1.FilterIndex = 2;
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    fn = openFileDialog1.FileName;
                    sr = new StreamReader(fn, System.Text.Encoding.Default);
                    textBox1.Text = sr.ReadToEnd();
                    textBox1.Modified = false;
                    sr.Close();
                    //this.Text = Path.GetFileName(fn) + " - 记事本";
                    this.Text = openFileDialog1.SafeFileName + " - 记事本";
                    saveFileDialog1.FileName = openFileDialog1.FileName;
                }
            }
            catch { }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter sw;
            try
            {
                if (textBox1.Modified)
                {
                    if (fn == "无标题")
                    {
                        saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt; *.cs";
                        saveFileDialog1.FilterIndex = 2;
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                            fn = saveFileDialog1.FileName;
                    }
                    sw = new StreamWriter(fn, false, System.Text.Encoding.Default);
                    sw.Write(textBox1.Text);
                    textBox1.Modified = false;
                    sw.Flush();
                    sw.Close();
                    this.Text = Path.GetFileName(fn) + " - 记事本";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件无法打开或读取。请确认文件名称是否正确,以及您是否有读取权限。\n异常:" + ex.Message);
            }
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != string.Empty)
                {
                    saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt; *.cs";
                    saveFileDialog1.FilterIndex = 2;
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        fn = saveFileDialog1.FileName;
                        StreamWriter sw = new StreamWriter(fn, false, System.Text.Encoding.Default);
                        sw.Write(textBox1.Text);
                        textBox1.Modified = false;
                        sw.Flush();
                        sw.Close();
                        this.Text = Path.GetFileName(fn) + " - 记事本";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件无法打开或读取。请确认文件名称是否正确,以及您是否有读取权限。\n异常:" + ex.Message);
            }
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox1.SelectedText != string.Empty)
                textBox1.Cut();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox1.SelectionLength > 0)
                textBox1.Copy();
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
            {
                if (textBox1.SelectionLength > 0)
                {
                    DialogResult result;
                    result = MessageBox.Show("你想覆盖掉选择的文本吗?", "覆盖确认", MessageBoxButtons.YesNo);
                    if (result == DialogResult.No)
                    {
                        textBox1.SelectionStart = textBox1.SelectionStart + textBox1.SelectionLength;
                        textBox1.SelectionLength = 0;
                    }
                }
                textBox1.Paste();
            }
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty)
                textBox1.SelectAll();
        }

        private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox1.CanUndo)
            {
                textBox1.Undo();
                textBox1.ClearUndo();
            }
        }

        private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectedText = "";
        }

        private void 编辑EToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
        {
            if (textBox1.Modified == false)
                撤消UToolStripMenuItem.Enabled = false;
            else
                撤消UToolStripMenuItem.Enabled = true;

            if (textBox1.SelectedText == string.Empty)
            {
                剪切TToolStripMenuItem.Enabled = false;
                复制CToolStripMenuItem.Enabled = false;
                删除LToolStripMenuItem.Enabled = false;
            }
            else
            {
                剪切TToolStripMenuItem.Enabled = true;
                复制CToolStripMenuItem.Enabled = true;
                删除LToolStripMenuItem.Enabled = true;
            }

            //if (textBox1.Text == string.Empty)
            //{
            //    查找ToolStripMenuItem.Enabled = false;
            //    替换ToolStripMenuItem.Enabled = false;
            //}
            //else
            //{
            //    查找ToolStripMenuItem.Enabled = true;
            //    替换ToolStripMenuItem.Enabled = true;
            //}
            
            if (Clipboard.GetText().ToString() == string.Empty)
                粘贴PToolStripMenuItem.Enabled = false;
            else
                粘贴PToolStripMenuItem.Enabled = true;
        }

        private void 日期时间DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectedText = DateTime.Now.ToString();
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter sw;
            try
            {
                if (textBox1.Modified)
                {
                    DialogResult result = MessageBox.Show("文本发生了改变, 要保存吗?", "注意", MessageBoxButtons.YesNoCancel);
                    if (result == DialogResult.Yes)
                    {
                        if (fn == "无标题")
                        {
                            saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt; *.cs";
                            saveFileDialog1.FilterIndex = 2;
                            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                                fn = saveFileDialog1.FileName;
                        }
                        sw = new StreamWriter(fn, false, System.Text.Encoding.Default);
                        sw.Write(textBox1.Text);
                        sw.Flush();
                        sw.Close();
                        textBox1.Clear();
                        textBox1.Modified = false;
                        this.Text = "无标题 - 记事本";
                        saveFileDialog1.FileName = fn = "无标题";
                    }
                    else if (result == DialogResult.No)
                    {
                        textBox1.Clear();
                        textBox1.Modified = false;
                        this.Text = "无标题 - 记事本";
                        saveFileDialog1.FileName = fn = "无标题";
                    }
                }
                else
                {
                    textBox1.Clear();
                    textBox1.Modified = false;
                    this.Text = "无标题 - 记事本";
                    saveFileDialog1.FileName = fn = "无标题";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件无法打开或读取。请确认文件名称是否正确,以及您是否有读取权限。\n异常:" + ex.Message);
            }
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            自定义CToolStripMenuItem.Checked = ! 自定义CToolStripMenuItem.Checked;
            if (自定义CToolStripMenuItem.Checked)
            {
                textBox1.WordWrap = true;
                textBox1.ScrollBars = ScrollBars.Vertical;
            }
            else
            {
                textBox1.WordWrap = false;
                textBox1.ScrollBars = ScrollBars.Both;
            }
        }

        private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
                textBox1.Font = fontDialog1.Font;
            textBox1.SelectionLength = 0;
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Xiesir 版权所有© 2008-2015", "关于\"记事本\"", MessageBoxButtons.OK);
        }

        private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                pageSetupDialog1.Document = printDocument1;
                //pageSetupDialog1.Document.DefaultPageSettings.Color = false;
                pageSetupDialog1.ShowDialog();
            }
            catch { }
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                printDialog1.Document = printDocument1;
                myReader = new StringReader(textBox1.Text);

                if (printDialog1.ShowDialog() == DialogResult.OK)
                {
                    printDocument1.Print();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                printDocument1.PrintController.OnEndPrint(printDocument1, new System.Drawing.Printing.PrintEventArgs());
            }
            finally
            {
                myReader.Close();
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            linesPerPage = e.MarginBounds.Height / textBox1.Font.GetHeight(e.Graphics);

            SolidBrush brush = new SolidBrush(textBox1.ForeColor);

            while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
            {
                yPos = topMargin + (count * textBox1.Font.GetHeight(e.Graphics));
                e.Graphics.DrawString(line, textBox1.Font, brush, leftMargin, yPos, new StringFormat());
                count++;
            }

            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            StreamWriter sw;
            try
            {
                if (textBox1.Modified && textBox1.Text != string.Empty)
                {
                    if (fn == "无标题")
                    {
                        saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt; *.cs";
                        saveFileDialog1.FilterIndex = 2;
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                            fn = saveFileDialog1.FileName;
                    }
                    sw = new StreamWriter(fn, false, System.Text.Encoding.Default);
                    sw.Write(textBox1.Text);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件无法打开或读取。请确认文件名称是否正确,以及您是否有读取权限。\n异常:" + ex.Message);
            }
        }
    }
}

 

源程序下载

执行程序下载


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM