www.pudn.com > notepad.rar > Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
namespace notepad
{
///
/// Form1 的摘要说明。
///
public class NotePad : System.Windows.Forms.Form
{
//皮肤
[DllImport("appface.dll")]
public static extern long SkinStart(string SkinFile,int nDefWinType,string CheckSum,long nType,
long hInstance,long nLen);
[DllImport("appface.dll")]
public static extern long SkinRemove() ;
[DllImport("appface.dll")]
public static extern long SkinWindowSet(IntPtr hWnd,long nSkintype);
//
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.FontDialog fontDialog1;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem menuItem15;
///
/// 必需的设计器变量。
private string CurrentFileName = "新建文本";
private bool needtosave;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.MenuItem menuItem24;
private System.Windows.Forms.MenuItem menuItemAbout;
private System.Windows.Forms.MenuItem menuItemNew;
private System.Windows.Forms.MenuItem menuItemOpen;
private System.Windows.Forms.MenuItem menuItemSave;
private System.Windows.Forms.MenuItem menuItemSaveAs;
private System.Windows.Forms.MenuItem menuItemExit;
private System.Windows.Forms.MenuItem menuItemUndo;
private System.Windows.Forms.MenuItem menuItemCut;
private System.Windows.Forms.MenuItem menuItemCopy;
private System.Windows.Forms.MenuItem menuItemPaste;
private System.Windows.Forms.MenuItem menuItemDel;
private System.Windows.Forms.MenuItem menuItemAll;
private System.Windows.Forms.MenuItem menuItemTime;
private System.Windows.Forms.MenuItem menuItemReturn;
private System.Windows.Forms.MenuItem menuItemFont;
private System.Windows.Forms.MenuItem menuItemBackcolor;
private System.Windows.Forms.MenuItem menuItemFile;
private System.Windows.Forms.MenuItem menuItemEdit;
private System.Windows.Forms.MenuItem menuItemFormat;
private System.Windows.Forms.MenuItem menuItemHelp;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItemJingDian;
private System.Windows.Forms.MenuItem menuItemChunTian;
private System.Windows.Forms.MenuItem menuItemHejin;
private System.Windows.Forms.MenuItem menuItemCancelSkin;
private System.Windows.Forms.MenuItem menuItemLoadSkin;
private IContainer components;
public NotePad()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(NotePad));
this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
this.menuItemFile = new System.Windows.Forms.MenuItem();
this.menuItemNew = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItemOpen = new System.Windows.Forms.MenuItem();
this.menuItemSave = new System.Windows.Forms.MenuItem();
this.menuItemSaveAs = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItemExit = new System.Windows.Forms.MenuItem();
this.menuItemEdit = new System.Windows.Forms.MenuItem();
this.menuItemUndo = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItemCut = new System.Windows.Forms.MenuItem();
this.menuItemCopy = new System.Windows.Forms.MenuItem();
this.menuItemPaste = new System.Windows.Forms.MenuItem();
this.menuItemDel = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItemAll = new System.Windows.Forms.MenuItem();
this.menuItemTime = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItemChunTian = new System.Windows.Forms.MenuItem();
this.menuItemJingDian = new System.Windows.Forms.MenuItem();
this.menuItemHejin = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItemCancelSkin = new System.Windows.Forms.MenuItem();
this.menuItemLoadSkin = new System.Windows.Forms.MenuItem();
this.menuItemFormat = new System.Windows.Forms.MenuItem();
this.menuItemReturn = new System.Windows.Forms.MenuItem();
this.menuItem24 = new System.Windows.Forms.MenuItem();
this.menuItemFont = new System.Windows.Forms.MenuItem();
this.menuItemBackcolor = new System.Windows.Forms.MenuItem();
this.menuItemHelp = new System.Windows.Forms.MenuItem();
this.menuItemAbout = new System.Windows.Forms.MenuItem();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.fontDialog1 = new System.Windows.Forms.FontDialog();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.textBox = new System.Windows.Forms.TextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// mainMenu
//
this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemFile,
this.menuItemEdit,
this.menuItem2,
this.menuItemFormat,
this.menuItemHelp});
//
// menuItemFile
//
this.menuItemFile.Index = 0;
this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemNew,
this.menuItem1,
this.menuItemOpen,
this.menuItemSave,
this.menuItemSaveAs,
this.menuItem7,
this.menuItemExit});
resources.ApplyResources(this.menuItemFile, "menuItemFile");
//
// menuItemNew
//
this.menuItemNew.Index = 0;
resources.ApplyResources(this.menuItemNew, "menuItemNew");
this.menuItemNew.Click += new System.EventHandler(this.menuItemNew_Click);
//
// menuItem1
//
this.menuItem1.Index = 1;
resources.ApplyResources(this.menuItem1, "menuItem1");
//
// menuItemOpen
//
this.menuItemOpen.Index = 2;
resources.ApplyResources(this.menuItemOpen, "menuItemOpen");
this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
//
// menuItemSave
//
this.menuItemSave.Index = 3;
resources.ApplyResources(this.menuItemSave, "menuItemSave");
this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
//
// menuItemSaveAs
//
this.menuItemSaveAs.Index = 4;
resources.ApplyResources(this.menuItemSaveAs, "menuItemSaveAs");
this.menuItemSaveAs.Click += new System.EventHandler(this.menuItemSaveAs_Click);
//
// menuItem7
//
this.menuItem7.Index = 5;
resources.ApplyResources(this.menuItem7, "menuItem7");
//
// menuItemExit
//
this.menuItemExit.Index = 6;
resources.ApplyResources(this.menuItemExit, "menuItemExit");
this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
//
// menuItemEdit
//
this.menuItemEdit.Index = 1;
this.menuItemEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemUndo,
this.menuItem10,
this.menuItemCut,
this.menuItemCopy,
this.menuItemPaste,
this.menuItemDel,
this.menuItem15,
this.menuItemAll,
this.menuItemTime});
resources.ApplyResources(this.menuItemEdit, "menuItemEdit");
this.menuItemEdit.Popup += new System.EventHandler(this.menuItemEdit_Popup);
//
// menuItemUndo
//
this.menuItemUndo.Index = 0;
resources.ApplyResources(this.menuItemUndo, "menuItemUndo");
this.menuItemUndo.Click += new System.EventHandler(this.menuItemUndo_Click);
//
// menuItem10
//
this.menuItem10.Index = 1;
resources.ApplyResources(this.menuItem10, "menuItem10");
//
// menuItemCut
//
this.menuItemCut.Index = 2;
resources.ApplyResources(this.menuItemCut, "menuItemCut");
this.menuItemCut.Click += new System.EventHandler(this.menuItemCut_Click);
//
// menuItemCopy
//
this.menuItemCopy.Index = 3;
resources.ApplyResources(this.menuItemCopy, "menuItemCopy");
this.menuItemCopy.Click += new System.EventHandler(this.menuItemCopy_Click);
//
// menuItemPaste
//
this.menuItemPaste.Index = 4;
resources.ApplyResources(this.menuItemPaste, "menuItemPaste");
this.menuItemPaste.Click += new System.EventHandler(this.menuItemPaste_Click);
//
// menuItemDel
//
this.menuItemDel.Index = 5;
resources.ApplyResources(this.menuItemDel, "menuItemDel");
this.menuItemDel.Click += new System.EventHandler(this.menuItemDel_Click);
//
// menuItem15
//
this.menuItem15.Index = 6;
resources.ApplyResources(this.menuItem15, "menuItem15");
//
// menuItemAll
//
this.menuItemAll.Index = 7;
resources.ApplyResources(this.menuItemAll, "menuItemAll");
this.menuItemAll.Click += new System.EventHandler(this.menuItemAll_Click);
//
// menuItemTime
//
this.menuItemTime.Index = 8;
resources.ApplyResources(this.menuItemTime, "menuItemTime");
this.menuItemTime.Click += new System.EventHandler(this.menuItemTime_Click);
//
// menuItem2
//
this.menuItem2.Index = 2;
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemChunTian,
this.menuItemJingDian,
this.menuItemHejin,
this.menuItem9,
this.menuItemCancelSkin,
this.menuItemLoadSkin});
resources.ApplyResources(this.menuItem2, "menuItem2");
//
// menuItemChunTian
//
this.menuItemChunTian.Checked = true;
this.menuItemChunTian.Index = 0;
resources.ApplyResources(this.menuItemChunTian, "menuItemChunTian");
this.menuItemChunTian.Click += new System.EventHandler(this.menuItemChunTian_Click);
//
// menuItemJingDian
//
this.menuItemJingDian.Index = 1;
resources.ApplyResources(this.menuItemJingDian, "menuItemJingDian");
this.menuItemJingDian.Click += new System.EventHandler(this.menuItemJingDian_Click);
//
// menuItemHejin
//
this.menuItemHejin.Index = 2;
resources.ApplyResources(this.menuItemHejin, "menuItemHejin");
this.menuItemHejin.Click += new System.EventHandler(this.menuItemHejin_Click);
//
// menuItem9
//
this.menuItem9.Index = 3;
resources.ApplyResources(this.menuItem9, "menuItem9");
//
// menuItemCancelSkin
//
this.menuItemCancelSkin.Index = 4;
resources.ApplyResources(this.menuItemCancelSkin, "menuItemCancelSkin");
this.menuItemCancelSkin.Click += new System.EventHandler(this.menuItemCancelSkin_Click);
//
// menuItemLoadSkin
//
resources.ApplyResources(this.menuItemLoadSkin, "menuItemLoadSkin");
this.menuItemLoadSkin.Index = 5;
this.menuItemLoadSkin.Click += new System.EventHandler(this.menuItemLoadSkin_Click);
//
// menuItemFormat
//
this.menuItemFormat.Index = 3;
this.menuItemFormat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemReturn,
this.menuItem24,
this.menuItemFont,
this.menuItemBackcolor});
resources.ApplyResources(this.menuItemFormat, "menuItemFormat");
//
// menuItemReturn
//
this.menuItemReturn.Index = 0;
this.menuItemReturn.RadioCheck = true;
resources.ApplyResources(this.menuItemReturn, "menuItemReturn");
this.menuItemReturn.Click += new System.EventHandler(this.menuItemReturn_Click);
//
// menuItem24
//
this.menuItem24.Index = 1;
resources.ApplyResources(this.menuItem24, "menuItem24");
//
// menuItemFont
//
this.menuItemFont.Index = 2;
resources.ApplyResources(this.menuItemFont, "menuItemFont");
this.menuItemFont.Click += new System.EventHandler(this.menuItemFont_Click);
//
// menuItemBackcolor
//
this.menuItemBackcolor.Index = 3;
resources.ApplyResources(this.menuItemBackcolor, "menuItemBackcolor");
this.menuItemBackcolor.Click += new System.EventHandler(this.menuItemBackcolor_Click);
//
// menuItemHelp
//
this.menuItemHelp.Index = 4;
this.menuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemAbout});
resources.ApplyResources(this.menuItemHelp, "menuItemHelp");
//
// menuItemAbout
//
this.menuItemAbout.Index = 0;
this.menuItemAbout.MdiList = true;
resources.ApplyResources(this.menuItemAbout, "menuItemAbout");
this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
//
// saveFileDialog1
//
resources.ApplyResources(this.saveFileDialog1, "saveFileDialog1");
this.saveFileDialog1.InitialDirectory = "%USERPROFILE%";
this.saveFileDialog1.RestoreDirectory = true;
//
// fontDialog1
//
this.fontDialog1.FontMustExist = true;
this.fontDialog1.ShowColor = true;
//
// colorDialog1
//
this.colorDialog1.AnyColor = true;
//
// textBox
//
this.textBox.AcceptsReturn = true;
resources.ApplyResources(this.textBox, "textBox");
this.textBox.Name = "textBox";
this.textBox.TextChanged += new System.EventHandler(this.textBox_TextChanged);
//
// openFileDialog1
//
resources.ApplyResources(this.openFileDialog1, "openFileDialog1");
this.openFileDialog1.RestoreDirectory = true;
//
// NotePad
//
resources.ApplyResources(this, "$this");
this.Controls.Add(this.textBox);
this.Menu = this.mainMenu;
this.Name = "NotePad";
this.Closing += new System.ComponentModel.CancelEventHandler(this.NotePad_Closing);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
if (SkinStart(".\\skin\\spring.urf",4,"",1,0,0)==0)
{
MessageBox.Show("找不到皮肤 -- \"春天\" ");
SkinStart(".\\skin\\belv.urf",4,"",1,0,0);
}
Application.Run(new NotePad());
SkinRemove();
}
//新建
private void menuItemNew_Click(object sender, System.EventArgs e)
{
if (needtosave==true)
{
DialogResult result = MessageBox.Show(CurrentFileName+" 的内容已经改变,需要保存吗?","保存文件",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
switch(result)
{
case DialogResult.OK:
{
menuItemSave_Click(sender,e);
textBox.Clear();
needtosave=false;
break;
}
case DialogResult.No:
{
textBox.Clear();
this.Text = "文本编辑--新建文本";
needtosave=false;
break;
}
case DialogResult.Cancel:
{
break;
}
}
}
else
{
textBox.Clear();
this.Text = "文本编辑--新建文本";
needtosave=false;
}
}
private void menuItemExit_Click(object sender, System.EventArgs e)
{
Close();
}
private void menuItemHelp_Click(object sender, System.EventArgs e)
{
}
private void menuItemOpen_Click(object sender, System.EventArgs e)
{
try
{
if (needtosave==true)
{
DialogResult result = MessageBox.Show(CurrentFileName+" 的内容已经改变,需要保存吗?","保存文件",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
if (result==DialogResult.Cancel)
{
return;
}
if (result==DialogResult.OK)
{
menuItemSave_Click(sender,e);
needtosave=false;
}
}
openFileDialog1.InitialDirectory = "%USERPROFILE%";
if(openFileDialog1.ShowDialog() == DialogResult.OK )
{
StreamReader sr = new StreamReader(openFileDialog1.FileName,Encoding.Default);
textBox.Text = sr.ReadToEnd();
sr.Close();
CurrentFileName = openFileDialog1.FileName;
this.Text = "文本编辑--"+CurrentFileName;
needtosave=false;
}
}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
private void menuItemSave_Click(object sender, System.EventArgs e)
{
try
{
saveFileDialog1.Title = "保存";
if(saveFileDialog1.ShowDialog() == DialogResult.OK )
{
if (saveFileDialog1.FileName != null)
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.Write(textBox.Text);
sw.Close();
}
}
}
catch(Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
private void menuItemSaveAs_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Title = "另存为";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter asw = new StreamWriter(saveFileDialog1.FileName,true);
asw.Write(textBox.Text);
asw.Close();
}
}
private void menuItemUndo_Click(object sender, System.EventArgs e)
{
if (textBox.CanUndo == true)
{
textBox.Undo();
textBox.ClearUndo();
}
}
private void menuItemCut_Click(object sender, System.EventArgs e)
{
if (textBox.SelectedText != "")
{
textBox.Cut();
}
}
private void menuItemCopy_Click(object sender, System.EventArgs e)
{
textBox.Copy();
}
private void menuItemPaste_Click(object sender, System.EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
textBox.Paste();
}
}
private void menuItemDel_Click(object sender, System.EventArgs e)
{
if (textBox.SelectedText != "")
{
textBox.SelectedText = "";
}
}
private void menuItemAll_Click(object sender, System.EventArgs e)
{
textBox.SelectAll();
}
private void menuItemAbout_Click(object sender, System.EventArgs e)
{
About aboutform = new About();
aboutform.ShowDialog();
}
bool flag;
private void menuItemReturn_Click(object sender, System.EventArgs e)
{
switch (flag)
{
case true :
flag = false;
menuItemReturn.Checked = false;
textBox.WordWrap = false;
break;
case false:
flag = true;
menuItemReturn.Checked = true;
textBox.WordWrap = true;
break;
}
}
private void menuItemFont_Click(object sender, System.EventArgs e)
{
if(fontDialog1.ShowDialog() == DialogResult.OK )
{
textBox.Font = fontDialog1.Font;
textBox.ForeColor = fontDialog1.Color;
}
}
private void menuItemBackcolor_Click(object sender, System.EventArgs e)
{
if(colorDialog1.ShowDialog() == DialogResult.OK )
{
textBox.BackColor = colorDialog1.Color;
}
}
private void menuItemTime_Click(object sender, System.EventArgs e)
{
}
//////////////////////////////////////////////////////////////////////////
///实现动态控制菜单
private void menuItemEdit_Popup(object sender, System.EventArgs e)
{
//根据剪贴板所否有内容 而确定 menueItemPaste所否可用
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
menuItemPaste.Enabled = true;
}
else
{
menuItemPaste.Enabled = false;
}
//决定menuItemCut,menuItemCopy,menuItemDel是否可用
if (textBox.SelectedText.Length>0)
{
menuItemCopy.Enabled= true;
menuItemCut.Enabled = true;
menuItemDel.Enabled = true;
}
else
{
menuItemCopy.Enabled = false;
menuItemCut.Enabled = false;
menuItemDel.Enabled = false;
}
//决定menuItemUndo是否可用
if (textBox.CanUndo == true)
{
menuItemUndo.Enabled = true;
}
else
{
menuItemUndo.Enabled = false;
}
}
private void textBox_TextChanged(object sender, System.EventArgs e)
{
needtosave = true;
}
private void NotePad_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (needtosave==true)
{
DialogResult result = MessageBox.Show(CurrentFileName+" 的内容已经改变。\n想保存文件吗?","保存",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
if(result == DialogResult.Cancel)
{
e.Cancel=true;
return;
}
if (result == DialogResult.Yes)
{
menuItemSave_Click(sender,e);
e.Cancel=false;
return;
}
}
}
private void DoSkin(string SkinFile)
{
SkinStart(".\\skin\\"+SkinFile,4,"",1,0,0);
}
private void CancelCheked()
{
menuItemJingDian.Checked = false;
menuItemChunTian.Checked = false;
menuItemHejin.Checked = false;
menuItemCancelSkin.Checked = false;
}
private void menuItemJingDian_Click(object sender, System.EventArgs e)
{
DoSkin("GtClassic.urf");
CancelCheked();
menuItemJingDian.Checked = true;
}
private void menuItemChunTian_Click(object sender, System.EventArgs e)
{
DoSkin("spring.urf");
CancelCheked();
menuItemChunTian.Checked = true;
}
private void menuItemHejin_Click(object sender, System.EventArgs e)
{
DoSkin("alloy.urf");
CancelCheked();
menuItemHejin.Checked = true;
}
private void menuItemLoadSkin_Click(object sender, System.EventArgs e)
{
openFileDialog1.Filter = "*.urf|*.urf";
openFileDialog1.Title = "打开皮肤";
openFileDialog1.InitialDirectory = ".\\Skin\\";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
SkinStart(openFileDialog1.FileName,4,"",1,0,0);
}
}
private void menuItemCancelSkin_Click(object sender, System.EventArgs e)
{
DoSkin("");
CancelCheked();
menuItemCancelSkin.Checked = true;
}
}
}