




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#实现打印功能实例详解C#实现打印功能是通过什么来实现的?C#实现打印功能的步骤是什么呢?那么本文就向你介绍这方面的内容。C#实现打印功能是通过使用PrintDialog控件来实现的。任何物有所值的应用程序都会拥有某种打印功能,不管是基本的打印功能还是更为复杂的打印功能,比如允许用户只打印所选的文本或某个范围内的页面。本节将探讨一下实现基本的C#打印功能,看看哪些类有助于打印文件中的文本。C#实现打印功能的过程是:在调用PrintDialog控件的ShowDialog方法之前,必须先设置PrintDialog类的Document属性。该属性接受一个PrintDocument类,以获得打印机设置并将输出内容发送给打印机。PrintDocument类需要System.Drawing.Printing名称空间,因此,在定义使用PrintDocument类的对象前,必须包含这个名称空间。C#实现打印功能具体的操作步骤如下:创建一个PrintDialog的实例。如下:1. System.Windows.Forms.PrintDialogPrintDialog1=newPrintDialog();创建一个PrintDocument的实例.如下:2. System.Drawing.Printing.PrintDocumentdocToPrint= 3. newSystem.Drawing.Printing.PrintDocument();设置打印机开始打印的事件处理函数.函数原形如下:4. voiddocToPrint_PrintPage(objectsender, 5. System.Drawing.Printing.PrintPageEventArgse)将事件处理函数添加到PrintDocument的PrintPage事件中。6. docToPrint.PrintPage+= 7. 8. newPrintPageEventHandler(docToPrint_PrintPage); 设置PrintDocument的相关属性,如:9. PrintDialog1.AllowSomePages= 10. 11. true;PrintDialog1.ShowHelp=true; 把PrintDialog的Document属性设为上面配置好的PrintDocument的实例:12. PrintDialog1.Document=docToPrint;调用PrintDialog的ShowDialog函数显示打印对话框:13. DialogResultresult=PrintDialog1.ShowDialog();根据用户的选择,开始打印:14. if(result=DialogResult.OK) 15. 16. docToPrint.Print(); 17. C#实现打印功能的实例如下:使用时先创建PrintService类的实例,然后调用void StartPrint(Stream streamToPrint,string streamType)函数开始打印。其中streamToPrint是要打印的内容(字节流),streamType是流的类型(txt表示普通文本,image表示图像);18. usingSystem; 19. usingSystem.Drawing.Printing; 20. usingSystem.Windows.Forms; 21. usingSystem.IO; 22. 23. namespaceEDImageSystem 24. 25. / 26. /PrintService的摘要说明。 27. / 28. publicclassPrintService 29. 30. publicPrintService() 31. 32. / 33. /TODO:在此处添加构造函数逻辑 34. / 35. this.docToPrint.PrintPage+= 36. newPrintPageEventHandler(docToPrint_PrintPage); 37. /将事件处理函数添加到PrintDocument的PrintPage中 38. 39. /DeclarethePrintDocumentobject. 40. private PrintDocumentdocToPrint= new PrintDocument(); 41. /创建一个PrintDocument的实例 42. 43. private StreamstreamToPrint; 44. stringstreamType; 45. 46. /ThismethodwillsetpropertiesonthePrintDialogobjectand 47. /thendisplaythedialog. 48. publicvoidStartPrint(StreamstreamToPrint,stringstreamType) 49. 50. 51. this.streamToPrint=streamToPrint; 52. this.streamType=streamType; 53. /Allowtheusertochoosethepagerangeheorshewould 54. /liketoprint. 55. PrintDialogPrintDialog1=newPrintDialog();56. /实现C#打印之创建一个PrintDialog的实例。 57. PrintDialog1.AllowSomePages=true; 58. 59. /Showthehelpbutton. 60. PrintDialog1.ShowHelp=true; 61. 62. /SettheDocumentpropertytothePrintDocumentfor 63. /whichthePrintPageEventhasbeenhandled.Todisplaythe 64. /dialog,eitherthispropertyorthePrinterSettingsproperty 65. /mustbeset 66. PrintDialog1.Document=docToPrint; 67. /把PrintDialog的Document属性设为上面配置好的PrintDocument的实例 68. 69. DialogResultresult=PrintDialog1.ShowDialog(); 70. /调用PrintDialog的ShowDialog函数显示打印对话框 71. 72. /IftheresultisOKthenprintthedocument. 73. if(result=DialogResult.OK) 74. 75. docToPrint.Print();/实现C#打印之开始打印 76. 77. 78. 79. 80. /ThePrintDialogwillprintthedocument 81. /byhandlingthedocumentsPrintPageevent. 82. privatevoiddocToPrint_PrintPage(objectsender, 83. System.Drawing.Printing.PrintPageEventArgse) 84. /设置打印机开始打印的事件处理函数 85. 86. 87. /Insertcodetorenderthepagehere. 88. /Thiscodewillbecalledwhenthecontrolisdrawn. 89. 90. /Thefollowingcodewillrenderasimple 91. /messageontheprinteddocument 92. switch(this.streamType) 93. 94. casetxt: 95. stringtext=null; 96. System.Drawing.FontprintFont=newSystem.Drawing.Font 97. (Arial,35,System.Drawing.FontStyle.Regular); 98. 99. /Drawthecontent. 100. System.IO.StreamReaderstreamReader= 101. newStreamReader(this.streamToPrint); 102. text=streamReader.ReadToEnd(); 103. e.Graphics.DrawString(text,printFont, 104. System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y); 105. break; 106. caseimage: 107. System.Drawing.Imageimage= 108. System.Drawing.Image.FromStream(this.streamToPrint); 109. intx=e.MarginBounds.X; 110. inty=e.MarginBounds.Y; 111. intwidth=image.Width; 112. intheight=image.Height; 113. if(width/e.MarginBounds.Width)( 114. height/e.MarginBounds.Height) 115. 116. width=e.MarginBounds.Width; 117. height=image.Height*e.MarginBounds.Width/image.Width; 118. 119. else120. 121. height=e.MarginBounds.Height; 122. width=image.Width*e.MarginBounds.Height/image.Height; 123. 124. System.Drawing.RectangledestRect= 125. newSystem.Drawing.Rectangle(x,y,width,height); 126. e.Graph
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 春夜喜雨:唐诗鉴赏及创作启发教学教案
- 老旧小区改造工程的策略及实施路径
- 现代企业管理理论与实务试题集汇编
- 我的心爱之物物品介绍作文(5篇)
- 软件测试技术与实践题目解析
- 《数学微积分基本概念与运用教案》
- 国际贸易发展趋势研究表格
- 书中寻宝记读后感演讲稿15篇
- 2025年物理学高考复习综合测试卷及答案
- 2025年生态学与可持续发展专业考试知识试题及答案
- 2025年佛山市南海区图书馆招聘题库带答案分析
- 中华民族共同体概论知到课后答案智慧树章节测试答案2025年春丽水学院
- 2024年浙江省中考社会试卷真题(含标准答案及评分标准)
- 加油站安全隐患排查检查表
- 《饮料总酸度的测定》教学设计
- 固定资产投资统计培训PPT课件
- 河南省天一大联考高一下学期期末数学试题(解析版)
- 广州市登革热疫情应急演练方案
- GB_T 30789.8-2015 色漆和清漆 涂层老化的评价 缺陷的数量和大小以及外观均匀变化程度的标识 第8部分:划线或其他人造缺陷周边剥离和腐蚀等级的评定
- 建设工程项目管理论文范文
- 同步发电机调速系统仿真设计
评论
0/150
提交评论