在windows应用程序中打印是一项非常重要的功能,在实际运用中也较多,.net中的打印功能都以组件的方式提供,为程序员提供了很大的方便,打印 操作通常包括以下四个功能

1 打印设置 设置打印机的一些参数比如更改打印机驱动程序等

2 页面设置 设置页面大小纸张类型等

3 打印预览 类似于word中的打印预览

4 打印

下面以是一个简单的示例

打印预览分页(C打印操作)(1)

打印预览分页(C打印操作)(2)

1、打印机设置代码

PrintDialogprintDialog = newPrintDialog(); printDialog.Document = printDocument; printDialog.ShowDialog();

2、打印纸张设置代码

PageSetupDialogpageSetupDialog = newPageSetupDialog(); pageSetupDialog.Document = printDocument; pageSetupDialog.ShowDialog();

3、打印预览代码

printDocument.PrintPage = PrintDocument_PrintPage; PrintPreviewDialogprintPreviewDialog = newPrintPreviewDialog{ Document = printDocument }; try { printPreviewDialog.ShowDialog(); } catch(Exceptionexcep) { MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error); }

4、打印内容设置代码

Graphicsg = e.Graphics ; Penp_Line = newPen(Color.Blue, 2f); p_Line.DashStyle = DashStyle.Solid; // g.DrawRectangle(p_Line, newRectangle(100, 50, 300, 200)); g.DrawLine(p_Line, newPoint(100, 150), newPoint(400, 150)); g.DrawLine(p_Line, newPoint(100, 188), newPoint(400, 188)); g.DrawLine(p_Line, newPoint(100, 221), newPoint(400, 221)); // g.DrawLine(p_Line, newPoint(200, 83), newPoint(400, 83)); g.DrawLine(p_Line, newPoint(200, 116), newPoint(400, 116)); //竖线 g.DrawLine(p_Line, newPoint(200, 50), newPoint(200, 250)); g.DrawLine(p_Line, newPoint(300, 50), newPoint(300, 150)); //文字 Brushb_Text = newSolidBrush(Color.Black); g.DrawString("姓名", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(230, 55)); g.DrawString("性别", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(230, 88)); g.DrawString("民族", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(230, 121)); g.DrawString("Lena", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(330, 55)); g.DrawString("女", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(340, 88)); g.DrawString("未知", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(330, 121)); g.DrawString("公司名称", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(115, 155)); g.DrawString("职位", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(130, 193)); g.DrawString("联系电话", newFont("微软雅黑", 12f, FontStyle.Regular), b_Text, newPoint(115, 225)); pic = resizeImage(pic,newSize(92,92)); g.DrawImage(pic, 102, 52);

打印代码

printDocument.PrintPage = PrintDocument_PrintPage; try { printDocument.Print(); } catch(Exceptionexcep) { MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error); printDocument.PrintController.OnEndPrint(printDocument, newPrintEventArgs()); }


5、完整代码已上传,如需请点赞关注后私信发送“打印”获取;感谢您的阅读。

,