一.分析步骤:
首先,在工具箱中添加一个PrintDocument的主要打印控件
然后,先后添加PrintDialog,PagePreviewDialog,PageSetDialog控件,并同时,设置它们的Document的属性为开始添加的PrintDocument的Name
最后,分别完善实现打印,预览的事件代码
二.效果截图
三.程序代码
public partial class Form1 : Form { public Form1() { InitializeComponent(); btnPreview.Click += btnPreview_Click; btnPrint.Click += btnPrint_Click; } /// <summary> /// 打印 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void btnPrint_Click(object sender, EventArgs e) { //打印机设置 if (printDialog.ShowDialog()==DialogResult.OK) { //页面设置 if (pageSetupDialog.ShowDialog() == DialogResult.OK) { printDocument.Print(); } } } /// <summary> /// 打印预览 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void btnPreview_Click(object sender, EventArgs e) { printPreviewDialog.ShowDialog(); } /// <summary> /// 打印内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void printDocument_PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString("小样你好", new Font("宋体", 12), Brushes.Black, 10, 10); } }