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