一对应的From.cs文件
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 NotePad
{
public partial class main_Form : Form
{
private Boolean b_contentChanged = false;
private String str_fileName = String.Empty;
public main_Form()
{
InitializeComponent();
}
//加载过程中将文本置城WordWrap属性
private void main_Form_Load(object sender, EventArgs e)
{
m_formatWordWrapItem_Click(sender, e);
}
//文本变化是,做一些操作时需要提示是否保存
private void txt_content_TextChanged(object sender, EventArgs e)
{
this.b_contentChanged = true;
}
private void m_formatWordWrapItem_Click(object sender, EventArgs e)
{
this.m_formatWordWrapItem.Checked = !(this.m_formatWordWrapItem.Checked);
this.txt_content.WordWrap = this.m_formatWordWrapItem.Checked;
}
//新建文件的时候,先检测文本是否改变,如果改变了,则提示用户是否保存
private void m_fileNewItem_Click(object sender, EventArgs e)
{
checkChangedAndSave();
}
private void checkChangedAndSave()
{
if (this.b_contentChanged)
{
if (DialogResult.Yes == MessageBox.Show(this, "Do You wish to save this Document?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
{
savedata();
}
this.b_contentChanged = false;
}
this.txt_content.Text = "";
this.str_fileName = "";
}
//如果文件名不为空,则直接保存,即如果第一次保存需输入名称,其后不用,除非另存为操作
private void savedata()
{
if (this.str_fileName == "")
{
sfdialog.Filter = "Text Files|*.txt";
if (DialogResult.Cancel == sfdialog.ShowDialog())
{
return;
}
this.str_fileName = sfdialog.FileName;
MessageBox.Show(this.str_fileName);
}
StreamWriter sw = new StreamWriter(this.str_fileName);
sw.WriteLine(this.txt_content.Text);
sw.Flush();
sw.Close();
this.b_contentChanged = false;
}
//打开一个已经存在的文件
private void m_fileOpenItem_Click(object sender, EventArgs e)
{
ofdialog.Multiselect = false;
ofdialog.Filter = "Text Files|*.txt";
ofdialog.ShowDialog();
if (File.Exists(ofdialog.FileName))
{
this.str_fileName = ofdialog.FileName;
StreamReader sr = new StreamReader(this.str_fileName);
this.txt_content.Text = sr.ReadToEnd();
this.b_contentChanged = false;
sr.Close();
}
}
//保存文件
private void m_fileSaveItem_Click(object sender, EventArgs e)
{
savedata();
}
//另存为文件
private void m_fileSaveAsItem_Click(object sender, EventArgs e)
{
sfdialog.Filter = "Text Files|*.txt";
sfdialog.ShowDialog();
this.str_fileName = sfdialog.FileName;
savedata();
}
//打印设置
private void m_filePageSetupItem_Click(object sender, EventArgs e)
{
PrintDocument pd1 = new PrintDocument();
pd1.DocumentName = this.str_fileName;
psdialog.Document = pd1;
psdialog.ShowDialog();
}
//打印
private void m_filePrintItem_Click(object sender, EventArgs e)
{
PrintDocument pd1 = new PrintDocument();
pd1.DocumentName = this.str_fileName;
pdialog.Document = pd1;
pdialog.AllowSomePages = true;
pdialog.AllowPrintToFile = true;
pdialog.ShowDialog();
}
//退出
private void m_fileExitItem_Click(object sender, EventArgs e)
{
checkChangedAndSave();
this.Dispose();
}
//剪切
private void m_editCutItem_Click(object sender, EventArgs e)
{
this.txt_content.Cut();
this.b_contentChanged = true;
}
//拷贝
private void m_editCopyItem_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(this.txt_content.SelectedText, true);
}
//粘贴
private void m_editPasteItem_Click(object sender, EventArgs e)
{
IDataObject iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
this.txt_content.SelectedText = iData.GetData(DataFormats.Text).ToString();
}
}
//设置颜色
private void m_formatFontItem_Click(object sender, EventArgs e)
{
fdialog.ShowColor = true;
fdialog.ShowDialog();
this.txt_content.Font = fdialog.Font;
this.txt_content.ForeColor = fdialog.Color;
}
}
}
二:对应的Designer.cs文件
namespace NotePad
{
partial class main_Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mms_mainMenu = new System.Windows.Forms.MenuStrip();
this.m_topFileMenu = new System.Windows.Forms.ToolStripMenuItem();
this.m_fileNewItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_fileOpenItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_fileSaveItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_fileSaveAsItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.m_filePageSetupItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_filePrintItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_fileExitItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_topEditMenu = new System.Windows.Forms.ToolStripMenuItem();
this.m_editCutItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_editCopyItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_editPasteItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_topFormatMenu = new System.Windows.Forms.ToolStripMenuItem();
this.m_formatWordWrapItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_formatFontItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_helpRegisterItem = new System.Windows.Forms.ToolStripMenuItem();
this.registerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.m_helpAboutItem = new System.Windows.Forms.ToolStripMenuItem();
this.psdialog = new System.Windows.Forms.PageSetupDialog();
this.ofdialog = new System.Windows.Forms.OpenFileDialog();
this.pdialog = new System.Windows.Forms.PrintDialog();
this.sfdialog = new System.Windows.Forms.SaveFileDialog();
this.fdialog = new System.Windows.Forms.FontDialog();
this.txt_content = new System.Windows.Forms.TextBox();
this.mms_mainMenu.SuspendLayout();
this.SuspendLayout();
//
// mms_mainMenu
//
this.mms_mainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.m_topFileMenu,
this.m_topEditMenu,
this.m_topFormatMenu,
this.m_helpRegisterItem});
this.mms_mainMenu.Location = new System.Drawing.Point(0, 0);
this.mms_mainMenu.Name = "mms_mainMenu";
this.mms_mainMenu.Size = new System.Drawing.Size(415, 24);
this.mms_mainMenu.TabIndex = 0;
this.mms_mainMenu.Text = "menuStrip1";
//
// m_topFileMenu
//
this.m_topFileMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.m_fileNewItem,
this.m_fileOpenItem,
this.m_fileSaveItem,
this.m_fileSaveAsItem,
this.toolStripSeparator1,
this.m_filePageSetupItem,
this.m_filePrintItem,
this.m_fileExitItem});
this.m_topFileMenu.Name = "m_topFileMenu";
this.m_topFileMenu.Size = new System.Drawing.Size(41, 20);
this.m_topFileMenu.Text = "File";
//
// m_fileNewItem
//
this.m_fileNewItem.Name = "m_fileNewItem";
this.m_fileNewItem.Size = new System.Drawing.Size(152, 22);
this.m_fileNewItem.Text = "&New";
this.m_fileNewItem.Click += new System.EventHandler(this.m_fileNewItem_Click);
//
// m_fileOpenItem
//
this.m_fileOpenItem.Name = "m_fileOpenItem";
this.m_fileOpenItem.Size = new System.Drawing.Size(152, 22);
this.m_fileOpenItem.Text = "&Open...";
this.m_fileOpenItem.Click += new System.EventHandler(this.m_fileOpenItem_Click);
//
// m_fileSaveItem
//
this.m_fileSaveItem.Name = "m_fileSaveItem";
this.m_fileSaveItem.Size = new System.Drawing.Size(152, 22);
this.m_fileSaveItem.Text = "&Save";
this.m_fileSaveItem.Click += new System.EventHandler(this.m_fileSaveItem_Click);
//
// m_fileSaveAsItem
//
this.m_fileSaveAsItem.Name = "m_fileSaveAsItem";
this.m_fileSaveAsItem.Size = new System.Drawing.Size(152, 22);
this.m_fileSaveAsItem.Text = "Save&As...";
this.m_fileSaveAsItem.Click += new System.EventHandler(this.m_fileSaveAsItem_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(145, 6);
//
// m_filePageSetupItem
//
this.m_filePageSetupItem.Name = "m_filePageSetupItem";
this.m_filePageSetupItem.Size = new System.Drawing.Size(152, 22);
this.m_filePageSetupItem.Text = "Page Set&up...";
this.m_filePageSetupItem.Click += new System.EventHandler(this.m_filePageSetupItem_Click);
//
// m_filePrintItem
//
this.m_filePrintItem.Name = "m_filePrintItem";
this.m_filePrintItem.Size = new System.Drawing.Size(152, 22);
this.m_filePrintItem.Text = "&Print";
this.m_filePrintItem.Click += new System.EventHandler(this.m_filePrintItem_Click);
//
// m_fileExitItem
//
this.m_fileExitItem.Name = "m_fileExitItem";
this.m_fileExitItem.Size = new System.Drawing.Size(152, 22);
this.m_fileExitItem.Text = "E&xit";
this.m_fileExitItem.Click += new System.EventHandler(this.m_fileExitItem_Click);
//
// m_topEditMenu
//
this.m_topEditMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.m_editCutItem,
this.m_editCopyItem,
this.m_editPasteItem});
this.m_topEditMenu.Name = "m_topEditMenu";
this.m_topEditMenu.Size = new System.Drawing.Size(41, 20);
this.m_topEditMenu.Text = "Edit";
//
// m_editCutItem
//
this.m_editCutItem.Name = "m_editCutItem";
this.m_editCutItem.Size = new System.Drawing.Size(152, 22);
this.m_editCutItem.Text = "Cut";
this.m_editCutItem.Click += new System.EventHandler(this.m_editCutItem_Click);
//
// m_editCopyItem
//
this.m_editCopyItem.Name = "m_editCopyItem";
this.m_editCopyItem.Size = new System.Drawing.Size(152, 22);
this.m_editCopyItem.Text = "Copy";
this.m_editCopyItem.Click += new System.EventHandler(this.m_editCopyItem_Click);
//
// m_editPasteItem
//
this.m_editPasteItem.Name = "m_editPasteItem";
this.m_editPasteItem.Size = new System.Drawing.Size(152, 22);
this.m_editPasteItem.Text = "Paste";
this.m_editPasteItem.Click += new System.EventHandler(this.m_editPasteItem_Click);
//
// m_topFormatMenu
//
this.m_topFormatMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.m_formatWordWrapItem,
this.m_formatFontItem});
this.m_topFormatMenu.Name = "m_topFormatMenu";
this.m_topFormatMenu.Size = new System.Drawing.Size(53, 20);
this.m_topFormatMenu.Text = "Format";
//
// m_formatWordWrapItem
//
this.m_formatWordWrapItem.Name = "m_formatWordWrapItem";
this.m_formatWordWrapItem.Size = new System.Drawing.Size(152, 22);
this.m_formatWordWrapItem.Text = "Word Wrap";
this.m_formatWordWrapItem.Click += new System.EventHandler(this.m_formatWordWrapItem_Click);
//
// m_formatFontItem
//
this.m_formatFontItem.Name = "m_formatFontItem";
this.m_formatFontItem.Size = new System.Drawing.Size(152, 22);
this.m_formatFontItem.Text = "Font...";
this.m_formatFontItem.Click += new System.EventHandler(this.m_formatFontItem_Click);
//
// m_helpRegisterItem
//
this.m_helpRegisterItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.registerToolStripMenuItem,
this.m_helpAboutItem});
this.m_helpRegisterItem.Name = "m_helpRegisterItem";
this.m_helpRegisterItem.Size = new System.Drawing.Size(41, 20);
this.m_helpRegisterItem.Text = "Help";
//
// registerToolStripMenuItem
//
this.registerToolStripMenuItem.Name = "registerToolStripMenuItem";
this.registerToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
this.registerToolStripMenuItem.Text = "Register";
//
// m_helpAboutItem
//
this.m_helpAboutItem.Name = "m_helpAboutItem";
this.m_helpAboutItem.Size = new System.Drawing.Size(118, 22);
this.m_helpAboutItem.Text = "About";
//
// pdialog
//
this.pdialog.UseEXDialog = true;
//
// txt_content
//
this.txt_content.Dock = System.Windows.Forms.DockStyle.Fill;
this.txt_content.Location = new System.Drawing.Point(0, 24);
this.txt_content.Multiline = true;
this.txt_content.Name = "txt_content";
this.txt_content.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txt_content.Size = new System.Drawing.Size(415, 332);
this.txt_content.TabIndex = 1;
this.txt_content.TextChanged += new System.EventHandler(this.txt_content_TextChanged);
//
// main_Form
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(415, 356);
this.Controls.Add(this.txt_content);
this.Controls.Add(this.mms_mainMenu);
this.MainMenuStrip = this.mms_mainMenu;
this.Name = "main_Form";
this.Text = "Note Pad in C#";
this.Load += new System.EventHandler(this.main_Form_Load);
this.mms_mainMenu.ResumeLayout(false);
this.mms_mainMenu.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip mms_mainMenu;
private System.Windows.Forms.ToolStripMenuItem m_topFileMenu;
private System.Windows.Forms.ToolStripMenuItem m_fileNewItem;
private System.Windows.Forms.ToolStripMenuItem m_fileOpenItem;
private System.Windows.Forms.ToolStripMenuItem m_fileSaveItem;
private System.Windows.Forms.ToolStripMenuItem m_fileSaveAsItem;
private System.Windows.Forms.ToolStripMenuItem m_topEditMenu;
private System.Windows.Forms.ToolStripMenuItem m_topFormatMenu;
private System.Windows.Forms.ToolStripMenuItem m_helpRegisterItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem m_filePageSetupItem;
private System.Windows.Forms.ToolStripMenuItem m_filePrintItem;
private System.Windows.Forms.ToolStripMenuItem m_fileExitItem;
private System.Windows.Forms.ToolStripMenuItem m_editCutItem;
private System.Windows.Forms.ToolStripMenuItem m_editCopyItem;
private System.Windows.Forms.ToolStripMenuItem m_editPasteItem;
private System.Windows.Forms.ToolStripMenuItem m_formatWordWrapItem;
private System.Windows.Forms.ToolStripMenuItem m_formatFontItem;
private System.Windows.Forms.ToolStripMenuItem registerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem m_helpAboutItem;
private System.Windows.Forms.PageSetupDialog psdialog;
private System.Windows.Forms.OpenFileDialog ofdialog;
private System.Windows.Forms.PrintDialog pdialog;
private System.Windows.Forms.SaveFileDialog sfdialog;
private System.Windows.Forms.FontDialog fdialog;
private System.Windows.Forms.TextBox txt_content;
}
}
简单Notepad代码
最新推荐文章于 2023-03-11 19:08:59 发布