概述
在Winform 中可以通过打印控件方便的实现应用程序打印功能,本文将使用一个简单实例进行演示。
操作步骤:
1、新建winform项目及创建窗体
2、拖取 打印 相关控件
PrintDialog 、 PrintDocument
3、设置打印按钮等控件 及 添加相应按钮事件
4、示意代码如下:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.printDocument1.OriginAtMargins = true;//启用页边距
this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
}
//打印设置
private void btnSetPrint_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
}
//打印预览
private void btnPrePrint_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.ShowDialog();
}
//打印
private void btnPrint_Click(object sender, EventArgs e)
{
this.printDialog1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
this.printDocument1.Print();
}
//打印内容的设置
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打印内容 为 整个Form
Image myFormImage;
myFormImage = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(myFormImage);
g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
e.Graphics.DrawImage(myFormImage, 0, 0);
打印内容 为 局部的 this.groupBox1
//Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
//groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
//e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
}
}
}
******WPF XAML技术交流群:36720523******
最后
以上就是英勇指甲油为你收集整理的C# Winform 无弹窗打印实例的全部内容,希望文章能够帮你解决C# Winform 无弹窗打印实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复