📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Example19_1
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
//新建工具栏按钮事件
private void toolStripNew_Click(object sender, EventArgs e)
{
frmEdit frm = new frmEdit();
//使新建的Form2窗体的父窗体为当前窗体
frm.MdiParent = this;
ToolStripMenuItem newWindowItem = new ToolStripMenuItem(frm.Text);
mnuWindows.DropDownItems.Add(newWindowItem);
frm.Show();
}
private void mnuCascade_Click(object sender, EventArgs e)
{
//层叠窗口
this.LayoutMdi(MdiLayout.Cascade);
}
private void mnuVerticle_Click(object sender, EventArgs e)
{
//垂直平铺
this.LayoutMdi(MdiLayout.TileVertical);
}
private void mnuHorizontal_Click(object sender, EventArgs e)
{
//水平平铺
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void toolStripButtonOpen_Click(object sender, EventArgs e)
{
//设置标题
openFileDialog.Title = "我的记事本--打开文件对话框";
//设置目录
string dir = @"C:\";
openFileDialog.InitialDirectory = dir;
//设置过滤器
string filter = "文本文件(*.txt)|*.txt";
openFileDialog.Filter = filter;
//限制多选
openFileDialog.Multiselect = false;
//显示对话框
if (openFileDialog.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
//新建编辑窗体并使其标题为打开的文件名
frmEdit frm = new frmEdit();
frm.Text = openFileDialog.FileName;
//读取文件的内容
StreamReader sr = new StreamReader(openFileDialog.OpenFile());
frm.RichTextBoxText = sr.ReadToEnd();
//显示窗体为MDI子窗体
frm.MdiParent = this;
frm.Show();
}
}
private void toolStripSave(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
{
return;
}
//设置标题
saveFileDialog.Title = "我的记事本--保存文件对话框";
//设置过滤器
string filter = "文本文件(*.txt)|*.txt";
saveFileDialog.Filter = filter;
//设置目录
string dir = @"C:\";
saveFileDialog.InitialDirectory = dir;
if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
//保存文件
Stream stream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(stream);
frmEdit frm = (frmEdit)this.ActiveMdiChild;
sw.Write(frm.RichTextBoxText);
}
}
private void toolStripPrint_Click(object sender, EventArgs e)
{
//没有要打开的文档,返回
if (MdiChildren.Length == 0)
{
return;
}
//点击打印时打印文档
if (printDialog.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
//打印文档方法,PrintDocument.Print
printDocument.Print();
}
}
private void toolStripPrintPreview_Click(object sender, EventArgs e)
{
//没有要打开的文档,返回
if (MdiChildren.Length == 0)
{
return;
}
//设置预览窗口关联的PrintDocument
printPreviewDialog.Document = printDocument;
//显示打印预览窗口
printPreviewDialog.ShowDialog();
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//将要打印的文本输出至PrintDocument
e.Graphics.DrawString(text, new Font("Arial", 10), Brushes.Black, 20, 20);
}
private void printDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
//获得要打印的文本
text = ((frmEdit)ActiveMdiChild).RichTextBoxText;
}
private string text = string.Empty;
private void toolStripFont_Click(object sender, EventArgs e)
{
if (MdiChildren.Length == 0)
{
return;
}
else
{
if (fontDialog.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
}
}
}
private void toolStripColor_Click(object sender, EventArgs e)
{
if (MdiChildren.Length == 0)
{
return;
}
else
{
if (colorDialog.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -