测试打印功能
// 先创建窗体,后拉入Button控件两个,一个预览功能,一个打印功能,再拉入控件PrintDocument控件、PrintPreviewDilogs控件、pageSetupDilogs控件
// 后台代码
1 List<string> students = new List<string>() {"张三","李四","王五" }; 2 int count = 0;//要打印的学员姓名的下标值 3 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 4 { 5 Graphics g = e.Graphics; 6 string name = students[count]; 7 g.DrawString(name,new Font("宋体",20),Brushes.Black,100,100); 8 //g.DrawLine(new Pen(Color.Red),new Point(200,200),new Point(300,200)); 9 g.Dispose(); 10 count++;//递增,为了打印下一个姓名 11 //判断打印是否结束 12 if (count == students.Count) 13 { 14 e.HasMorePages = false;//不会打印下一页 15 } 16 else 17 { 18 e.HasMorePages = true;//继续打印下一页 19 } 20 } 21 22 //预览按钮 23 private void btnPrev_Click(object sender, EventArgs e) 24 { 25 this.printPreviewDialog1.Document = this.printDocument1; 26 this.printPreviewDialog1.ShowDialog(); 27 } 28 29 // 打印按钮 30 private void btnPrint_Click(object sender, EventArgs e) 31 { 32 this.pageSetupDialog1.Document = this.printDocument1; 33 if(this.pageSetupDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 34 { 35 this.printDocument1.Print();//打印 36 } 37 }