当前位置: 代码迷 >> C# >> 生手编程C#MyPrintDocument_PrintPage不受控多次循环,求大能们指点
  详细解决方案

生手编程C#MyPrintDocument_PrintPage不受控多次循环,求大能们指点

热度:286   发布时间:2016-04-28 08:39:42.0
新手编程C#MyPrintDocument_PrintPage不受控多次循环,求大能们指点。
本帖最后由 skingcai 于 2015-10-21 20:42:49 编辑
我用listview做数据源,第一次调用F1时正常,第二次F1时MyPrintDocument_PrintPage自执行两次,第三次调用时MyPrintDocument_PrintPage自己走了三次才出一张纸
        private void F1()
        {
            double xpage = 0, xz = 16, xy = lv.Items.Count;
            xpage = xy / xz;
            xpage += 0.999999999999999;
            printPage.Tnumpage = (int)xpage;
            if (printPage.Tnumpage > 1)
                printPage.Ismore = true;
            printPage.Cpage = 1;
            printPage.Crow = 0;
            printPage.Endpage = lv.Items.Count % 16;
            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 854, 547);
            this.printDocument1.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage);
            this.printDocument1.PrinterSettings.PrinterName = serarr.Print;
                this.printDocument1.Print();
            lv.Items.Clear();
            printPage.Ismore = false;
            printPage.Cpage = 1;
            printPage.Crow = 0;
            this.printDocument1.Dispose();
        }
        private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if(printPage.Ismore)
                e.HasMorePages = true;
            #region                画图开始
            int top = 20;
            int left = 50;
            // 显示LOGO image.
            Image imageFile = Image.FromFile("LOGO.jpg");
            Graphics newGraphics = Graphics.FromImage(imageFile);
            newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);
            e.Graphics.DrawImage(imageFile, left + 25, top + 25, 196, 50);
            e.Graphics.DrawString("XXXXX小票", new Font(new FontFamily("黑体"), 22, FontStyle.Bold), System.Drawing.Brushes.Black, left + 210, top + 32);
            e.Graphics.DrawString("XXXXX点", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, left + 610, top + 50);
            //信息的名称
            e.Graphics.DrawLine(Pens.Black, left + 0, top + 70, left + 730, top + 70);
            e.Graphics.DrawString("商品条码", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 35, top + 75);
            e.Graphics.DrawString("品名", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 150, top + 75);
            e.Graphics.DrawString("数量", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 250, top + 75);
            e.Graphics.DrawString("零售价", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 350, top + 75);
            e.Graphics.DrawString("折扣", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 450, top + 75);
            e.Graphics.DrawString("现价", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 530, top + 75);
            e.Graphics.DrawString("实收金额", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 610, top + 75);
            //内容
            int pagerow = printPage.Cpage * 16;
            if (printPage.Cpage == printPage.Tnumpage && printPage.Endpage > 0)
                pagerow = pagerow - 16 + printPage.Endpage;
            for (int i = top + 95; printPage.Crow < pagerow; printPage.Crow++, i += 20)
            {
                e.Graphics.DrawString(lv.Items[printPage.Crow].SubItems[1].Text, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 35, i);
                e.Graphics.DrawString(lv.Items[printPage.Crow].SubItems[3].Text, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 150, i);
                e.Graphics.DrawString(lv.Items[printPage.Crow].SubItems[4].Text, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 250, i);
                e.Graphics.DrawString(lv.Items[printPage.Crow].SubItems[5].Text, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 350, i);
                e.Graphics.DrawString(lv.Items[printPage.Crow].SubItems[10].Text, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 450, i);
                e.Graphics.DrawString(lv.Items[printPage.Crow].SubItems[7].Text, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 530, i);
                e.Graphics.DrawString(lv.Items[printPage.Crow].SubItems[8].Text, new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, left + 610, i);
            }
            //页脚
            e.Graphics.DrawString("合计件数:" + printarr.Num.ToString(), new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, left + 230, top + 425);
            e.Graphics.DrawString("应付:" + printarr.Yingfu.ToString() + "   优惠:" + printarr.Youhui.ToString() + "   实收:" + printarr.Shifu.ToString(), new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, left + 310, top + 425);
            e.Graphics.DrawString("商品一经销售,不退不换", new Font(new FontFamily("黑体"), 11, FontStyle.Bold), System.Drawing.Brushes.Black, left + 550, top + 420);
            e.Graphics.DrawLine(Pens.Black, left + 0, top + 440, left + 730, top + 440);
            e.Graphics.DrawString("交易时间:" + DateTime.Now.ToString(), new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, left + 35, top + 442);
            e.Graphics.DrawString("销售单号:" + printarr.Did, new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, left + 370, top + 442);
            e.Graphics.DrawString("第" + printPage.Cpage.ToString() + "/" + printPage.Tnumpage.ToString() + "页", new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, left + 670, top + 442);
            e.Graphics.DrawString("欢迎下次光临", new Font(new FontFamily("黑体"), 11, FontStyle.Bold), System.Drawing.Brushes.Black, left + 580, top + 460);
            #endregion
            if (printPage.Ismore && printPage.Cpage < printPage.Tnumpage)
                printPage.Cpage++;            
            else            
                e.HasMorePages = false;
        }

------解决思路----------------------
因为你只有+=没有-=,所以每次调用F1都会注册一个新的事件监听
  相关解决方案