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