C# 连续多页打印打到同一页问题

.Net技术 码拜 9年前 (2015-11-24) 2408次浏览
尝试一个多页打印的程序,第一次打印是正常的,第二次再点按钮打印时,就会把前两页的内容打到同一页上?

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;
namespace 打印多页
{
    public partial class Form1 : Form
    {
        private int Pages;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)//同样的问题,连续打印时,第二次打印会把1,2页打印到同一页面
        {
            Pages = 0;
            printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
            printDocument1.Print();
        }
        void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //throw new NotImplementedException();
            e.HasMorePages = true;
            Pages++;
            if (Pages == 1)
            {
                Image MyPic = Image.FromFile("相片.JPG");
                e.Graphics.DrawImage(MyPic, 20, 40);
            }
            if (Pages == 2)
            {
                e.Graphics.FillPie(new SolidBrush(Color.AliceBlue), new Rectangle(40, 60, 300, 100), 30, 60);
            }
            if (Pages == 3)
            {
                e.HasMorePages = false;
                e.Graphics.FillRectangle(new SolidBrush(Color.BurlyWood),new Rectangle(46,20,600,400));
            }
            if (Pages > 3)
                e.HasMorePages = false;
        }
    }
}
解决方案:10分
第2次点击时事件绑定了2次,printDocument1_PrintPage也会执行2次,明白?
解决方案:30分
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
这个有点问题吧  每点击一次都会在事件上注册了这个方法 这样感觉不行
printDocument1_PrintPage  在这个方法完成后 你要注销下这个方法

printDocument1.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage)


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 连续多页打印打到同一页问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)