Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 6k
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_5
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private System.Windows.Forms.ToolBar toolBar1;
  17. private System.Windows.Forms.RichTextBox richTextBox1;
  18. private System.ComponentModel.IContainer components;
  19. private System.Windows.Forms.ToolBarButton toolBarButton1;
  20. private System.Windows.Forms.ImageList imageList1;
  21. private System.Drawing.Printing.PrintDocument pDoc;
  22.         //表示待打印流
  23. private StreamReader sReader;
  24. public Form1()
  25. {
  26. //
  27. // Windows 窗体设计器支持所必需的
  28. //
  29. InitializeComponent();
  30.         
  31. //初始化打印流
  32. sReader=new StreamReader("toprint.txt",System.Text.Encoding.Default);
  33. //读取文本的全部内容给RichTextBox
  34. this.richTextBox1.Text=sReader.ReadToEnd();
  35. //
  36. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  37. //
  38. }
  39. /// <summary>
  40. /// 清理所有正在使用的资源。
  41. /// </summary>
  42. protected override void Dispose( bool disposing )
  43. {
  44. if( disposing )
  45. {
  46. if (components != null) 
  47. {
  48. components.Dispose();
  49. }
  50. }
  51. base.Dispose( disposing );
  52. }
  53. #region Windows Form Designer generated code
  54. /// <summary>
  55. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  56. /// 此方法的内容。
  57. /// </summary>
  58. private void InitializeComponent()
  59. {
  60. this.components = new System.ComponentModel.Container();
  61. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  62. this.toolBar1 = new System.Windows.Forms.ToolBar();
  63. this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
  64. this.imageList1 = new System.Windows.Forms.ImageList(this.components);
  65. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  66. this.pDoc = new System.Drawing.Printing.PrintDocument();
  67. this.SuspendLayout();
  68. // 
  69. // toolBar1
  70. // 
  71. this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  72. this.toolBarButton1});
  73. this.toolBar1.DropDownArrows = true;
  74. this.toolBar1.ImageList = this.imageList1;
  75. this.toolBar1.Name = "toolBar1";
  76. this.toolBar1.ShowToolTips = true;
  77. this.toolBar1.Size = new System.Drawing.Size(292, 38);
  78. this.toolBar1.TabIndex = 1;
  79. this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  80. // 
  81. // toolBarButton1
  82. // 
  83. this.toolBarButton1.ImageIndex = 0;
  84. this.toolBarButton1.Text = "打印";
  85. // 
  86. // imageList1
  87. // 
  88. this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
  89. this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
  90. this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
  91. this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
  92. // 
  93. // richTextBox1
  94. // 
  95. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  96. this.richTextBox1.Location = new System.Drawing.Point(0, 38);
  97. this.richTextBox1.Name = "richTextBox1";
  98. this.richTextBox1.Size = new System.Drawing.Size(292, 235);
  99. this.richTextBox1.TabIndex = 2;
  100. this.richTextBox1.Text = "";
  101. // 
  102. // pDoc
  103. // 
  104. this.pDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pDoc_PrintPage);
  105. // 
  106. // Form1
  107. // 
  108. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  109. this.ClientSize = new System.Drawing.Size(292, 273);
  110. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  111.   this.richTextBox1,
  112.   this.toolBar1});
  113. this.Name = "Form1";
  114. this.Text = "文件打印";
  115. this.ResumeLayout(false);
  116. }
  117. #endregion
  118. /// <summary>
  119. /// 应用程序的主入口点。
  120. /// </summary>
  121. [STAThread]
  122. static void Main() 
  123. {
  124. Application.Run(new Form1());
  125. }
  126. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  127. {
  128. if (e.Button==this.toolBarButton1)
  129. {
  130. try 
  131. {
  132. pDoc.Print();
  133. catch(Exception error) 
  134. {
  135. //打印出错处理
  136. MessageBox.Show("打印文件发生错误:   " + error.Message);
  137. }
  138. finally 
  139. {
  140. //关闭字符流
  141. sReader.Close() ;
  142. }
  143. }
  144. }
  145. //每个要打印的页所激发的事件
  146. private void pDoc_PrintPage(object sender, PrintPageEventArgs e) 
  147. {
  148. //获得绘制对象
  149. Graphics g=e.Graphics;
  150.             //一页中的行数
  151. float linePage = 0 ;
  152. //待绘文本的纵向坐标
  153. float yPosition =  0 ;
  154. //行计数
  155. int count = 0 ;
  156. //左边界
  157. float leftMargin = e.MarginBounds.Left;
  158. //顶边界
  159. float topMargin = e.MarginBounds.Top;
  160. //字符串流
  161. String line=null;
  162. //根据页面的高度和字体的高度计算
  163. //一页中可以打印的行数
  164. linePage = e.MarginBounds.Height  / this.Font.GetHeight(g) ;
  165.             
  166. //每次从字符串流中读取一行并打印
  167. while (count < linePage && ((line=sReader.ReadLine()) != null)) 
  168. {
  169. //计算这一行的显示位置
  170. yPosition = topMargin + (count * this.Font.GetHeight(g));
  171.                 //绘制文本
  172. g.DrawString (line, this.Font, Brushes.Black, leftMargin, yPosition, new StringFormat());
  173.                 //行数增加
  174. count++;
  175. }
  176. //如果有多行,则另外打印一页
  177. if (line != null)
  178. e.HasMorePages = true ;
  179. else
  180. e.HasMorePages = false ;
  181. }
  182. }
  183. }