winform —— 对话框和流及打印
对话框: 注意引用using System.IO;
showdialog();显示对话框,返回一个dialogresult的枚举类型
colorDialog:color属性,用来获取颜色
folderBrowserDialog:SelectedPath选中路径
fontDialog:font属性,返回一个font类型的值,里面存储了关于字体的设置
openFileDialog:
filename获取或设置文件路径包含文件名
filenames 是文件路径字符串数组
filter:文件筛选器 格式为 提示文本一|*.后缀|提示文本二|*.后缀|提示文本三|*.后缀
saveFileDialog1:
filename获取或设置文件路径包含文件名
filenames 是文件路径字符串数组
filter:文件筛选器 格式为 提示文本一|*.后缀|提示文本二|*.后缀|提示文本三|*.后缀
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 11 namespace WindowsFormsApplication6 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 //颜色 20 private void button1_Click(object sender, EventArgs e) 21 { 22 DialogResult dr = colorDialog1.ShowDialog(); 23 if (dr == DialogResult.OK) 24 { 25 this.BackColor = colorDialog1.Color; 26 } 27 } 28 //文件夹浏览器 29 private void button2_Click(object sender, EventArgs e) 30 { 31 DialogResult dr = folderBrowserDialog1.ShowDialog(); 32 if (dr == DialogResult.OK) 33 { 34 MessageBox.Show(folderBrowserDialog1.SelectedPath); 35 } 36 else 37 { 38 MessageBox.Show(folderBrowserDialog1.SelectedPath); 39 } 40 } 41 //字体 42 private void button3_Click(object sender, EventArgs e) 43 { 44 fontDialog1.ShowDialog(); 45 MessageBox.Show(fontDialog1.Font.Size.ToString()); 46 } 47 //打开 48 private string Files; 49 private void button4_Click(object sender, EventArgs e) 50 { 51 DialogResult dr = openFileDialog1.ShowDialog(); 52 if (DialogResult.OK == dr) 53 { 54 string filename = openFileDialog1.FileName; 55 StreamReader sr = new StreamReader(filename); 56 textBox1.Text = sr.ReadToEnd(); 57 sr.Close(); 58 59 Files = filename; 60 } 61 } 62 //保存 63 private void button5_Click(object sender, EventArgs e) 64 { 65 if (Files == null) 66 { 67 saveFileDialog1.Filter = "文本 |*.txt|word|*.doc|excel|*.xls"; 68 DialogResult dr = saveFileDialog1.ShowDialog(); 69 if (dr == DialogResult.OK) 70 { 71 string filename = saveFileDialog1.FileName; 72 73 StreamWriter sw = new StreamWriter(filename); 74 sw.Write(textBox1.Text); 75 sw.Close(); 76 } 77 } 78 else 79 { 80 StreamWriter sw = new StreamWriter(Files); 81 sw.Write(textBox1.Text); 82 sw.Close(); 83 } 84 } 85 86 private void Form1_Load(object sender, EventArgs e) 87 { 88 89 } 90 //关闭窗体 91 private void button6_Click(object sender, EventArgs e) 92 { 93 this.Close(); 94 } 95 //页面设置 96 private void button7_Click(object sender, EventArgs e) 97 { 98 pageSetupDialog1.Document = printDocument1; 99 pageSetupDialog1.ShowDialog(); 100 } 101 //打印 102 private void button8_Click(object sender, EventArgs e) 103 { 104 printDialog1.Document = printDocument1; 105 DialogResult dr = printDialog1.ShowDialog(); 106 if (dr == DialogResult.OK) 107 { 108 printDocument1.Print(); 109 } 110 } 111 112 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 113 { 114 System.Drawing.Font f = new System.Drawing.Font("宋体",12); 115 e.Graphics.DrawString(textBox1.Text,f,System.Drawing.Brushes.Aqua,5,5); 116 } 117 } 118 }
流:
输入流:
string filename = openFileDialog1.FileName;
//通过读入流进行文件读取
StreamReader sr = new StreamReader(filename);
textBox1.Text = sr.ReadToEnd();
sr.Close();
输出流:
string filename = saveFileDialog1.FileName;
//写入流,可以在硬盘上创建文件,并为文件写入信息
StreamWriter sw = new StreamWriter(filename);
sw.Write(this.textBox1.Text);
sw.Close();
this:代表的它所在的那个类当前对象
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.IO; 10 11 namespace FirstForm 12 { 13 public partial class JiShiben : Form 14 { 15 public JiShiben() 16 { 17 InitializeComponent(); 18 } 19 20 private void textBox1_TextChanged(object sender, EventArgs e) 21 { 22 //MessageBox.Show("你好"); 23 if (this.textBox1.Text.Length > 0) 24 { 25 撤销ToolStripMenuItem.Enabled = true; 26 } 27 } 28 29 private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) 30 { 31 textBox1.Copy(); 32 } 33 34 private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) 35 { 36 textBox1.Paste(); 37 } 38 39 private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) 40 { 41 textBox1.Cut(); 42 } 43 44 private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e) 45 { 46 if (textBox1.CanUndo == true) 47 { 48 // Undo the last operation. 49 textBox1.Undo(); 50 // Clear the undo buffer to prevent last action from being redone. 51 textBox1.ClearUndo(); 52 } 53 } 54 55 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) 56 { 57 textBox1.SelectedText = ""; 58 } 59 60 private void 全选ToolStripMenuItem_Click(object sender, EventArgs e) 61 { 62 textBox1.SelectAll(); 63 } 64 65 private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) 66 { 67 DialogResult dr = openFileDialog1.ShowDialog(); 68 if (dr == DialogResult.OK) 69 { 70 string filename = openFileDialog1.FileName; 71 //通过读入流进行文件读取 72 StreamReader sr = new StreamReader(filename); 73 textBox1.Text = sr.ReadToEnd(); 74 sr.Close(); 75 } 76 } 77 78 private void 新建ToolStripMenuItem_Click(object sender, EventArgs e) 79 { 80 if (this.textBox1.Text.Length > 0) 81 { 82 DialogResult drg= MessageBox.Show("是否进行保存?","保存对话框",MessageBoxButtons.YesNo); 83 if (DialogResult.Yes == drg) 84 { 85 if (FileName == null) 86 { 87 DialogResult dr = saveFileDialog1.ShowDialog(); 88 if (dr == DialogResult.OK) 89 { 90 string filename = saveFileDialog1.FileName; 91 //写入流,可以在硬盘上创建文件,并为文件写入信息 92 StreamWriter sw = new StreamWriter(filename); 93 sw.Write(this.textBox1.Text); 94 sw.Close(); 95 } 96 } 97 else 98 { 99 //写入流,可以在硬盘上创建文件,并为文件写入信息 100 StreamWriter sw = new StreamWriter(FileName); 101 sw.Write(this.textBox1.Text); 102 sw.Close(); 103 } 104 } 105 } 106 107 FileName = null; 108 this.textBox1.Text = ""; 109 } 110 private string FileName; 111 private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) 112 { 113 if (FileName == null) 114 { 115 DialogResult dr = saveFileDialog1.ShowDialog(); 116 if (dr == DialogResult.OK) 117 { 118 string filename = saveFileDialog1.FileName; 119 //写入流,可以在硬盘上创建文件,并为文件写入信息 120 StreamWriter sw = new StreamWriter(filename); 121 sw.Write(this.textBox1.Text); 122 sw.Close(); 123 } 124 } 125 else 126 { 127 //写入流,可以在硬盘上创建文件,并为文件写入信息 128 StreamWriter sw = new StreamWriter(FileName); 129 sw.Write(this.textBox1.Text); 130 sw.Close(); 131 } 132 } 133 134 private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e) 135 { 136 saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|word文件(*.doc)|*.doc"; 137 DialogResult dr = saveFileDialog1.ShowDialog(); 138 if (dr == DialogResult.OK) 139 { 140 string filename = saveFileDialog1.FileName; 141 //写入流,可以在硬盘上创建文件,并为文件写入信息 142 StreamWriter sw = new StreamWriter(filename); 143 sw.Write(this.textBox1.Text); 144 sw.Close(); 145 } 146 } 147 148 private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e) 149 { 150 pageSetupDialog1.Document = printDocument1;//为页面设置对话框指定打印对象 151 pageSetupDialog1.ShowDialog();//打开页面对话框 152 } 153 154 private void 打印ToolStripMenuItem_Click(object sender, EventArgs e) 155 { 156 DialogResult dr = printDialog1.ShowDialog(); 157 if (dr == DialogResult.OK) 158 { 159 printDocument1.Print(); 160 } 161 } 162 163 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 164 { 165 //设置打印的画板内容 166 System.Drawing.Font f = new System.Drawing.Font("宋体", 12); 167 e.Graphics.DrawString(this.textBox1.Text, f, SystemBrushes.ActiveBorder, 10.0f, 0f); 168 } 169 170 private void 查找ToolStripMenuItem_Click(object sender, EventArgs e) 171 { 172 //Find ff = new Find(this.textBox1.SelectedText,this); 173 //ff.Owner = this; 174 //ff.Show(); 175 } 176 177 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 178 { 179 this.Close(); 180 } 181 182 private void JiShiben_Load(object sender, EventArgs e) 183 { 184 185 } 186 } 187 }
打印:
打印对话框:printdialog
页面设置:pagesetupdialog
这两个对话框都需要通过设置printdocument来指定打印对象
printdocument:打印对象,必须要有,一块画板,用于打印机与打印内容之间中转,打印机打印的是printdoment
printDocument1_PrintPage:事件,每打印一页之前触发,用于给printdocument指定打印内容
通过画板把内容画到打印对象的页上:
System.Drawing.Font f = new System.Drawing.Font("宋体",12);
e.Graphics.DrawString(textBox1.Text,f,System.Drawing.Brushes.Aqua,5,5);
最后打印: 打印对话框那,如果打印对话框返回确定打印,就执行printdocument.print();