C#打印類PrintDocument、PrintDialog、PrintPreviewDialog應用示例。本站提示廣大學習愛好者:(C#打印類PrintDocument、PrintDialog、PrintPreviewDialog應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是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事宜,打印每頁時會觸發該事宜 printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //開端打印 printDocument1.Print(); } private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //設置打印內容及其字體,色彩和地位 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事宜,打印每頁時會觸發該事宜 printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //初始化打印對話框對象 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) { //設置打印內容及其字體,色彩和地位 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事宜,打印每頁時會觸發該事宜 printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage); //初始化打印預覽對話框對象 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) { //設置打印內容及其字體,色彩和地位 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)中。