public partial class Form1 : Form
{
public int num=0;
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
string nums;
num++;
nums=num.ToString();
Form2 frm = new Form2();
frm.MdiParent=this;//设置form1为form2的父窗口;
ToolStripMenuItem newWindowItem = new ToolStripMenuItem(frm.Text +nums+".txt");
窗口HToolStripMenuItem.DropDownItems.Add(newWindowItem);
frm.Show();
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
string nums;
num++;
nums = num.ToString();
Form2 frm = new Form2();
ToolStripMenuItem newWindowItem = new ToolStripMenuItem(frm.Text + nums + ".txt");
窗口HToolStripMenuItem.DropDownItems.Add(newWindowItem);
frm.MdiParent = this;
frm.Show();
}
private void 帮助主题ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.Cascade);
}
private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileVertical);
}
private void 水平平铺IToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void toolStripButton2_Click(object sender, EventArgs e)//打开文件对话框
{
openFileDialog1.Title = "我的记事本---打开文本对话框";
string dir = @"C:/";//设置目录
openFileDialog1.InitialDirectory = dir;
string filter = "文本文件(*.txt)|*.txt";//设置过虑器
openFileDialog1.Filter = filter;
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
Form2 frm = new Form2();
frm.Text = openFileDialog1.FileName;
StreamReader sr = new StreamReader(openFileDialog1.OpenFile());
frm.richTextBox1.Text = sr.ReadToEnd();
frm.MdiParent = this;
frm.Show();
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
{
return;
}
saveFileDialog1.Title = "我的记事本---打开保存对话框";
string filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Filter = filter;
string dir = @"C:/";
saveFileDialog1.InitialDirectory = dir;
if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
{
return;
}
else//存在问题:写不进文件内容
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
Form2 frm = (Form2)ActiveMdiChild;
sw.Write(frm.richTextBox1.Text);
sw.Close();
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "我的记事本---打开文本对话框";
string dir = @"C:/";//设置目录
openFileDialog1.InitialDirectory = dir;
string filter = "文本文件(*.txt)|*.txt";//设置过虑器
openFileDialog1.Filter = filter;
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
Form2 frm = new Form2();
frm.Text = openFileDialog1.FileName;
StreamReader sr = new StreamReader(openFileDialog1.OpenFile());
frm.richTextBox1.Text = sr.ReadToEnd();
frm.MdiParent = this;
frm.Show();
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
{
return;
}
saveFileDialog1.Title = "我的记事本---打开保存对话框";
string filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Filter = filter;
string dir = @"C:/";
saveFileDialog1.InitialDirectory = dir;
if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
{
return;
}
else
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
Form2 frm = (Form2)ActiveMdiChild;
sw.Write(frm.richTextBox1.Text);
sw.Close();
}
}
private string text = string.Empty;
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
text = ((Form2)ActiveMdiChild).richTextBox1.Text;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(text, new Font("Arial", 10), Brushes.Black, 20, 20);
}
private void toolStripButton4_Click(object sender, EventArgs e)//打印按钮
{
if (MdiChildren.Length == 0)
return;
if (printDialog1.ShowDialog() == DialogResult.Cancel)
return;
else
printDocument1.Print();
}
private void toolStripButton5_Click(object sender, EventArgs e)//打印预览按钮
{
if (MdiChildren.Length == 0)
{
return;
}
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void toolStripButton10_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
}
private void toolStripButton11_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
}
}