WPF打印之一
这个示例演示了如何进行一个最简单的打印工作,为此需要引入两个dll:System.Windows.Forms,System.Drawing
/// <summary>
/// 打印类
/// </summary>
public class PrintService
{
public PrintService()
{
//创建一个PrintDialog的实例
System.Windows.Forms.PrintDialog dlg = new System.Windows.Forms.PrintDialog();
//创建一个PrintDocument的实例
System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
//将事件处理函数添加到PrintDocument的PrintPage事件中
docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(docToPrint_PrintPage);
//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
dlg.Document = docToPrint;
//根据用户的选择,开始打印
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
docToPrint.Print();//开始打印
}
}
//设置打印机开始打印的事件处理函数
private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Hello, world!", new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 100, 100);
}
}