自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET Framework打印詳細分析

開發(fā) 后端
.NET Framework打印主要包括以下幾個操作步驟,包括:打印設置;頁面設置;打印預覽;以及最后的結(jié)果打印。其中有一個核心類叫做:PrintDocument。

如果你是一名開發(fā)人員,就不應該不知道.NET Framework。它的作用在編程人員眼中是非常強大的。.NET Framework打印功能都以組件的方式提供,為程序員提供了很大的方便,但是這幾個組件的使用還是很復雜的,有必要解釋一下。 #t#

.NET Framework打印操作通常包括以下四個功能

1 打印設置 設置打印機的一些參數(shù)比如更改打印機驅(qū)動程序等

2 頁面設置 設置頁面大小紙張類型等

3 打印預覽 類似于word中的打印預覽

4 打印

實現(xiàn)打印功能的核心是PrintDocument類這個類屬于System.Drawing.Printing名字空間這個類封裝了當前的打印設置頁面設置以及所有的與打印有關(guān)的事件和方法。這個類包括以下幾個屬性 事件 和方法:

1、PrinterSettings 屬性

存放打印機的設置信息這個屬性不需要程序員設置因為它是由打印對話框獲取的

2、PrintCountroller 屬性

控制.NET Framework打印過程

3、DefaultPageSettings 屬性

存放頁面設置信息 打印紙大小方向等也不需要程序員設置因為它是由頁面設置對話框獲取的

4、DocumentName 屬性

指定文檔名稱,出現(xiàn)在打印機狀態(tài)窗口中

1、BeginPrint事件

在打印之前發(fā)出

2. PrintPage事件

每打印一頁是發(fā)出,事件接受一個PrintPageEventArgs參數(shù)該參數(shù)封裝了打印相關(guān)的信息

PrintPageEventArgs參數(shù)有很多重要的屬性

1 Cancel 取消打印

2 Graphics 頁面的繪圖對象

3 HasMorePages 是否還有要打印的頁面

Print 方法

該方法沒有參數(shù) 調(diào)用它將按照當前設置開始打印

若實現(xiàn)打印功能首先構(gòu)造PrintDocument對象添加打印事件

 

  1. PrintDocument printDocument;   
  2. private void InitializeComponent()   
  3. {   
  4. ...   
  5. printDocument=new PrintDocument();   
  6. printDocument.PrintPage += new 
    PrintPageEventHandler (this.print
    Document_PrintPage);   
  7. ...   
  8. }  

實現(xiàn).NET Framework打印事件功能

打印和繪圖類v似都是調(diào)用Graphics 類的方法進行畫圖 不同的是一個在顯示器上一個在打印紙上并且打印要進行一些復雜的計算,如換行 分頁等。

  1. private void printDocument_PrintPage
    (object sender,PrintPageEventArgs e)   
  2. {   
  3. Graphics g = e.Graphics; //獲得繪圖對象   
  4. float linesPerPage = 0; //頁面的行號   
  5. float yPosition = 0; //繪制字符串的縱向位置   
  6. int count = 0; //行計數(shù)器   
  7. float leftMargin = e.MarginBounds.Left; 
  8. //左邊距   
  9. float topMargin = e.MarginBounds.Top; 
  10. //上邊距   
  11. string line = null; 行字符串   
  12. Font printFont = this.textBox.Font; 
  13. //當前的打印字體   
  14. SolidBrush myBrush = new SolidBrush
    (Color.Black);//刷子   
  15. linesPerPage = e.MarginBounds.Height / 
    printFont.GetHeight(g);//每頁可打印的行數(shù)   
  16. //逐行的循環(huán)打印一頁   
  17. while(count < linesPerPage && ((line=
    lineReader.ReadLine()) != null))   
  18. {   
  19. yPosition = topMargin + (count * 
    printFont.GetHeight(g));   
  20. g.DrawString(line, printFont, myBrush, 
    leftMargin, yPosition, new StringFormat());   
  21. count++;   

如果本頁打印完成而line不為空說明還有沒完成的頁面這將觸發(fā)下一次的打印事件在下一次的打印中l(wèi)ineReader會

自動讀取上次沒有打印完的內(nèi)容因為lineReader是這個打印方法外的類的成員它可以記錄當前讀取的位置

  1. if(line != null)   
  2. e.HasMorePages = true;   
  3. else   
  4. e.HasMorePages = false;   

.NET Framework打印設置,構(gòu)造打印對話框 將對話框中設置的Document屬性賦給printDocument這樣會將用戶的設置自動保存到printDocument的PrinterSettings屬性中

  1. protected void FileMenuItem_
    PrintSet_Click(object sender,
    EventArgs e)   
  2. {   
  3. PrintDialog printDialog = 
    new PrintDialog();   
  4. printDialog.Document =
     
    printDocument;   
  5. printDialog.ShowDialog();   

頁面設置和打印預覽與打印設置原理相同都是構(gòu)造對話框?qū)⒂脩粼趯υ捒蛑械脑O置保存到相應的類的屬性中

  1. protected void FileMenuItem_PageSet_
    Click(object sender,EventArgs e)   
  2. {   
  3. PageSetupDialog pageSetupDialog = 
    new PageSetupDialog();   
  4. pageSetupDialog.Document = 
    printDocument;   
  5. pageSetupDialog.ShowDialog();   

.NET Framework打印預覽

  1. protected void FileMenuItem_PrintView_
    Click(object sender,EventArgs e)   
  2. {   
  3. PrintPreviewDialog printPreviewDialog = 
    new PrintPreviewDialog();   
  4. printPreviewDialog.Document = 
    printDocument;   
  5. lineReader = new StringReader
    (textBox.Text);   
  6. try   
  7. {   
  8. printPreviewDialog.ShowDialog();   
  9. }   
  10. catch(Exception excep)   
  11. {   
  12. MessageBox.Show(excep.Message, "打印出錯",
     MessageBoxButtons.OK, MessageBoxIcon.Error);   
  13. }   

打印就可以直接調(diào)用printDocument的Print()方法因為用戶可能在打印之前還要再更改打印設置所以

在這里再次顯示.NET Framework打印設置對話框

  1. protected void FileMenuItem_Print_Click
    (object sender,EventArgs e)   
  2. {   
  3. PrintDialog printDialog = new PrintDialog();   
  4. printDialog.Document = printDocument;   
  5. lineReader = new StringReader(textBox.Text);   
  6. if (printDialog.ShowDialog() == DialogResult.OK)   
  7. {   
  8. try   
  9. {   
  10. printDocument.Print();   
  11. }   
  12. catch(Exception excep)   
  13. {   
  14. MessageBox.Show(excep.Message, "打印出錯", 
    MessageBoxButtons.OK, MessageBoxIcon.Error);   
  15. printDocument.PrintController.OnEndPrint
    (printDocument,new PrintEventArgs());   
  16. }   
  17. }   

總結(jié).NET Framework打印的過程是

1 在應用程序窗體初始化時構(gòu)造PrintDocument對象 添加 printDocument 的 PrintPage 方法

2 實現(xiàn)PrintPage方法 4 在用戶的單擊事件中調(diào)用 printDocument 的 Print方法實現(xiàn)打印功能

在這中間可能要用到 PrintDialog PrintPreviewDialog PageSetupDialog 設置和查看打印效

果這些方法通常是由菜單的單擊觸發(fā)的。

責任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-10-28 10:06:29

VB.NET With

2009-10-10 15:19:43

VB.NET Web

2009-10-12 15:41:09

VB.NET動態(tài)代碼

2009-09-25 14:23:39

2009-09-28 10:39:01

Hibernate基礎

2009-06-18 14:00:51

2009-09-09 09:48:43

Linq延遲加載

2009-10-10 13:52:57

VB Update方法

2009-09-14 16:21:34

LINQ To XML

2009-09-14 13:50:35

LINQ編程模型

2009-09-08 15:56:50

Linq使用Group

2009-11-20 13:11:44

Oracle XML數(shù)

2009-11-24 13:56:53

Visual Stud

2010-01-07 17:00:38

VB.NET控件數(shù)組

2009-12-07 15:37:00

WCF控件

2009-09-04 15:43:07

C#流模型

2009-09-07 13:19:44

C#線程同步

2009-03-24 08:30:54

AndroidGoogle移動os

2010-04-26 18:17:19

Oracle存儲過程

2009-09-03 17:57:06

C#聲明事件
點贊
收藏

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