/**
* 问题1:打印分页的根据是什么?
* 问题2:我怎么知道一共有几页?
* 问题3:每一个分页里要显示的内容如何才能灵活的取到相关内容,而不是用if来判断?
* **/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
printPreviewControl1.StartPageChanged += new EventHandler(printPreviewControl1_StartPageChanged);
}
public static int currentPage = 1;
public static int totalPage = 4;
private void printPreviewControl1_StartPageChanged(object sender,EventArgs e)
{
//只要StartPage发生了改变,就会执行本方法
//MessageBox.Show("预览页发生了变化");
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) //如何判断是否还有下一页?
{
e.Graphics.FillRectangle(Brushes.Azure, e.PageBounds);
e.Graphics.FillRectangle(Brushes.DarkSlateGray, e.MarginBounds);
e.Graphics.DrawImage(Image.FromFile(@"D:\wpftestmusic\"+currentPage+".jpg"), 0, 0);
e.Graphics.DrawLine(new Pen(Color.Red, 3), new Point(0, 0), new Point(50, 100));
e.Graphics.DrawLine(new Pen(Color.Red, 3), new Point(0, 0), new Point(500, 1000));
e.Graphics.DrawLine(new Pen(Color.Red, 3), new Point(0, 0), new Point(500, 2000));
e.HasMorePages = true;
if (currentPage < totalPage+1)
{
//MessageBox.Show(currentPage.ToString());
currentPage++;
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
private void 打印PToolStripButton_Click(object sender, EventArgs e)
{
printDocument1.Print(); //直接调用打印文档的打印方法
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e) //调整打印纸张的一些属性
{
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("A4", 400, 800); //调整打印参数
printDocument1.DefaultPageSettings.Landscape = false; //false为纵向
printPreviewControl1.UseAntiAlias = true;
}
private void p1_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage = 1; //这边很重要的一点,委托和事件实现了动作的自动化发生
}
private void p2_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage = 2;
}
private void p3_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage = 3;
}
private void p4_Click(object sender, EventArgs e)
{
printPreviewControl1.StartPage = 4;
}
}
}