📄 form1.cs
字号:
#region 编辑菜单
//实现动态控制菜单
private void menuItemEdit_popup(object sender, System.EventArgs e)
{
//根据剪贴板上是否有TEXT的类型数据,决定menuItemPaste是否可用
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
menuItemPaste.Enabled = true;
}
else
{
menuItemPaste.Enabled = false;
}
//决定menuItemCut,menuItemCopy,menuItemDel是否可用
if (textBoxEdit.SelectedText.Length > 0)
{
menuItemCopy.Enabled= true;
menuItemCut.Enabled = true;
menuItemDel.Enabled = true;
}
else
{
menuItemCopy.Enabled= false;
menuItemCut.Enabled = false;
menuItemDel.Enabled = false;
}
//决定menuItemUndo是否可用
if (textBoxEdit.CanUndo ==true)
{
menuItemUndo.Enabled = true;
}
else
{
menuItemUndo.Enabled = false;
}
}
//撤消
private void menuItemUndo_Click(object sender, System.EventArgs e)
{
if (textBoxEdit.CanUndo == true)
{
textBoxEdit.Undo();
textBoxEdit.ClearUndo();
}
}
//剪切
private void menuItemCut_Click(object sender, System.EventArgs e)
{
if (textBoxEdit.SelectedText != "")
{
textBoxEdit.Cut();
}
}
//复制
private void menuItemCopy_Click(object sender, System.EventArgs e)
{
textBoxEdit.Copy();
}
//粘贴
private void menuItemPaste_Click(object sender, System.EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) ==true)
{
if (textBoxEdit.SelectionLength > 0)
{
DialogResult result;
result = MessageBox.Show("你想覆盖掉选择的文本吗?","覆盖确认",MessageBoxButtons.YesNo);
if(result ==DialogResult.No)
{
textBoxEdit.SelectionStart = textBoxEdit.SelectionStart + textBoxEdit.SelectionLength;
}
}
textBoxEdit.Paste();
}
}
//删除
private void menuItemDel_Click(object sender, System.EventArgs e)
{
if (textBoxEdit.SelectedText != "")
{
textBoxEdit.SelectedText = "";
}
}
//全选
private void menuItemAll_Click(object sender, System.EventArgs e)
{
textBoxEdit.SelectAll();
}
#endregion
#region 文件菜单
//新建菜单
private void menuItemNew_Click(object sender, System.EventArgs e)
{
if (needToSave == true)
{
DialogResult result = MessageBox.Show("文本文件内容已经改变,需要保存吗?"
,"保存文件",MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
switch (result)
{
case DialogResult.Yes:
{
menuItemSave_Click(sender,e);
textBoxEdit.Clear();
this.Text = "文本编辑--新建文本";
needToSave = false;
break;
}
case DialogResult.No:
{
textBoxEdit.Clear();
this.Text = "文本编辑--新建文本";
needToSave = false;
break;
}
case DialogResult.Cancel:
{
break;
}
}
}
else
{
textBoxEdit.Clear();
this.Text ="文本编辑--新建文本";
}
}
//打开
private void menuItemoOpen_Click(object sender, System.EventArgs e)
{
if (needToSave == true)
{
DialogResult result = MessageBox.Show("文本内容已经改变,需要保存吗?","保存文件",
MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
if (result == DialogResult.Cancel)
{
return;
}
if (result == DialogResult.Yes)
{
menuItemSave_Click (sender,e);
needToSave = false;
}
string file = GetOpenFile();
if(file == null)
{
return;
}
else
{
currentFileName = file;
OpenFile();
needToSave = false;
}
}
}
//打开指定文件
private void OpenFile()
{
try
{
FileInfo f = new FileInfo(currentFileName);
StreamReader reader = f.OpenText();
textBoxEdit.Text = reader.ReadToEnd();
reader.Close();
this.Text = "文本编辑--" + f.Name;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
//获取要打开的文件名
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 menuItemSave_Click(object sender, System.EventArgs e)
{
if(currentFileName == null)
{
menuItemSaveAs_Click (sender,e);
}
else
{
SaveFile(textBoxEdit.Text);
}
needToSave = false;
}
//另存为
private void menuItemSaveAs_Click(object sender, System.EventArgs e)
{
string file = GetSaveFile();
if(file == null)
{
return;
}
else
{
currentFileName = file;
SaveFile(textBoxEdit.Text);
FileInfo f = new FileInfo(currentFileName);
this.Text = "文本编辑--" + f.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 saveFile = new SaveFileDialog();
saveFile.Title = "保存文本文件";
saveFile.OverwritePrompt = true;
saveFile.CreatePrompt = true;
saveFile.AddExtension = true;
saveFile.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|(*.*)";
if(saveFile.ShowDialog() == DialogResult.OK)
{
return saveFile.FileName;
}
else
{
return null;
}
}
//退出
private void menuItemExit_Click(object sender, System.EventArgs e)
{
//关闭窗口
DialogResult diaRes;
if (textBoxEdit.Text == "")
{
this.Close();
return;
}
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1;
MessageBoxIcon icon = MessageBoxIcon.Question;
MessageBoxOptions options = MessageBoxOptions.RightAlign;
string ask = "将要退出程序,是否继续";
string caption = "退出程序";
diaRes = MessageBox.Show(this,ask,caption,buttons,icon,defaultButton,options);
if(diaRes == DialogResult.Yes)
{
this.Close();
}
}
//打印清单
private void printDocument_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;
StreamReader streamToPrint = new StreamReader(currentFileName);
SolidBrush brush = new SolidBrush(textBoxEdit.ForeColor);
linesPerPage = e.MarginBounds.Height / textBoxEdit.Font.GetHeight(e.Graphics);
while (count < linesPerPage && ((line =streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * textBoxEdit.Font.GetHeight(e.Graphics));
e.Graphics.DrawString(line,textBoxEdit.Font,brush,leftMargin,yPos, new StringFormat());
count++;
}
if (line != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
//打印
private void menuItemprint_Click(object sender, System.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 menuItemPageSet_Click(object sender, System.EventArgs e)
{
PageSetupDialog pageSet = new PageSetupDialog();
pageSet.Document = printDocument;
pageSet.ShowDialog();
}
#endregion
#region 格式菜单
//改变字体
private void menuItemFont_Click(object sender, System.EventArgs e)
{
FontDialog fontDialog = new FontDialog();
fontDialog.ShowColor = true;
fontDialog.AllowScriptChange = true;
fontDialog.AllowVectorFonts = true;
fontDialog.ShowEffects = true;
if (fontDialog.ShowDialog() == DialogResult.OK)
{
textBoxEdit.Font = fontDialog.Font;
textBoxEdit.ForeColor = fontDialog.Color;
}
}
//改变背景颜色
private void menuItemColor_Click(object sender, System.EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.AllowFullOpen = true;
colorDialog.AnyColor = true;
colorDialog.FullOpen = true;
if (colorDialog.ShowDialog() == DialogResult.OK)
{
textBoxEdit.BackColor = colorDialog.Color;
}
}
#endregion
// 关于
private void menuItemAbout_Click(object sender, System.EventArgs e)
{
AboutForm aboutForm = new AboutForm();
aboutForm.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -