Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 7k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Drawing.Printing;
  8. using System.IO;
  9. namespace ch3_6
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.RichTextBox richTextBox1;
  17. private System.Windows.Forms.MainMenu mainMenu1;
  18. private System.Windows.Forms.MenuItem menuItem1;
  19. private System.Windows.Forms.MenuItem menuItem2;
  20. private System.Windows.Forms.MenuItem menuItem3;
  21. private System.Windows.Forms.MenuItem menuItem4;
  22. private StreamReader sReader;
  23. private System.Drawing.Printing.PrintDocument pDoc;
  24. private System.Windows.Forms.MenuItem menuItem5;
  25. /// <summary>
  26. /// 必需的设计器变量。
  27. /// </summary>
  28. private System.ComponentModel.Container components = null;
  29. public Form1()
  30. {
  31. //
  32. // Windows 窗体设计器支持所必需的
  33. //
  34. InitializeComponent();
  35.             
  36. sReader=new StreamReader("toprint.txt",System.Text.Encoding.Default);
  37. this.richTextBox1.Text=sReader.ReadToEnd();
  38. //
  39. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  40. //
  41. }
  42. /// <summary>
  43. /// 清理所有正在使用的资源。
  44. /// </summary>
  45. protected override void Dispose( bool disposing )
  46. {
  47. if( disposing )
  48. {
  49. if (components != null) 
  50. {
  51. components.Dispose();
  52. }
  53. }
  54. base.Dispose( disposing );
  55. }
  56. #region Windows Form Designer generated code
  57. /// <summary>
  58. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  59. /// 此方法的内容。
  60. /// </summary>
  61. private void InitializeComponent()
  62. {
  63. this.pDoc = new System.Drawing.Printing.PrintDocument();
  64. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  65. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  66. this.menuItem1 = new System.Windows.Forms.MenuItem();
  67. this.menuItem2 = new System.Windows.Forms.MenuItem();
  68. this.menuItem3 = new System.Windows.Forms.MenuItem();
  69. this.menuItem4 = new System.Windows.Forms.MenuItem();
  70. this.menuItem5 = new System.Windows.Forms.MenuItem();
  71. this.SuspendLayout();
  72. // 
  73. // pDoc
  74. // 
  75. this.pDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pDoc_PrintPage);
  76. // 
  77. // richTextBox1
  78. // 
  79. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  80. this.richTextBox1.Name = "richTextBox1";
  81. this.richTextBox1.Size = new System.Drawing.Size(292, 273);
  82. this.richTextBox1.TabIndex = 0;
  83. this.richTextBox1.Text = "";
  84. // 
  85. // mainMenu1
  86. // 
  87. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  88.   this.menuItem1});
  89. // 
  90. // menuItem1
  91. // 
  92. this.menuItem1.Index = 0;
  93. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  94.   this.menuItem2,
  95.   this.menuItem3,
  96.   this.menuItem4,
  97.   this.menuItem5});
  98. this.menuItem1.Text = "文件(&F)";
  99. // 
  100. // menuItem2
  101. // 
  102. this.menuItem2.Index = 0;
  103. this.menuItem2.Text = "打印(&P)...";
  104. this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
  105. // 
  106. // menuItem3
  107. // 
  108. this.menuItem3.Index = 1;
  109. this.menuItem3.Text = "打印预览(&V)...";
  110. this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
  111. // 
  112. // menuItem4
  113. // 
  114. this.menuItem4.Index = 2;
  115. this.menuItem4.Text = "页面设置(&U)...";
  116. this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
  117. // 
  118. // menuItem5
  119. // 
  120. this.menuItem5.Index = 3;
  121. this.menuItem5.Text = "打印设置(&S)...";
  122. this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
  123. // 
  124. // Form1
  125. // 
  126. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  127. this.ClientSize = new System.Drawing.Size(292, 273);
  128. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  129.   this.richTextBox1});
  130. this.Menu = this.mainMenu1;
  131. this.Name = "Form1";
  132. this.Text = "综合打印实例";
  133. this.ResumeLayout(false);
  134. }
  135. #endregion
  136. /// <summary>
  137. /// 应用程序的主入口点。
  138. /// </summary>
  139. [STAThread]
  140. static void Main() 
  141. {
  142. Application.Run(new Form1());
  143. }
  144. private void menuItem2_Click(object sender, System.EventArgs e)
  145. {
  146. //构造打印对话框
  147. PrintDialog printDialog = new PrintDialog();
  148. //设置Document属性
  149. printDialog.Document = this.pDoc;
  150. if (printDialog.ShowDialog()==DialogResult.OK)
  151. {
  152. try
  153. {   
  154. pDoc.Print();
  155. }
  156. catch(Exception excep)
  157. {
  158. //显示打印出错消息
  159. MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK,MessageBoxIcon.Error);
  160. }
  161. }
  162. }
  163. private void menuItem3_Click(object sender, System.EventArgs e)
  164. {
  165.       
  166. //构造打印预览对话框 
  167.     PrintPreviewDialog printPreviewDialog1=new PrintPreviewDialog();
  168.             //设置Document属性
  169.             printPreviewDialog1.Document=this.pDoc;
  170. //显示打印预览窗口
  171. try
  172. {
  173. printPreviewDialog1.ShowDialog();
  174. }
  175. catch(Exception excep)
  176. {
  177. //显示打印出错消息
  178. MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK,MessageBoxIcon.Error);
  179. }
  180. }
  181. private void menuItem4_Click(object sender, System.EventArgs e)
  182. {
  183. //构造页面设置对话框
  184. PageSetupDialog   pageSetupDialog1=new PageSetupDialog();
  185. //设置Document属性
  186. pageSetupDialog1.Document=this.pDoc;
  187.             //显示对话框
  188. pageSetupDialog1.ShowDialog();
  189. }
  190. private void pDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  191. {
  192. //获得绘制对象
  193. Graphics g=e.Graphics;
  194. //一页中的行数
  195. float linePage = 0 ;
  196. //待绘文本的纵向坐标
  197. float yPosition =  0 ;
  198. //行计数
  199. int count = 0 ;
  200. //左边界
  201. float leftMargin = e.MarginBounds.Left;
  202. //顶边界
  203. float topMargin = e.MarginBounds.Top;
  204. //字符串流
  205. String line=null;
  206. //根据页面的高度和字体的高度计算
  207. //一页中可以打印的行数
  208. linePage = e.MarginBounds.Height  / this.Font.GetHeight(g) ;
  209.             
  210. //每次从字符串流中读取一行并打印
  211. while (count < linePage && ((line=sReader.ReadLine()) != null)) 
  212. {
  213. //计算这一行的显示位置
  214. yPosition = topMargin + (count * this.Font.GetHeight(g));
  215. //绘制文本
  216. g.DrawString (line, this.Font, Brushes.Black, leftMargin, yPosition, new StringFormat());
  217. //行数增加
  218. count++;
  219. }
  220. }
  221. private void menuItem5_Click(object sender, System.EventArgs e)
  222. {
  223. //构造打印对话框
  224. PrintDialog printDialog = new PrintDialog();
  225. //设置Document属性
  226. printDialog.Document = this.pDoc;
  227. //显示对话框
  228. printDialog.ShowDialog();
  229. }
  230. }
  231. }