最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#打印類PrintDocument、PrintDialog、PrintPreviewDialog使用示例

 更新時間:2015年06月16日 11:41:10   投稿:junjie  
這篇文章主要介紹了C#打印類PrintDocument、PrintDialog、PrintPreviewDialog使用示例,本文分別給出了示例代碼,需要的朋友可以參考下

1.使用PrintDocument進行打印

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
 
namespace PrintTest
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
      //實例化打印對象
      PrintDocument printDocument1 = new PrintDocument();
      //設置打印用的紙張,當設置為Custom的時候,可以自定義紙張的大小
      printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500);
      //注冊PrintPage事件,打印每一頁時會觸發(fā)該事件
      printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
      //開始打印
      printDocument1.Print();
    }
    private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
      //設置打印內(nèi)容及其字體,顏色和位置
      e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑體"), 24), System.Drawing.Brushes.Red, 50, 50);
    }
  }
}

2.使用PrintDialog增加打印對話框

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
 
namespace PrintTest
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
      //實例化打印對象
      PrintDocument printDocument1 = new PrintDocument();
      //設置打印用的紙張,當設置為Custom的時候,可以自定義紙張的大小
      printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500);
      //注冊PrintPage事件,打印每一頁時會觸發(fā)該事件
      printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
   
      //初始化打印對話框?qū)ο?
      PrintDialog printDialog1 = new PrintDialog();
      //將PrintDialog.UseEXDialog屬性設置為True,才可顯示出打印對話框
      printDialog1.UseEXDialog = true;
      //將printDocument1對象賦值給打印對話框的Document屬性
      printDialog1.Document = printDocument1;
      //打開打印對話框
      DialogResult result = printDialog1.ShowDialog();
      if (result == DialogResult.OK)         
        printDocument1.Print();//開始打印
    }
    private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
      //設置打印內(nèi)容及其字體,顏色和位置
      e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑體"), 24), System.Drawing.Brushes.Red, 50, 50);
    }
  }
}

打印對話框如下圖所示。

3.使用PrintPreviewDialog增加打印預覽對話框

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
 
namespace PrintTest
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
      //實例化打印對象
      PrintDocument printDocument1 = new PrintDocument();
      //設置打印用的紙張,當設置為Custom的時候,可以自定義紙張的大小
      printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 500, 500);
      //注冊PrintPage事件,打印每一頁時會觸發(fā)該事件
      printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
   
      //初始化打印預覽對話框?qū)ο?
      PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
      //將printDocument1對象賦值給打印預覽對話框的Document屬性
      printPreviewDialog1.Document = printDocument1;
      //打開打印預覽對話框
      DialogResult result = printPreviewDialog1.ShowDialog();
      if (result == DialogResult.OK)         
        printDocument1.Print();//開始打印
    }
    private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
      //設置打印內(nèi)容及其字體,顏色和位置
      e.Graphics.DrawString("Hello World!", new Font(new FontFamily("黑體"), 24), System.Drawing.Brushes.Red, 50, 50);
    }
  }
}

打印時,會顯示下圖所示預覽畫面。

注意:PrintDialog與PrintPreviewDialog位于名稱空間System.Windows.Forms(程序集為System.Windows.Forms.dll)中,而PrintDocument位于名稱空間System.Drawing.Printing(程序集為System.Drawing.dll)中。

相關(guān)文章

最新評論

高淳县| 临湘市| 连州市| 丹阳市| 五大连池市| 舒兰市| 宣武区| 靖边县| 西平县| 大田县| 山阳县| 蓬莱市| 灵台县| 道孚县| 太谷县| 确山县| 元阳县| 泊头市| 大冶市| 镇巴县| 霍林郭勒市| 普格县| 双柏县| 大竹县| 平凉市| 阿勒泰市| 五原县| 南安市| 泸西县| 昆明市| 华阴市| 攀枝花市| 清涧县| 中江县| 大埔县| 成安县| 宜兰市| 巨鹿县| 黄龙县| 嘉鱼县| 内乡县|