基于php旅游网站的毕业设计,太原网站建设主页,有没有做那事的网站,网站设计实验本文的代码可以从这里获取#xff1a;winformDemo.rar 张祥裕/分享的资源名称 - Gitee.com 作者的水平有限#xff0c;如有错误#xff0c;望指正。 为了简单起见#xff0c;纸张大小#xff0c;打印机等信息按照默认的来#xff0c;本文的实现方案是#xff1a;打印Pa…本文的代码可以从这里获取winformDemo.rar · 张祥裕/分享的资源名称 - Gitee.com 作者的水平有限如有错误望指正。 为了简单起见纸张大小打印机等信息按照默认的来本文的实现方案是打印Panel中的控件信息循环进行打印打印完一张把信息重新填充到对应的控件上再继续打印。
这个地址winforms - Print the entire area of the control with C# - Stack Overflow链接提到了这么一句 说是要把控件里的内容重新赋值给另外一个控件myControlToDisplay大概看了一下提供的代码感觉代码不全就没有照着来试了 还有这里的链接
Printing Multiple Pages of a Text File in Windows Forms | DotNetCurry
读取txt记事本里面的内容并分页打印提供的代码 提到了一个HasMorePages属性设置它应该可以实现连续打印结合这两个链接提供的方案这让我想到了另外一种方案把要打印的内容全部渲染到一个容器控件上根据纸张的大小计算并截取要打印的内容类似效果如下 从上往下就像叠罗汉一样直到截取并打印完毕这只是个人的想法还没验证。 好了本文的内容正式开始
步骤如下
1 新建Winform程序名为winfromDemo
UI布局及控件的名称如下 2 新增类名为Student代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace winformDemo
{public class Student{/// summary/// 姓名/// /summarypublic string Name { set; get; }/// summary/// 年龄/// /summarypublic string Age { set; get; }}
}3 打印逻辑代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace winformDemo
{public partial class Form1 : Form{PrintPreviewDialog printDialog new PrintPreviewDialog();PrintDocument printDocument new PrintDocument();public Form1(){InitializeComponent();InitPrintInfo();this.btnPrint.Click new System.EventHandler(this.btnPrint_Click);}/// summary/// 初始化打印信息/// /summaryprivate void InitPrintInfo(){printDialog.Document printDocument;printDocument.PrintPage PrintDocument_PrintPage;}/// summary/// 打印逻辑/// /summary/// param namesender/param/// param namee/paramprivate void btnPrint_Click(object sender, EventArgs e){ListStudent studentPrintList new ListStudent();studentPrintList.Add(new Student() { Name张三, Age29});studentPrintList.Add(new Student() { Name 李四, Age 28 });int currentPageNumber 0;//遍历循环打印foreach (var item in studentPrintList){currentPageNumber;//设置页码信息SetPageInfo(currentPageNumber, studentPrintList.Count);this.txtName.Text item.Name;this.txtSex.Text item.Age;printDialog.ShowDialog();}}/// summary/// 调用printDialog.ShowDialog()方法就会自动调该方法进行打印/// /summary/// param namesender/param/// param namee/paramprivate void PrintDocument_PrintPage(object sender, PrintPageEventArgs e){PrintStudentInfo(e.Graphics, this.printArea);}/// summary/// 打印控件/// /summary/// param namegraphics/param/// param nameprintControl/parampublic void PrintStudentInfo(Graphics graphics,Control printControl){Bitmap bitmap new Bitmap(printControl.Width, printControl.Height);printControl.DrawToBitmap(bitmap, new Rectangle(0,0, bitmap.Width, bitmap.Height));Rectangle target new Rectangle(0, 0, bitmap.Width, bitmap.Height);graphics.PageUnit GraphicsUnit.Display;graphics.DrawImage(bitmap, target);}/// summary/// 设置页码信息/// /summary/// param namecurrentPageNumber/param/// param nametotalPageNumber/paramprivate void SetPageInfo(int currentPageNumber,int totalPageNumber){this.lblPageInfo.Text string.Format(第{0}页/总{1}页,currentPageNumber,totalPageNumber);}}
}代码太简单了就不解释了 4 点击打印按钮运行效果如下 关掉预览框后会接着弹出打印第二页的预览框如下图 好了本水文到此结束。如有疑问可以通过邮箱联系2847225301qq.com