国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c#windowsForm打印

2019-11-14 16:35:08
字體:
來源:轉載
供稿:網友

在windows應用程序中文檔的打印是一項非常重要的功能,在以前一直是一個非常復雜的工作,Microsoft .net Framework的打

印功能都以組件的方式提供,為程序員提供了很大的方便,但是這幾個組件的使用還是很復雜的,有必要解釋一下。

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

1 打印設置 設置打印機的一些參數比如更改打印機驅動程序等

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

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

4 打印

實現打印功能的核心是PRintDocument類這個類屬于System.Drawing.Printing名字空間這個類封裝了當前的打印設置頁面設置以及所

有的與打印有關的事件和方法

這個類包括以下幾個屬性 事件 和方法

1、PrinterSettings 屬性

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

2、PrintCountroller 屬性

  控制打印過程

3、DefaultPageSettings 屬性

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

4、DocumentName 屬性

  指定文檔名稱,出現在打印機狀態窗口中

1。 BeginPrint事件

  在打印之前發出

2. PrintPage事件

  每打印一頁是發出,事件接受一個PrintPageEventArgs參數該參數封裝了打印相關的信息

  PrintPageEventArgs參數有很多重要的屬性

  1 Cancel 取消打印

  2 Graphics 頁面的繪圖對象

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

Print 方法 該方法沒有參數 調用它將按照當前設置開始打印

若實現打印功能首先構造PrintDocument對象添加打印事件

PrintDocument printDocument;

private void InitializeComponent()

{

...

printDocument=new PrintDocument();

printDocument.PrintPage += new PrintPageEventHandler (this.printDocument_PrintPage);

...

}

實現打印事件功能

打印和繪圖類似都是調用Graphics 類的方法進行畫圖 不同的是一個在顯示器上一個在打印紙上并且打印要進行一些復雜的計算

如換行 分頁等。

private void printDocument_PrintPage(object sender,PrintPageEventArgs e)

{

StringReader lineReader = new StringReader(textBox.Text);

Graphics g = e.Graphics; //獲得繪圖對象

float linesPerPage = 0; //頁面的行號

float yPosition = 0;   //繪制字符串的縱向位置

int count = 0; //行計數器

float leftMargin = e.MarginBounds.Left; //左邊距

float topMargin = e.MarginBounds.Top; //上邊距

string line = null; 行字符串

Font printFont = this.textBox.Font; //當前的打印字體

SolidBrush myBrush = new SolidBrush(Color.Black);//刷子

linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每頁可打印的行數

//逐行的循環打印一頁

    while(count < linesPerPage && ((line=lineReader.ReadLine()) != null))

    {

       yPosition = topMargin + (count * printFont.GetHeight(g));

       g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());

       count++;

    }

如果本頁打印完成而line不為空說明還有沒完成的頁面這將觸發下一次的打印事件在下一次的打印中lineReader會

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

    if(line != null)

        e.HasMorePages = true;

    else

        e.HasMorePages = false;

}

打印設置,構造打印對話框 將對話框中設置的Document屬性賦給printDocument這樣會將用戶的設置自動保存到printDocument

的PrinterSettings屬性中

protected  void FileMenuItem_PrintSet_Click(object sender,EventArgs e)

{

PrintDialog printDialog = new PrintDialog();

printDialog.Document = printDocument;

printDialog.ShowDialog();

}

頁面設置和打印預覽與打印設置原理相同都是構造對話框將用戶在對話框中的設置保存到相應的類的屬性中

protected  void FileMenuItem_PageSet_Click(object sender,EventArgs e)

{

  PageSetupDialog pageSetupDialog = new PageSetupDialog();

  pageSetupDialog.Document = printDocument;

  pageSetupDialog.ShowDialog();

}

打印預覽

protected void FileMenuItem_PrintView_Click(object sender,EventArgs e)

{

   PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();

   printPreviewDialog.Document = printDocument;

      try

      {

    printPreviewDialog.ShowDialog();

      }

    catch(Exception excep)

    {

    MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

}

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

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

  protected void FileMenuItem_Print_Click(object sender,EventArgs e)

  {

   PrintDialog printDialog = new PrintDialog();

   printDialog.Document = printDocument;

   lineReader = new StringReader(textBox.Text);

   if (printDialog.ShowDialog() == DialogResult.OK)

   {

    try

       {

       printDocument.Print();

       }

       catch(Exception excep)

            {

              MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);

              printDocument.PrintController.OnEndPrint(printDocument,new PrintEventArgs());

            }

       }

  }

總結打印的過程是

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

2 實現PrintPage方法  4 在用戶的單擊事件中調用 printDocument 的 Print方法實現打印功能

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北碚区| 沙洋县| 黄骅市| 永城市| 荥阳市| 鄯善县| 内黄县| 郁南县| 高阳县| 尼木县| 灵丘县| 公主岭市| 龙门县| 池州市| 武邑县| 宁强县| 宜城市| 西吉县| 遂宁市| 白山市| 隆回县| 衡水市| 社会| 许昌市| 江油市| 六枝特区| 岳池县| 汉源县| 顺义区| 甘肃省| 罗田县| 芜湖县| 彩票| 天台县| 叙永县| 克拉玛依市| 新乡县| 东台市| 棋牌| 江阴市| 邵武市|