av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

創(chuàng)新互聯(lián)公司主要從事網(wǎng)頁設計、PC網(wǎng)站建設(電腦版網(wǎng)站建設)、wap網(wǎng)站建設(手機版網(wǎng)站建設)、響應式網(wǎng)站、程序開發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、小程序制作等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設行業(yè)積累了豐富的成都網(wǎng)站設計、網(wǎng)站制作、網(wǎng)站設計、網(wǎng)絡營銷經(jīng)驗,集策劃、開發(fā)、設計、營銷、管理等多方位專業(yè)化運作于一體。

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

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

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

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

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

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

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

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

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

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

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

把PrintDialog的Document屬性設為上面配置好的PrintDocument的實例:

 
 
 
  1. PrintDialog1.Document = docToPrint; 

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

 
 
 
  1. DialogResult result = PrintDialog1.ShowDialog(); 

根據(jù)用戶的選擇,開始打?。?/p>

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

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

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

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

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


本文名稱:C#實現(xiàn)打印功能實例詳解
文章網(wǎng)址:http://uogjgqi.cn/article/cdsdedi.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流