winform 控件-对话框控件、打印控件
对话框
ColorDialog------显示一个对话框,用户可以自定义的颜色
1 private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 DialogResult dr = colorDialog1.ShowDialog(); 4 if (dr == DialogResult.OK) 5 { 6 textBox1.ForeColor = colorDialog1.Color; 7 } 8 }
FolderBrowserDialog------显示一个对话框,提示用户选择文件夹(最小到文件夹,绝对路径)
1 private void 选择文件夹ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 folderBrowserDialog1.ShowDialog(); 4 textBox1.Text = folderBrowserDialog1.SelectedPath; 5 }
FontDialog-----显示一个对话框,选择字体(包括大小、字体、字形,可以添加颜色)
1 private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 fontDialog1.ShowColor = true; // 显示颜色 4 fontDialog1.ShowDialog(); // 打开 5 textBox1.Font = fontDialog1.Font; // 字体改变 6 textBox1.ForeColor = fontDialog1.Color; // 颜色改变 7 }
OpenFileDialog-------显示一个对话框,提示用户打开文件(到文件,用绝对路径,)
-----UnicodeEncoding.GetEncoding("GB2312")---------------编码方式
1 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 openFileDialog1.Filter = "文本文件|*.txt"; //限制显示打开的文件类型 4 DialogResult dr = openFileDialog1.ShowDialog(); 5 6 if (dr == DialogResult.OK) //点击确定 7 { 8 StreamReader sr = new StreamReader(openFileDialog1.FileName,UnicodeEncoding.GetEncoding("GB2312")); 9 textBox1.Text = sr.ReadToEnd(); 10 sr.Close(); //关闭流通道 11 } 12 }
SaveFileDialog------显示一个对话框,提示用户选择文件保存位置
1 string path = ""; 2 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) 3 { 4 if (path == "") // 是否已保存过 5 { 6 saveFileDialog1.FileName = "新建文本文件.txt"; 7 saveFileDialog1.ShowDialog(); 8 path = saveFileDialog1.FileName; 9 } 10 StreamWriter sw = new StreamWriter(path); 11 sw.Write(textBox1.Text); 12 sw.Close(); 13 } 14 15 private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e) 16 { 17 saveFileDialog1.FileName = "新建文本文件.txt"; 18 saveFileDialog1.ShowDialog(); 19 path = saveFileDialog1.FileName; 20 StreamWriter sw = new StreamWriter(path); 21 sw.Write(textBox1.Text); 22 sw.Close(); 23 }
打印
PrintDocument--------定义一个向打印机发送输出的对象---首先定义
-----using System.IO;-------引用
1 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 2 { //打印对象 3 Font f = new Font("宋体", 14); // 字体格式 4 Brush b = new SolidBrush(Color.Black); // 填充的前景色为黑色 5 PointF p = new PointF(10, 10); // 起始打印位置 6 7 e.Graphics.DrawString(textBox1.Text, f, b, p); 8 // e.Graphics.DrawString(要绘制的字符串,字符串的文本格式,绘制文本的颜色和纹理,绘制文本的左上角位置) 9 }
PageSetupDialog---------显示一个对话框,允许用户更改与页面相关的打印设置(包括页边距和纸张方向)---要有对象即PrintDocument
1 private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 pageSetupDialog1.Document = printDocument1; //要打印设置对象是 printDocument1 4 pageSetupDialog1.ShowDialog(); //打开对话框 5 }
PrintPreviewControl----------只表示打印预览中显示正在预览的文档部分,不包含任何对话框或按钮(这是一个控件)--(预览)
1 private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 printPreviewControl1.Document = printDocument1; ------在控件中预览 4 5 }
PrintPreviewDialog----------显示一个对话框,向用户显示关联文档打印时的样子 --- (预览)
1 private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 printPreviewDialog1.Document = printDocument1; //要打印预览对象是 printDocument1 4 printPreviewDialog1.ShowDialog(); 5 }
PrintDiaiog---------显示一个对话框,允许用户选择打印机并选择其他打印设置(如份数和纸张方向)---最后
打印
1 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 printDialog1.Document = printDocument1; //要打印对象是 printDocument1 4 printDialog1.ShowDialog(); 5 }