|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace print
{
public partial class Form1 : Form
{
private int CheckPoint; //定义全局变量表示为页面
public Form1()
{
InitializeComponent();
}
private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
MessageBox.Show(printDocument1.DocumentName + " 已经打印完成!"); //打印结束
}
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
CheckPoint = 0; //打印开始
}
private void button1_Click(object sender, EventArgs e)
{
PageSetupDialog ps = new PageSetupDialog(); //通过模式窗体调用打印页面设置
ps.Document = printDocument1;
ps.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog(); //通过模式窗体调用打印
pd.Document = printDocument1;
if (pd.ShowDialog() != DialogResult.Cancel)
{
try
{
printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
StringReader lineReader = new StringReader(zybRichTextBox1.Text);//注意引用IO,将我开发的自定义文本筐的内容读出来
try
{
PrintPreviewDialog ppd = new PrintPreviewDialog();//通过模式窗体调用打印页面设置
ppd.Document = printDocument1;
ppd.FormBorderStyle = FormBorderStyle.Fixed3D;
ppd.ShowDialog();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
CheckPoint = zybRichTextBox1.Print(CheckPoint, zybRichTextBox1.TextLength, e); //打印装载事件
if (CheckPoint < zybRichTextBox1.TextLength)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
}
}
关于printDocument控件的更详细使用请查看微软官方网站MSDN
测试效果如图
|
-
|