偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C#實(shí)現(xiàn)打印功能實(shí)例詳解

開(kāi)發(fā) 后端
C#實(shí)現(xiàn)打印功能是通過(guò)什么來(lái)實(shí)現(xiàn)的?C#實(shí)現(xiàn)打印功能的步驟是什么呢?那么本文就向你介紹這方面的內(nèi)容。

C#實(shí)現(xiàn)打印功能是通過(guò)使用PrintDialog控件來(lái)實(shí)現(xiàn)的。任何物有所值的應(yīng)用程序都會(huì)擁有某種打印功能,不管是基本的打印功能還是更為復(fù)雜的打印功能,比如允許用戶只打印所選的文本或某個(gè)范圍內(nèi)的頁(yè)面。本節(jié)將探討一下實(shí)現(xiàn)基本的C#打印功能,看看哪些類(lèi)有助于打印文件中的文本。C#實(shí)現(xiàn)打印功能的過(guò)程是:在調(diào)用PrintDialog控件的ShowDialog方法之前,必須先設(shè)置PrintDialog類(lèi)的Document屬性。該屬性接受一個(gè)PrintDocument類(lèi),以獲得打印機(jī)設(shè)置并將輸出內(nèi)容發(fā)送給打印機(jī)。PrintDocument類(lèi)需要System.Drawing.Printing名稱空間,因此,在定義使用PrintDocument類(lèi)的對(duì)象前,必須包含這個(gè)名稱空間。

C#實(shí)現(xiàn)打印功能具體的操作步驟如下:

創(chuàng)建一個(gè)PrintDialog的實(shí)例。如下:

  1. System.Windows.Forms.PrintDialog PrintDialog1=new PrintDialog ();  

創(chuàng)建一個(gè)PrintDocument的實(shí)例.如下:

  1. System.Drawing.Printing.PrintDocument docToPrint =   
  2.  new System.Drawing.Printing.PrintDocument();  

設(shè)置打印機(jī)開(kāi)始打印的事件處理函數(shù).函數(shù)原形如下:

  1. void docToPrint_PrintPage(object sender,   
  2.  System.Drawing.Printing.PrintPageEventArgs e)  

將事件處理函數(shù)添加到PrintDocument的PrintPage事件中。

  1. docToPrint.PrintPage+=  
  2.  
  3. new PrintPageEventHandler(docToPrint_PrintPage);   

設(shè)置PrintDocument的相關(guān)屬性,如:

  1. PrintDialog1.AllowSomePages =   
  2.  
  3. true;PrintDialog1.ShowHelp = true;   

把PrintDialog的Document屬性設(shè)為上面配置好的PrintDocument的實(shí)例:

  1. PrintDialog1.Document = docToPrint;  

調(diào)用PrintDialog的ShowDialog函數(shù)顯示打印對(duì)話框:

  1. DialogResult result = PrintDialog1.ShowDialog();  

根據(jù)用戶的選擇,開(kāi)始打印:

  1. if (result==DialogResult.OK)  
  2.  {  
  3. docToPrint.Print();  
  4.  } 

C#實(shí)現(xiàn)打印功能的實(shí)例如下:

使用時(shí)先創(chuàng)建PrintService類(lèi)的實(shí)例,然后調(diào)用void StartPrint(Stream streamToPrint,string streamType)函數(shù)開(kāi)始打印。其中streamToPrint是要打印的內(nèi)容(字節(jié)流),streamType是流的類(lèi)型(txt表示普通文本,image表示圖像);

  1. using System;  
  2. using System.Drawing.Printing;  
  3. using System.Windows.Forms;  
  4. using System.IO;   
  5.  
  6. namespace EDImageSystem  
  7. {  
  8.  /// <summary>  
  9.  /// PrintService 的摘要說(shuō)明。  
  10.  /// </summary>  
  11.  public class PrintService  
  12.  {  
  13. public PrintService()  
  14. {  
  15.  //  
  16.  // TODO: 在此處添加構(gòu)造函數(shù)邏輯  
  17.  //  
  18.  this.docToPrint.PrintPage+=  
  19. new PrintPageEventHandler(docToPrint_PrintPage);  
  20. }//將事件處理函數(shù)添加到PrintDocument的PrintPage中   
  21.  
  22. // Declare the PrintDocument object.  
  23. private System.Drawing.Printing.PrintDocument docToPrint =   
  24.  new System.Drawing.Printing.PrintDocument();  
  25. //創(chuàng)建一個(gè)PrintDocument的實(shí)例   
  26.  
  27. private System.IO.Stream streamToPrint;  
  28. string streamType;   
  29.  
  30. // This method will set properties on the PrintDialog object and  
  31. // then display the dialog.  
  32. public void StartPrint(Stream streamToPrint,string streamType)  
  33. {   
  34.  
  35.  this.streamToPrint=streamToPrint;  
  36.  this.streamType=streamType;  
  37.  // Allow the user to choose the page range he or she would  
  38.  // like to print.  
  39.  System.Windows.Forms.PrintDialog PrintDialog1=  
  40. new PrintDialog ();//實(shí)現(xiàn)C#打印之創(chuàng)建一個(gè)PrintDialog的實(shí)例。  
  41.  PrintDialog1.AllowSomePages = true;   
  42.  
  43.  // Show the help button.  
  44.  PrintDialog1.ShowHelp = true;   
  45.  
  46.  // Set the Document property to the PrintDocument for   
  47.  // which the PrintPage Event has been handled. To display the  
  48.  // dialog, either this property or the PrinterSettings property   
  49.  // must be set   
  50.  PrintDialog1.Document = docToPrint;  
  51. //把PrintDialog的Document屬性設(shè)為上面配置好的PrintDocument的實(shí)例   
  52.  
  53.  DialogResult result = PrintDialog1.ShowDialog();  
  54. //調(diào)用PrintDialog的ShowDialog函數(shù)顯示打印對(duì)話框   
  55.  
  56.  // If the result is OK then print the document.  
  57.  if (result==DialogResult.OK)  
  58.  {  
  59. docToPrint.Print();//實(shí)現(xiàn)C#打印之開(kāi)始打印  
  60.  }   
  61.  
  62. }   
  63.  
  64. // The PrintDialog will print the document  
  65. // by handling the document's PrintPage event.  
  66. private void docToPrint_PrintPage(object sender,   
  67.  System.Drawing.Printing.PrintPageEventArgs e)  
  68. //設(shè)置打印機(jī)開(kāi)始打印的事件處理函數(shù)  
  69. {   
  70.  
  71.  // Insert code to render the page here.  
  72.  // This code will be called when the control is drawn.   
  73.  
  74.  // The following code will render a simple  
  75.  // message on the printed document  
  76.  switch(this.streamType)  
  77.  {  
  78. case "txt":  
  79.  string text = null;  
  80.  System.Drawing.Font printFont = new System.Drawing.Font  
  81. ("Arial", 35, System.Drawing.FontStyle.Regular);   
  82.  
  83.  // Draw the content.  
  84.  System.IO.StreamReader streamReader=  
  85. new StreamReader(this.streamToPrint);  
  86.  text=streamReader.ReadToEnd();  
  87.  e.Graphics.DrawString(text,printFont,  
  88. System.Drawing.Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y);  
  89.  break;  
  90. case "image":  
  91.  System.Drawing.Image image=  
  92. System.Drawing.Image.FromStream(this.streamToPrint);  
  93.  int x=e.MarginBounds.X;  
  94.  int y=e.MarginBounds.Y;  
  95.  int width=image.Width;  
  96.  int height=image.Height;  
  97.  if((width/e.MarginBounds.Width)>(  
  98. height/e.MarginBounds.Height))  
  99.  {  
  100. width=e.MarginBounds.Width;  
  101. height=image.Height*e.MarginBounds.Width/image.Width;  
  102.  }  
  103.  else 
  104.  {  
  105. height=e.MarginBounds.Height;  
  106. width=image.Width*e.MarginBounds.Height/image.Height;  
  107.  }  
  108.  System.Drawing.Rectangle destRect=  
  109. new System.Drawing.Rectangle(x,y,width,height);  
  110.  e.Graphics.DrawImage(image,  
  111. destRect,0,0,image.Width,image.Height,  
  112. System.Drawing.GraphicsUnit.Pixel);  
  113.  break;  
  114. default:  
  115.  break;  
  116.  }  
  117.    
  118. }   
  119.  
  120. }  
  121. }  

實(shí)現(xiàn)C#打印的具體實(shí)現(xiàn)步驟和具體的實(shí)例演示就向你介紹到這里,希望對(duì)你了解實(shí)現(xiàn)C#打印以及學(xué)習(xí)C#有所幫助。

【編輯推薦】

  1. 創(chuàng)建C#串口通信程序詳解
  2. 詳解C#串口監(jiān)聽(tīng)的實(shí)現(xiàn)
  3. C#入門(mén)之概念簡(jiǎn)介
  4. C#入門(mén)之C#特點(diǎn)淺析
  5. .NET Framework概念及開(kāi)發(fā)淺析
責(zé)任編輯:仲衡 來(lái)源: 博客園
相關(guān)推薦

2009-08-26 11:32:37

C#打印文檔

2009-08-26 11:07:36

C#打印窗體

2009-08-26 10:43:14

C#實(shí)現(xiàn)打印功能

2009-08-26 12:59:08

C#打印設(shè)置

2009-08-21 10:13:02

C#異步初步

2009-08-26 13:22:24

C#打印程序

2009-08-26 11:53:56

C#打印文本文件

2009-09-03 19:00:15

C#判斷瀏覽器

2009-09-09 12:55:59

C# TextBox事

2009-09-03 14:55:56

C#實(shí)現(xiàn)DataGri

2009-08-26 12:14:44

C#打印設(shè)置

2009-09-02 17:12:06

C#關(guān)機(jī)代碼

2009-08-20 11:01:51

C#操作內(nèi)存

2009-08-18 10:14:19

C#插件構(gòu)架

2009-09-11 12:31:52

C#實(shí)例詳解TypeConvert

2009-08-26 09:54:45

C#打印預(yù)覽C#打印

2009-08-26 14:31:08

C#打印文件

2009-08-28 12:47:30

C#靜態(tài)方法應(yīng)用

2009-08-18 17:05:08

C#操作xml文件

2009-08-28 13:12:56

C#反射實(shí)例C#反射
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)