1 / 16

对话框控件 & 打印

第 2 章. 对话框控件 & 打印. 教学目标. 理解打印对话框。 掌握 CommonDialog 。 掌握 文件操作相关的对话框。. CommonDialog 抽象类 所有对话框的基类 ShowDialog() 显示对话框 Reset() 重置对话框 Tag 返回对话框数据 DialogResult 对话框返回值. ColorDialog 属性: AllowFullOpen 是否允许使用自定义颜色 SolidColorOnly 是否只能使用纯色 FullOpen 是否直接打开自定义颜色界面

lorant
Download Presentation

对话框控件 & 打印

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第2章 对话框控件 & 打印

  2. 教学目标 • 理解打印对话框。 • 掌握CommonDialog。 • 掌握文件操作相关的对话框。

  3. CommonDialog抽象类 • 所有对话框的基类 • ShowDialog() 显示对话框 • Reset() 重置对话框 • Tag 返回对话框数据 • DialogResult 对话框返回值

  4. ColorDialog • 属性: • AllowFullOpen是否允许使用自定义颜色 • SolidColorOnly 是否只能使用纯色 • FullOpen 是否直接打开自定义颜色界面 • Color 返回用户选择的颜色 • 方法: • ShowDialog 打开对话框 使用RichTextBox和ColorDialog

  5. 设置颜色 • private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) • { • this.colorDialog1.AllowFullOpen = true; • this.colorDialog1.FullOpen = true; • if (this.colorDialog1.ShowDialog() == DialogResult.OK) • { • Color color = this.colorDialog1.Color; • this.BackColor = color; • } • }

  6. FontDialog • 属性: • ShowApply 是否显示应用按钮 • ShowColor 是否显示颜色选择 • Font 对话框当前的字体 • Color 对话框当前的颜色 • 方法: • ShowDialog 打开对话框 使用RichTextBox和FontDialog

  7. 文件对话框-打开,保存 • FileDialog,抽象类 • 打开OpenFileDialog • 保存SaveFileDialog

  8. FileDialog • 属性: • AddExtension 是否自动加上后缀名 • DefaultExt 默认的后缀名 • FileName 被选择的文件(路径) • FileNames 多选时被选择的文件(路径) • Filter 筛选器语法:描述1|后缀1|描述2|后缀2 • InitialDirectory 默认打开的路径 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) • RestoreDirectory 对话框关闭后是否恢复到初时路径 • SupportMultiDottedExtensions 文件名是否支持多个. • ValidateNames 验证文件名是否合法

  9. OpenFileDialog ofd=new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) this.richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);

  10. SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "rtf文件(*.rtf)|*.rtf"; sfd.AddExtension = true; sfd.DefaultExt = "rtf"; if(sfd.ShowDialog()==DialogResult.OK) this.richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.RichText);

  11. 打开文件 • private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) • { • this.openFileDialog1.Filter = "txt files(*.txt)|*.txt|all files(*.*)|*.*"; • this.openFileDialog1.FilterIndex = 1; • this.openFileDialog1.RestoreDirectory = true; • if (this.openFileDialog1.ShowDialog() == DialogResult.OK) • { • string filename = this.openFileDialog1.FileName; • this.richTextBox1.Text = System.IO.File.ReadAllText(filename); • MessageBox.Show("打开文件成功"); • } • }

  12. 保存文件 • private void 保存ToolStripMenuItem1_Click(object sender, EventArgs e) • { • string str=this.richTextBox1.Text; • string []s=str.Split('\n'); • this.saveFileDialog1.AddExtension = true; • this.saveFileDialog1.DefaultExt = "doc"; • this.saveFileDialog1.InitialDirectory = "d:\\"; • if (this.saveFileDialog1.ShowDialog() == DialogResult.OK) • { • System.IO.FileStream fs = new System.IO.FileStream(this.saveFileDialog1.FileName, System.IO.FileMode.Append); • System.IO.StreamWriter sw = new System.IO.StreamWriter(fs); • for (int i = 0; i < s.Length;i++ ) • sw.WriteLine(s[i]); • sw.Close(); • MessageBox.Show("文件保存成功"); • } • }

  13. FolderBrowserDialog • 属性: • RootFolder 打开时的根路径 • SelectedPath 选择的路径 • ShowNewFolderButton 是否显示“新建文件夹”

  14. 打印 • PrintDocument • PageSetupDialog • PrintDialog • PrintPreviewDialog

  15. PrintDocument对象用以打印 • 步骤: • 实例化PrintDocument对象 • 实现该对象的PrintPage事件,在事件处理代码中使用Graphics对象逐行打印 • 直接调用Print方法开始打印 02\PrintDemo

  16. 总结 • ColorDialog • FontDialog • OpenFileDialog • SaveFileDialog • FolderBrowserDialog • PrintDocument • PageSetupDialog • PrintDialog • PrintPreviewDialog

More Related