分享下最近编写的记事本小程序

开发工具VS2008,语言C#!

具体实现功能请看下面代码:

 

代码
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;
using System.Drawing.Printing;

namespace TestProject
{
    
public partial class MainForm : Form
    {    
        
private string currentFileName;
        
private bool needToSave;
        
private System.Drawing.Printing.PrintDocument printDocument = new PrintDocument();

        
public MainForm()
        {
            InitializeComponent();
        }

        
private void MainForm_Load(object sender, EventArgs e)
        {
            
this.toolStripStatusTotalAmount.Text = "当前文本字数合计:" + richTextForm.Text.Length;
        }

        
//工具栏菜单实现
        private void toolStripStandard_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            
if (e.ClickedItem == toolStripStandardNew)
                menuFileNew_Click(
nullnull);
            
else if (e.ClickedItem == toolStripStandardOpen)
                menuFileOpen_Click(
nullnull);
            
else if (e.ClickedItem == toolStripStandardSave)
                menuFileSave_Click_1(
nullnull);
            
else if (e.ClickedItem == toolStripStandardUndo)
                menuEditorUndo_Click(
nullnull);
            
else if (e.ClickedItem == toolStripStandardCut)
                menuEditCut_Click_1(
nullnull);
            
else if (e.ClickedItem == toolStripStandardCopy)
                menuEditCopy_Click_1(
nullnull);
            
else if (e.ClickedItem == toolStripStandardPaste)
                menuEditPaste_Click_1(
nullnull);
        }

        
//显示/隐藏工具栏
        private void standardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleStandardToolStrip();
        }

        
public void ToggleStandardToolStrip()
        { 
            StandardToolStripVisibility 
= !(StandardToolStripVisibility);
        }

        
public bool StandardToolStripVisibility
        {
            
get 
            {
                
return toolStripStandard.Visible;
            }
            
set
            {
                toolStripStandard.Visible 
= value;

                menuViewToolStripsStandard.Checked 
= value;
            }
        }

        
private void menuViewToolStripsOptions_Click(object sender, EventArgs e)
        {
            ToggleOptionsToolStrip();
        }

        
public void ToggleOptionsToolStrip()
        { 
            OptionsToolStripVisibility 
= !(OptionsToolStripVisibility);
        }

        
public bool OptionsToolStripVisibility
        {
            
get
            {
                
return statusStripTotalAmount.Visible;
            }
            
set
            {
                statusStripTotalAmount.Visible 
= value;

                menuViewToolStripsOptions.Checked 
= value;
            }
        }

        
//字体设置
        private void FormatFont_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog 
= new FontDialog();
            fontDialog.ShowColor 
= true;
            fontDialog.AllowScriptChange 
= true;
            fontDialog.AllowVerticalFonts 
= true;
            fontDialog.ShowEffects 
= true;

            
if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                richTextForm.Font 
= fontDialog.Font;
                richTextForm.ForeColor 
= fontDialog.Color;
            }
        }

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

        
//新建文本
        private void menuFileNew_Click(object sender, EventArgs e)
        {
            
if (needToSave == true)
            {
                DialogResult result 
= MessageBox.Show("文本内容已经改变,需要保存吗?""保存文本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                
switch (result)
                {
                    
case DialogResult.Yes:
                        {
                            menuFileSave_Click_1(sender, e); 
                            richTextForm.Clear();
                            
this.Text = "编辑文本...新建文本";
                            needToSave 
= false;
                            
break;
                        }
                    
case DialogResult.No:
                        {
                            richTextForm.Clear();
                            
this.Text = "编辑文本...新建文本";
                            needToSave 
= false;
                            
break;
                        }
                    
case DialogResult.Cancel:
                        {
                            
break;
                        }
                }
            }
            
else
            {
                richTextForm.Clear();
                
this.Text = "编辑文本...新建文本";
            }
        }

        
//打开文本
        private void menuFileOpen_Click(object sender, EventArgs e)
        {
            
if (needToSave == true)
            {
                DialogResult result 
= MessageBox.Show("文本的内容已经改变,需要保存吗?","保存文本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                
if (result == DialogResult.Cancel)
                {
                    
return;
                }
                
if (result == DialogResult.Yes)
                {
                    menuFileSave_Click_1(sender, e); 
                    needToSave 
= false;
                }
            }

            
string file = GetOpenFile();
            
if (file == null)
            {
                
return;
            }
            
else
            {
                currentFileName 
= file;
                OpenFile();
            }            
        }

        
//定义打开文本
        private string GetOpenFile()
        {
            OpenFileDialog openFile 
= new OpenFileDialog();
            openFile.Title 
= "打开文本文件";
            openFile.CheckFileExists 
= true;
            openFile.CheckPathExists 
= true;
            openFile.AddExtension 
= true;
            openFile.Multiselect 
= false;
            openFile.Filter 
= "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";

            
if (openFile.ShowDialog() == DialogResult.OK)
            {
                
return openFile.FileName;
            }
            
else
            {
                
return null;
            }
        }
        
        
//保存文本
        private void menuFileSave_Click()
        {
            
throw new NotImplementedException();
        }
        
//定义打开
        private void OpenFile()
        {
            
try
            {
                FileInfo fileInfo 
= new FileInfo(currentFileName);
                StreamReader reader 
= fileInfo.OpenText();
                richTextForm.Text 
= reader.ReadToEnd();
                reader.Close();

                
this.Text = "文本编辑..." + fileInfo.Name;
            }
            
catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        
//关于系统
        private void HelpAbout_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm 
= new AboutForm();
            aboutForm.Show();
        }

        
//联系方式
        private void HelpContact_Click(object sender, EventArgs e)
        {
            ContactForm contactForm 
= new ContactForm();
            contactForm.Show();
        }

        
//背景设置
        private void FormatColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog 
= new ColorDialog();
            colorDialog.AllowFullOpen 
= true;
            colorDialog.AnyColor 
= true;
            colorDialog.FullOpen 
= true;
            
if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                richTextForm.BackColor 
= colorDialog.Color;
            }
        }

        
//自动换行
        private void FormatNewLine_Click(object sender, EventArgs e)
        {
            
if (this.FormatNewLine.Checked == false)
            {
                
this.FormatNewLine.Checked = true;
                
this.richTextForm.WordWrap = true;
            }

            
else
            {
                
this.FormatNewLine.Checked = false;
                
this.richTextForm.WordWrap = false;
            }
        }

        
//打印设置
        private void menuFilePrint_Click(object sender, EventArgs e)
        {
            PrintDialog printDialog 
= new PrintDialog();
            printDialog.Document 
= printDocument;

            
if (printDialog.ShowDialog() == DialogResult.OK)
            {
                
try
                {
                    printDocument.Print();
                }
                
catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        
//设置
        private void menuFileSetting_Click(object sender, EventArgs e)
        {
            PageSetupDialog pageSetupDialog 
= new PageSetupDialog();
            pageSetupDialog.Document 
= printDocument;
            pageSetupDialog.ShowDialog();
        }

        
//撤销
        private void menuEditorUndo_Click(object sender, EventArgs e)
        {
            
if (richTextForm.CanUndo == true)
            {
                richTextForm.Undo();
                richTextForm.ClearUndo();
            }
        }

        
//剪切
        private void menuEditCut_Click_1(object sender, EventArgs e)
        {
            
if (richTextForm.SelectedText != "")
            {
                richTextForm.Cut();
            }
        }

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

        
//粘帖
        private void menuEditPaste_Click_1(object sender, EventArgs e)
        {
            
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
            {
                
if (richTextForm.SelectionLength > 0)
                {
                    DialogResult result;
                    result 
= MessageBox.Show("您想要覆盖所选择的文本吗?","确定覆盖", MessageBoxButtons.YesNo);
                    
if (result == DialogResult.No)
                    {
                        richTextForm.SelectionStart 
= richTextForm.SelectionStart + richTextForm.SelectionLength;
                    }
                }
                richTextForm.Paste();
            }
        }

        
//删除
        private void menuEditDelete_Click(object sender, EventArgs e)
        {
            richTextForm.SelectedText.Remove(
0, richTextForm.SelectionLength);
            richTextForm.SelectedText 
= "";
        }

        
//全选
        private void menuEditAll_Click(object sender, EventArgs e)
        {
            richTextForm.SelectAll();
        }

        
private void menuEditDateTime_Click(object sender, EventArgs e)
        {
            DateTime dt 
= DateTime.Now;
            richTextForm.AppendText(dt.ToString());
        }

        
//查找
        private void menuEditSearch_Click(object sender, EventArgs e)
        {
            SearchForm searchForm 
= new SearchForm();
            searchForm.rtb 
= this.richTextForm; //传值(从主窗口传到FindForm)
            searchForm.Owner = this;   //悬浮于当前窗体
            searchForm.Show();
        }

        
//保存
        private void menuFileSave_Click_1(object sender, EventArgs e)
        {
            
if (currentFileName == null)
            {
                menuFileSaveAs_Click(sender, e);
            }
            
else
            {
                SaveFile(richTextForm.Text);
            }
            needToSave 
= false;
        }

        
//保存为
        private void menuFileSaveAs_Click(object sender, EventArgs e)
        {
            
string file = GetSaveFile();
            
if (file == null)
            {
                
return;
            }
            
else
            {
                currentFileName 
= file;
                SaveFile(richTextForm.Text);
                FileInfo fileInfo 
= new FileInfo(currentFileName);
                
this.Text = "文本编辑--" + fileInfo.Name;
                needToSave 
= false;
            }
        }

        
//保存文件
        private void SaveFile(string str)
        {
            
try
            {
                StreamWriter writer 
= new StreamWriter(currentFileName);
                writer.Write(str);
                writer.Close();
            }
            
catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

        
//保存格式
        private string GetSaveFile()
        {
            SaveFileDialog saveFileDialog 
= new SaveFileDialog();
            saveFileDialog.Title 
= "保存文本文件";
            saveFileDialog.OverwritePrompt 
= true;
            saveFileDialog.CreatePrompt 
= true;
            saveFileDialog.Filter 
= "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            
if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                
return saveFileDialog.FileName;
            }
            
else
            {
                
return null;
            }
        }

        
//保存与否及显示统计字数
        private void richTextForm_TextChanged_1(object sender, EventArgs e)
        {
            needToSave 
= true;
            
this.toolStripStatusTotalAmount.Text = "当前文本字数合计:" + richTextForm.Text.Length;
        }

        
//菜单项下事件发生及时更新其菜单的属性
        private void menuEdit_DropDownOpened(object sender, EventArgs e)
        {
            
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
            {
                
this.menuEditPaste.Enabled = true;
            }
            
else
            {
                
this.menuEditPaste.Enabled = false;
            }

            
if (richTextForm.SelectedText.Length > 0)
            {
                menuEditCopy.Enabled 
= true;
                menuEditCut.Enabled 
= true;
                menuEditDelete.Enabled 
= true;
            }
            
else
            {
                menuEditCopy.Enabled 
= false;
                menuEditCut.Enabled 
= false;
                menuEditDelete.Enabled 
= false;
            }
            
            
if (richTextForm.CanUndo == true)
            {
                menuEditorUndo.Enabled 
= true;
            }
            
else
            {
                menuEditorUndo.Enabled 
= false;
            }

            
if (richTextForm.Text.Length > 0)
            {
                menuEditAll.Enabled 
= true;
                menuEditSearch.Enabled 
= true;
            }
            
else
            {
                menuEditAll.Enabled 
= false;
                menuEditSearch.Enabled 
= false;
            }
        }

        
//右键设置
        private void contextMSTUndo_Click(object sender, EventArgs e)   
        {
            richTextForm.Undo();
        }

        
private void contextMSTCopy_Click(object sender, EventArgs e)
        {
            
if (richTextForm.SelectedText == "")
                
return;
            
else
                richTextForm.Copy();
        }

        
private void contextMSTCut_Click(object sender, EventArgs e)
        {
            
if (richTextForm.SelectedText == "")
                
return;
            
else
                richTextForm.Cut();
        }

        
private void contextMSTPaste_Click(object sender, EventArgs e)
        {
            richTextForm.Paste();
        }

        
private void contextMSTAll_Click(object sender, EventArgs e)
        {
            richTextForm.SelectAll();
        }

        
private void contextMSTBig_Click_1(object sender, EventArgs e)
        {
            
this.WindowState = FormWindowState.Maximized;
        }

        
private void contextMSTSmall_Click_1(object sender, EventArgs e)
        {
            
this.WindowState = FormWindowState.Minimized;
        }

        
private void contextMSTNormal_Click(object sender, EventArgs e)
        {
            
this.WindowState = FormWindowState.Normal;
        }

        
//右键设置菜单项下事件发生及时更新其菜单的属性
        private void contextMenuStripText_Opening(object sender, CancelEventArgs e)
        {
            
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
            {
                
this.contextMSTPaste.Enabled = true;
            }
            
else
            {
                
this.contextMSTPaste.Enabled = false;
            }

            
if (richTextForm.SelectedText.Length > 0)
            {
                contextMSTCopy.Enabled 
= true;
                contextMSTCut.Enabled 
= true;
                
            }
            
else
            {
                contextMSTCopy.Enabled 
= false;
                contextMSTCut.Enabled 
= false;
            }

            
if (richTextForm.CanUndo == true)
            {
                contextMSTUndo.Enabled 
= true;
            }
            
else
            {
                contextMSTUndo.Enabled 
= false;
            }

            
if (richTextForm.Text.Length > 0)
            {
                contextMSTAll.Enabled 
= true;
            }
            
else
            {
                contextMSTAll.Enabled 
= false;
            }
        }
    }
}

 

 

 至此完成!

posted @ 2009-12-27 00:32  Jerome_wu  阅读(834)  评论(1编辑  收藏  举报