2017年11月27日 C#MDI窗体创建&记事本打印&记事本查找、自动换行
MDI窗体第一个父窗体
把属性里的IsMdiContainer设置为true就可以了
父窗体连接子窗体
//创建一个新的类,用来连接别的窗体,并且别的窗体为唯一窗体
List<Form> F = new List<Form>(); private void opendao(Form f) { Form F1 = null; bool isopen = false; foreach(Form gf in F) { gf.Hide(); if(gf.Name == f.Name) { isopen = true; F1 = gf; } } if (isopen) { f.Close(); F1.Show(); } else { f.MdiParent = this; f.WindowState = FormWindowState.Maximized; f.Parent = panel1; f.FormBorderStyle = FormBorderStyle.None; f.Show(); F.Add(f); } }
第一个按钮的连接或者别的也可以
//连接到第二个窗口 private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Name = "1"; opendao(f2); }
第二个按钮连接或者别的也可以
//连接到第二个窗口 private void button2_Click(object sender, EventArgs e) { Form3 f3 = new Form3(); f3.Name = "2"; opendao(f3); }
注:可以多个窗体连接只显示在父窗体里
记事本页面设置
private void toolStripMenuItem1_Click(object sender, EventArgs e) { pageSetupDialog1.Document = printDocument1; DialogResult dr = pageSetupDialog1.ShowDialog(); if (dr == DialogResult.OK) { } }
记事本打印
//注:第一个代码为绘画,将字符串绘画,重要! private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { string s = textBox1.Text; Font f = new System.Drawing.Font("微软雅黑", 25.5f); Brush b = new SolidBrush(Color.Red); e.Graphics.DrawString(s, f, b, 20, 20); } //打印 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1; DialogResult dr = printDialog1.ShowDialog(); if (dr == DialogResult.OK) { printDocument1.Print(); } }
记事本打印预览
private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e) { printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); }
记事本自动换行
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e) { if (textBox1.WordWrap) { //不自动换行 textBox1.WordWrap = false; textBox1.ScrollBars = ScrollBars.Both; 自动换行ToolStripMenuItem.Checked = false; } else { //自动换行 textBox1.WordWrap = true; textBox1.ScrollBars = ScrollBars.Vertical; 自动换行ToolStripMenuItem.Checked = true; } }
记事本新窗体查找
//此为第二个窗口的设置 Form1 F1; public Form2(Form1 f1) { InitializeComponent(); F1 = f1; } int a = 0; private void button1_Click(object sender, EventArgs e) { string s = textBox1.Text; a = F1.textBox1.Text.IndexOf(s, a + 1); if (a != -1) { F1.textBox1.Select(a, s.Length); F1.textBox1.Focus(); } else { MessageBox.Show("无匹配项!"); } }
//此为第一个窗口使用 private void 查找ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f2 = new Form2(this); f2.Owner = this; f2.Show(); }