实例介绍
【实例截图】
【核心代码】
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 记事本程序设计
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuWenjian;
private System.Windows.Forms.MenuItem menuBianji;
private System.Windows.Forms.MenuItem menuBaocun;
private System.Windows.Forms.MenuItem menuLingcunwei;
private System.Windows.Forms.MenuItem menuTuichu;
private System.Windows.Forms.MenuItem menuChexiao;
private System.Windows.Forms.MenuItem menuJianqie;
private System.Windows.Forms.MenuItem menuFuzhi;
private System.Windows.Forms.MenuItem menuQuanxuan;
private System.Windows.Forms.MenuItem menuNiantie;
private System.Windows.Forms.MenuItem menuShanchu;
private System.Windows.Forms.MenuItem menuGeshi;
private System.Windows.Forms.MenuItem menuBangzhu;
private System.Windows.Forms.MenuItem menuZiti;
private System.Windows.Forms.MenuItem menuGuanyuzhuti;
private System.Windows.Forms.MenuItem menuGuanyujishiben;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ToolBarButton toolBarButton4;
private System.Windows.Forms.ToolBarButton toolBarButton5;
private System.Windows.Forms.ToolBarButton toolBarButton8;
private System.Windows.Forms.ToolBarButton toolBarButton7;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.MenuItem menuDakai;
private System.Windows.Forms.MenuItem menuXinjian;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.FontDialog fontDialog1;
private System.Windows.Forms.MenuItem menuChexiao2;
private System.Windows.Forms.MenuItem menuFengexian;
private System.Windows.Forms.MenuItem menuFengexian3;
private System.Windows.Forms.MenuItem menuFengexian4;
private System.Windows.Forms.MenuItem menuFengexian6;
private System.Windows.Forms.MenuItem menuJianqie2;
private System.Windows.Forms.MenuItem menuFuzhi2;
private System.Windows.Forms.MenuItem menuNiantie2;
private System.Windows.Forms.MenuItem menuShanchu2;
private System.Windows.Forms.MenuItem menuFengexian7;
private System.Windows.Forms.MenuItem menuQuanxuan2;
private System.Windows.Forms.MenuItem menuBeijingyanse;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.menuWenjian = new System.Windows.Forms.MenuItem();
this.menuXinjian = new System.Windows.Forms.MenuItem();
this.menuDakai = new System.Windows.Forms.MenuItem();
this.menuBaocun = new System.Windows.Forms.MenuItem();
this.menuLingcunwei = new System.Windows.Forms.MenuItem();
this.menuFengexian = new System.Windows.Forms.MenuItem();
this.menuTuichu = new System.Windows.Forms.MenuItem();
this.menuBianji = new System.Windows.Forms.MenuItem();
this.menuChexiao = new System.Windows.Forms.MenuItem();
this.menuFengexian3 = new System.Windows.Forms.MenuItem();
this.menuJianqie = new System.Windows.Forms.MenuItem();
this.menuFuzhi = new System.Windows.Forms.MenuItem();
this.menuNiantie = new System.Windows.Forms.MenuItem();
this.menuShanchu = new System.Windows.Forms.MenuItem();
this.menuFengexian4 = new System.Windows.Forms.MenuItem();
this.menuQuanxuan = new System.Windows.Forms.MenuItem();
this.menuGeshi = new System.Windows.Forms.MenuItem();
this.menuBeijingyanse = new System.Windows.Forms.MenuItem();
this.menuZiti = new System.Windows.Forms.MenuItem();
this.menuBangzhu = new System.Windows.Forms.MenuItem();
this.menuGuanyuzhuti = new System.Windows.Forms.MenuItem();
this.menuGuanyujishiben = new System.Windows.Forms.MenuItem();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton8 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuChexiao2 = new System.Windows.Forms.MenuItem();
this.menuFengexian6 = new System.Windows.Forms.MenuItem();
this.menuJianqie2 = new System.Windows.Forms.MenuItem();
this.menuFuzhi2 = new System.Windows.Forms.MenuItem();
this.menuNiantie2 = new System.Windows.Forms.MenuItem();
this.menuShanchu2 = new System.Windows.Forms.MenuItem();
this.menuFengexian7 = new System.Windows.Forms.MenuItem();
this.menuQuanxuan2 = new System.Windows.Forms.MenuItem();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.fontDialog1 = new System.Windows.Forms.FontDialog();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuWenjian,
this.menuBianji,
this.menuGeshi,
this.menuBangzhu});
//
// menuWenjian
//
this.menuWenjian.Index = 0;
this.menuWenjian.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuXinjian,
this.menuDakai,
this.menuBaocun,
this.menuLingcunwei,
this.menuFengexian,
this.menuTuichu});
this.menuWenjian.Text = "文件(&F)";
this.menuWenjian.Click = new System.EventHandler(this.menuWenjian_Click);
//
// menuXinjian
//
this.menuXinjian.Index = 0;
this.menuXinjian.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.menuXinjian.Text = "新建(&N)";
this.menuXinjian.Click = new System.EventHandler(this.menuXinjian_Click);
//
// menuDakai
//
this.menuDakai.Index = 1;
this.menuDakai.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
this.menuDakai.Text = "打开(&O)...";
this.menuDakai.Click = new System.EventHandler(this.menuDakai_Click);
//
// menuBaocun
//
this.menuBaocun.Index = 2;
this.menuBaocun.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
this.menuBaocun.Text = "保存(&S)";
this.menuBaocun.Click = new System.EventHandler(this.menuBaocun_Click);
//
// menuLingcunwei
//
this.menuLingcunwei.Index = 3;
this.menuLingcunwei.Text = "另存为(&A)...";
this.menuLingcunwei.Click = new System.EventHandler(this.menuLingcunwei_Click_1);
//
// menuFengexian
//
this.menuFengexian.Index = 4;
this.menuFengexian.Text = "-";
//
// menuTuichu
//
this.menuTuichu.Index = 5;
this.menuTuichu.Text = "退出(&X)";
this.menuTuichu.Click = new System.EventHandler(this.menuTuichu_Click);
//
// menuBianji
//
this.menuBianji.Index = 1;
this.menuBianji.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuChexiao,
this.menuFengexian3,
this.menuJianqie,
this.menuFuzhi,
this.menuNiantie,
this.menuShanchu,
this.menuFengexian4,
this.menuQuanxuan});
this.menuBianji.Text = "编辑(&E)";
//
// menuChexiao
//
this.menuChexiao.Index = 0;
this.menuChexiao.Shortcut = System.Windows.Forms.Shortcut.CtrlZ;
this.menuChexiao.Text = "撤消(&U)";
this.menuChexiao.Click = new System.EventHandler(this.menuChexiao_Click);
//
// menuFengexian3
//
this.menuFengexian3.Index = 1;
this.menuFengexian3.Text = "-";
//
// menuJianqie
//
this.menuJianqie.Index = 2;
this.menuJianqie.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
this.menuJianqie.Text = "剪切(&T)";
this.menuJianqie.Click = new System.EventHandler(this.menuJianqie_Click);
//
// menuFuzhi
//
this.menuFuzhi.Index = 3;
this.menuFuzhi.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
this.menuFuzhi.Text = "复制(&C)";
this.menuFuzhi.Click = new System.EventHandler(this.menuFuzhi_Click);
//
// menuNiantie
//
this.menuNiantie.Index = 4;
this.menuNiantie.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
this.menuNiantie.Text = "粘贴(&P)";
this.menuNiantie.Click = new System.EventHandler(this.menuNiantie_Click);
//
// menuShanchu
//
this.menuShanchu.Index = 5;
this.menuShanchu.Shortcut = System.Windows.Forms.Shortcut.Del;
this.menuShanchu.Text = "删除(&L)";
this.menuShanchu.Click = new System.EventHandler(this.menuShanchu_Click);
//
// menuFengexian4
//
this.menuFengexian4.Index = 6;
this.menuFengexian4.Text = "-";
//
// menuQuanxuan
//
this.menuQuanxuan.Index = 7;
this.menuQuanxuan.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
this.menuQuanxuan.Text = "全选(&A)";
this.menuQuanxuan.Click = new System.EventHandler(this.menuQuanxuan_Click);
//
// menuGeshi
//
this.menuGeshi.Index = 2;
this.menuGeshi.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuBeijingyanse,
this.menuZiti});
this.menuGeshi.Text = "格式(&O)";
//
// menuBeijingyanse
//
this.menuBeijingyanse.Index = 0;
this.menuBeijingyanse.Text = "背景颜色(&C)";
this.menuBeijingyanse.Click = new System.EventHandler(this.menuZhidonghuanhang_Click);
//
// menuZiti
//
this.menuZiti.Index = 1;
this.menuZiti.MdiList = true;
this.menuZiti.Text = "字体(&F)...";
this.menuZiti.Click = new System.EventHandler(this.menuZiti_Click);
//
// menuBangzhu
//
this.menuBangzhu.Index = 3;
this.menuBangzhu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuGuanyuzhuti,
this.menuGuanyujishiben});
this.menuBangzhu.Text = "帮助(&H)";
//
// menuGuanyuzhuti
//
this.menuGuanyuzhuti.Index = 0;
this.menuGuanyuzhuti.ShowShortcut = false;
this.menuGuanyuzhuti.Text = "关于主题(&H)";
this.menuGuanyuzhuti.Click = new System.EventHandler(this.menuGuanyuzhuti_Click);
//
// menuGuanyujishiben
//
this.menuGuanyujishiben.Index = 1;
this.menuGuanyujishiben.Text = "关于记事本(&A)";
this.menuGuanyujishiben.Click = new System.EventHandler(this.menuGuanyujishiben_Click);
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth4Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// statusBar1
//
this.statusBar1.AllowDrop = true;
this.statusBar1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.statusBar1.Location = new System.Drawing.Point(0, 335);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(592, 26);
this.statusBar1.TabIndex = 2;
this.statusBar1.PanelClick = new System.Windows.Forms.StatusBarPanelClickEventHandler(this.statusBar1_PanelClick);
//
// toolBarButton1
//
this.toolBarButton1.ImageIndex = 0;
this.toolBarButton1.Name = "toolBarButton1";
this.toolBarButton1.ToolTipText = "新建";
//
// toolBarButton2
//
this.toolBarButton2.ImageIndex = 1;
this.toolBarButton2.Name = "toolBarButton2";
this.toolBarButton2.ToolTipText = "打开";
//
// toolBarButton3
//
this.toolBarButton3.ImageIndex = 2;
this.toolBarButton3.Name = "toolBarButton3";
this.toolBarButton3.ToolTipText = "保存";
//
// toolBarButton4
//
this.toolBarButton4.ImageIndex = 3;
this.toolBarButton4.Name = "toolBarButton4";
this.toolBarButton4.ToolTipText = "剪切";
//
// toolBarButton5
//
this.toolBarButton5.ImageIndex = 4;
this.toolBarButton5.Name = "toolBarButton5";
this.toolBarButton5.ToolTipText = "复制";
//
// toolBarButton8
//
this.toolBarButton8.ImageIndex = 5;
this.toolBarButton8.Name = "toolBarButton8";
this.toolBarButton8.ToolTipText = "粘贴";
//
// toolBarButton7
//
this.toolBarButton7.ImageIndex = 6;
this.toolBarButton7.Name = "toolBarButton7";
this.toolBarButton7.ToolTipText = "帮助";
//
// toolBar1
//
this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3,
this.toolBarButton4,
this.toolBarButton5,
this.toolBarButton8,
this.toolBarButton7});
this.toolBar1.ButtonSize = new System.Drawing.Size(23, 22);
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Location = new System.Drawing.Point(0, 0);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(592, 28);
this.toolBar1.TabIndex = 0;
this.toolBar1.ButtonClick = new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// richTextBox1
//
this.richTextBox1.AllowDrop = true;
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBox1.AutoSize = true;
this.richTextBox1.ContextMenu = this.contextMenu1;
this.richTextBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.richTextBox1.Size = new System.Drawing.Size(595, 329);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuChexiao2,
this.menuFengexian6,
this.menuJianqie2,
this.menuFuzhi2,
this.menuNiantie2,
this.menuShanchu2,
this.menuFengexian7,
this.menuQuanxuan2});
//
// menuChexiao2
//
this.menuChexiao2.Index = 0;
this.menuChexiao2.Shortcut = System.Windows.Forms.Shortcut.CtrlU;
this.menuChexiao2.ShowShortcut = false;
this.menuChexiao2.Text = "撤消(&U)";
this.menuChexiao2.Click = new System.EventHandler(this.menuChexiao2_Click);
//
// menuFengexian6
//
this.menuFengexian6.Index = 1;
this.menuFengexian6.Text = "-";
//
// menuJianqie2
//
this.menuJianqie2.Index = 2;
this.menuJianqie2.Shortcut = System.Windows.Forms.Shortcut.CtrlT;
this.menuJianqie2.ShowShortcut = false;
this.menuJianqie2.Text = "剪切(&T)";
this.menuJianqie2.Click = new System.EventHandler(this.menuJianqie2_Click);
//
// menuFuzhi2
//
this.menuFuzhi2.Index = 3;
this.menuFuzhi2.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
this.menuFuzhi2.ShowShortcut = false;
this.menuFuzhi2.Text = "复制(&C)";
this.menuFuzhi2.Click = new System.EventHandler(this.menuFuzhi2_Click);
//
// menuNiantie2
//
this.menuNiantie2.Index = 4;
this.menuNiantie2.Shortcut = System.Windows.Forms.Shortcut.CtrlP;
this.menuNiantie2.ShowShortcut = false;
this.menuNiantie2.Text = "粘贴(&P)";
this.menuNiantie2.Click = new System.EventHandler(this.menuNiantie2_Click);
//
// menuShanchu2
//
this.menuShanchu2.Index = 5;
this.menuShanchu2.Shortcut = System.Windows.Forms.Shortcut.CtrlL;
this.menuShanchu2.ShowShortcut = false;
this.menuShanchu2.Text = "删除(&L)";
this.menuShanchu2.Click = new System.EventHandler(this.menuShanchu2_Click);
//
// menuFengexian7
//
this.menuFengexian7.Index = 6;
this.menuFengexian7.Text = "-";
//
// menuQuanxuan2
//
this.menuQuanxuan2.Index = 7;
this.menuQuanxuan2.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
this.menuQuanxuan2.ShowShortcut = false;
this.menuQuanxuan2.Text = "全选(&A)";
this.menuQuanxuan2.Click = new System.EventHandler(this.menuItem5_Click);
//
// openFileDialog1
//
this.openFileDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";
this.openFileDialog1.Title = "快乐打开";
//
// saveFileDialog1
//
this.saveFileDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";
this.saveFileDialog1.InitialDirectory = "C:\\Documents and Settings\\user\\My Documents";
this.saveFileDialog1.Title = "快乐保存";
//
// fontDialog1
//
this.fontDialog1.ShowApply = true;
this.fontDialog1.ShowColor = true;
//
// notifyIcon1
//
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Visible = true;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(592, 361);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.toolBar1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "记事本";
this.Load = new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
int imageList1=e.Button.ImageIndex;
if(imageList1==0)
{
menuXinjian_Click(sender,e);
}
if(imageList1==1)
{
menuDakai_Click(sender,e);
}
if(imageList1==2)
{
menuBaocun_Click(sender,e);
}
if(imageList1==3)
{
richTextBox1.Cut();
}
if(imageList1==4)
{
richTextBox1.Copy();
}
if(imageList1==5)
{
richTextBox1.Paste();
}
if(imageList1==6)
{
bangzhu();
}
}
private void menuDakai_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.DialogResult yzResult;
string yzmsg;
string m_strFileName="";
string m_strTemp="";
statusBar1.Text="打开中...";
if(richTextBox1.Text!="")
{//如果rcihTextBox中有文本
if(ModifiedFlag==true)
{//如果该文本被修改过,就提示是否保存文件
yzmsg="文件" FileName "是否保存该文件?\n";
yzResult=MessageBox.Show(this,yzmsg,"记事本",System.Windows.Forms.MessageBoxButtons.YesNoCancel,System.Windows.Forms.MessageBoxIcon.Exclamation,System.Windows.Forms.MessageBoxDefaultButton.Button1);
if(yzResult==DialogResult.Yes)
{//如果保存的对话框选择的结果为Yes,则保存文件
if(IsSavedFlag==true)
{//如果已经保存过,调用保存文件菜单的PerformClick方法
menuBaocun.PerformClick();
}
else
{//如果还没有保存过,调用另存为文件菜单的PerformClick方法
menuLingcunwei.PerformClick();
}
}
if(yzResult==DialogResult.Cancel)
{//如果保存的对话框选择的结果为Cancel
return;
}
}
}
openFileDialog1.Filter="文本文件(*.txt)|*.txt";//设置文件过滤器
openFileDialog1.FileName="";//初始化文件名变量
//弹出打开文件对话框
if(openFileDialog1.ShowDialog()==DialogResult.OK||openFileDialog1.FileName.Length>=0)
{//如果选择了要打开的文件,且点击了"打开"按钮
m_strFileName=openFileDialog1.FileName;
FileName=m_strFileName;
}
else
{
return;
}
richTextBox1.Text="";//清空richTextBOx
FileStream m_FileStream=null;//声明一个文件流对象
string m_strFileContents="";
m_strOriginText="";
if(m_strFileName!="")
{//如果选择了要打开的文件
try
{
m_strTemp="";
int m_GetByte=1;
//创建一个FileStream类的对象m_FileStream
m_FileStream=new FileStream(m_strFileName,FileMode.Open,FileAccess.Read);
Byte[] m_Byte=new Byte[1024];//声明一个数组
m_Byte.Initialize();//初始化该数组
for(;m_GetByte!=0;)
{//按字节读文件
System.Text.Encoding encoding=System.Text.Encoding.UTF8;
m_Byte.Initialize();
m_strTemp="";
m_GetByte=m_FileStream.Read(m_Byte,0,1024);
m_strTemp=encoding.GetString(m_Byte,0,1024);
m_strFileContents =m_strTemp;//把结果保存在m_strFileContents中
}
}
catch(FileNotFoundException e1)
{//捕获错误
string m_strmsg="文件没有找到:" e1;
System.Windows.Forms.MessageBox.Show(m_strmsg,"记事本");
return;
}
finally
{
if(m_FileStream!=null)
{
m_FileStream.Close();
}
}
}
m_strOriginText=m_strFileContents;
richTextBox1.Text=m_strFileContents;
this.Text=m_strFileName "无标题——记事本";
statusBar1.Text="就绪";
ExistFlag=true;
IsSavedFlag=false;
ModifiedFlag=false;
richTextBox1.Select(0,0);
}
private void menuBaocun_Click(object sender, System.EventArgs e)
{
SaveFile();//调用SaveFile方法
statusBar1.Text="就绪";//设置状态栏信息
}
private void SaveDocFile(string m_strFileName)
{
this.Text=m_strFileName "——记事本";//改变窗体文本状态
richTextBox1.Select(0,0);//定义文本框的输入光标位置
FileStream m_strStream=null;//声名一个文件流对象
try
{//保存文件
System.Text.Encoding encoding=System.Text.Encoding.UTF8;//设定编码方式
//创建一个FileStream类对象
m_strStream=new
FileStream(m_strFileName,FileMode.Create,FileAccess.Write);
Byte[] m_Byte; //声名数组用于保存文本框的内容
m_Byte=new byte [richTextBox1.Text.Length];//声明一个文件大小的数组
m_Byte.Initialize();
//写文件到指定的文件名中
m_Byte=encoding.GetBytes(richTextBox1.Text.ToString());
m_strStream.Write(m_Byte,0,m_Byte.Length);
}
catch(Exception e)
{
string m_strmsg="Error:" e;//捕捉错误信息
MessageBox.Show(m_strmsg,"记事本");
return;
}
finally //对错误处理方法
{
if(m_strStream!=null)
m_strStream.Close();
}
this.Text=m_strFileName "——记事本";
IsSavedFlag=true;
ModifiedFlag=false;
}
private void SaveFile()
{
string m_strFileName=""; //用于保存文件名
saveFileDialog1.Filter="文本文件(*.txt)|*.txt";//设置文件过滤器
statusBar1.Text="保存文件";//在状态栏显示状态
if(FileName=="无标题")//判定FileName状态
//调用保存文件对话框
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{ //如果点击了对话框中的保存
FileName=m_strFileName=saveFileDialog1.FileName;//则为文件命名
}
else
{
return;
}
SaveDocFile(FileName);//调用SaveDocFile方法
}
private void menuWenjian_Click(object sender, System.EventArgs e)
{
}
string FileName;//保存文件名的位置
bool ModifiedFlag;//标识文件是否被修改过
bool IsSavedFlag;//标识文件是否保存过
bool ExistFlag; //标识richTextBox中是否存在文件
string m_strOriginText;//保存richTextBox中原始的文件
private void Form1_Load(object sender, System.EventArgs e)
{
ModifiedFlag=true;//设置文件被修改过
IsSavedFlag=false;//设置文件被保存过
FileName="无标题";//设置文件名
this.Text=FileName "——记事本";//设置“我的//记事本”的窗口标题
m_strOriginText="额";//状态栏设置
}
private void menuLingcunwei_Click_1(object sender, System.EventArgs e)
{
FileName="无标题";
menuBaocun_Click(sender,e);//调用保存文件菜单的事件代码
}
private void menuTuichu_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
richTextBox1.SelectAll();
}
private void menuZiti_Click(object sender, System.EventArgs e)
{
fontDialog1.ShowDialog();
richTextBox1.Font=fontDialog1.Font;
richTextBox1.ForeColor=fontDialog1.Color;
}
private void menuNiantie_Click(object sender, System.EventArgs e)
{
richTextBox1.Paste();
}
private void menuChexiao_Click(object sender, System.EventArgs e)
{
richTextBox1.Undo();
}
private void menuJianqie_Click(object sender, System.EventArgs e)
{
richTextBox1.Cut();
}
private void menuFuzhi_Click(object sender, System.EventArgs e)
{
richTextBox1.Copy();
}
private void menuShanchu_Click(object sender, System.EventArgs e)
{
richTextBox1.Clear();
this.Text="无标题——写字板";
}
private void menuQuanxuan_Click(object sender, System.EventArgs e)
{
richTextBox1.SelectAll();
}
private void menuGuanyujishiben_Click(object sender, System.EventArgs e)
{
bangzhu();
}
private void bangzhu()
{
string str = "ACCP(R) 记事本\n\n版本 \n\n2\n***********************************\n\n授权给:\n\n\nACCP()\n";
MessageBox.Show(str,"关于记事本");
}
private void menuGuanyuzhuti_Click(object sender, System.EventArgs e)
{
zhuti();
}
private void zhuti()
{
string str="用来保存信息,仅供学习!\n\n\t ^(00)^";
MessageBox.Show(str,"关于 主题");
}
private void menuChexiao2_Click(object sender, System.EventArgs e)
{
richTextBox1.Undo();
}
private void menuJianqie2_Click(object sender, System.EventArgs e)
{
richTextBox1.Cut();
}
private void menuFuzhi2_Click(object sender, System.EventArgs e)
{
richTextBox1.Copy();
}
private void menuNiantie2_Click(object sender, System.EventArgs e)
{
richTextBox1.Paste();
}
private void menuShanchu2_Click(object sender, System.EventArgs e)
{
richTextBox1.Clear();
}
private void menuZhidonghuanhang_Click(object sender, System.EventArgs e)
{
colorDialog1.ShowDialog();
richTextBox1.BackColor=colorDialog1.Color;
}
private void menuXinjian_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.DialogResult yzResult; //定义变量来保存对话框选择的结果
string yzMsg;
yzMsg="是否保存文档?\n" FileName;
richTextBox1.Select(0,0);
if(richTextBox1.Text!=m_strOriginText)//当前richTextBox1中的文本与上次不一样时
ModifiedFlag=true;//将modifiedFlag设为True,表示文本被修改过
if(richTextBox1.Text!="")
{
if(ModifiedFlag==true)//如果richTextBox1中有文本
{//如果文本被修改过,就提示是否保存文件
yzResult=MessageBox.Show(this,yzMsg,"文本文件",System.Windows.Forms.MessageBoxButtons.YesNoCancel,System.Windows.Forms.MessageBoxIcon.Exclamation,System.Windows.Forms.MessageBoxDefaultButton.Button1);
if(yzResult==System.Windows.Forms.DialogResult.Yes)
{//如果保存的对话框选择结果为Yes,则保存文件
if(IsSavedFlag)
{ //如果已经保存过,调用保存文件菜单的PerformClick方法
menuBaocun.PerformClick();
}
else
{//如果还没有保存过,调用另存为文件菜单的PerformClick方法
menuLingcunwei.PerformClick();
}
richTextBox1.Text="";//变量初始化
ModifiedFlag=false;
ExistFlag=false;
IsSavedFlag=false;
FileName="新建文件";
this.Text=FileName "——无标题";
statusBar1.Text="新建完成";
}
else if(yzResult==System.Windows.Forms.DialogResult.No)
{//如果保存的对话框选择的结果为No,则保存文件
richTextBox1.Text="";
richTextBox1.SelectAll();
ModifiedFlag=false;
ExistFlag=false;
IsSavedFlag=false;
FileName="新建文件";
this.Text=FileName "——无标题";
statusBar1.Text="新建完成";
}
}
else
{//如果richTextBox1中没有文本的话
richTextBox1.Text="";
ModifiedFlag=false;
ExistFlag=false;
IsSavedFlag=false;
FileName="新建文件";
this.Text=FileName "——无标题";
statusBar1.Text="新建完成";
}
}
}
private void statusBar1_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e)
{
}
}
}
标签: 记事本
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论