日期:2014-05-17 浏览次数:20991 次
PrintDocument printDoc = new PrintDocument(); PrintPreviewDialog ppvw = new PrintPreviewDialog(); ppvw.Document = printDoc; //显示页面的打印预览 printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage); printDoc.Print();
------解决方案--------------------
完整代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Printing;//打印命名空间 namespace Study { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void btnCommon_Click(object sender, EventArgs e) { PrintDocument printDoc = new PrintDocument(); PrintPreviewDialog ppvw = new PrintPreviewDialog(); ppvw.Document = printDoc; printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintTextValue); if (ppvw.ShowDialog() != DialogResult.OK) { printDoc.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintTextValue);//取消打印 return; } printDoc.Print(); } private void PrintDoc_PrintTextValue(object sender, System.Drawing.Printing.PrintPageEventArgs e) { try { e.Graphics.DrawString(richTextBox1.Text, new Font("宋体", 14, FontStyle.Bold), Brushes.Black, 30, 105); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }