繁体   English   中英

在Winforms中使用printdocument打印时更新控件

[英]Update controls while printing using printdocument in Winforms

我正在打印一个面板及其所有控件。 我的问题是我想在此面板内做一个计数器,并增加其在每打印页面上的价值。 我试图在我的PrintPage事件中增加计数器,但这不起作用。 是否可以为每个页面添加多个带有增加计数器的printpage事件? 这是我不工作的代码。 感谢您给我有关如何进行此打印的建议...

 private void PrintPage(object o, PrintPageEventArgs e)

    {
        try
        {
            if (File.Exists(toPrint) && nbrPrint < nbrPages)
            {


                Rectangle m = panel1.ClientRectangle;


                Bitmap imaag = new Bitmap(m.Width, m.Height);
                panel1.DrawToBitmap(imaag, m);

                e.Graphics.DrawImage(imaag, e.MarginBounds);
                nbrPrint++;
                compteurPrint++;
                cpt.Text = compteurPrint.ToString();
            }
        }
        catch (Exception)
        {

        }
    }

nbrPrint是要打印的页面,nbrPages是要打印的页面数,compteurPrint是我需要在页面中打印的值(需要增加),cpt是显示compteurPrint的标签(Inside panel1)。

由于我这样做:

 pd.PrintPage += PrintPage;
 pd.Print();

我可不可以做:

while (nbrPrint < nbrPages)
{
     pd.PrintPage += PrintPage;
     compteurPrint++;
}
pd.Print();

我认为这将与我目前正在做的结果相同...谢谢您的帮助!

您需要在PrintPageEventArgs上设置HasMorePages属性,以告诉它还有更多页面要打印。

private void PrintPage(object sender, PrintPageEventArgs e)
{
  try
  {
    if (File.Exists(toPrint) && nbrPrint < nbrPages)
    {
      Rectangle m = panel1.ClientRectangle;


      Bitmap imaag = new Bitmap(m.Width, m.Height);
      panel1.DrawToBitmap(imaag, m);

      e.Graphics.DrawImage(imaag, e.MarginBounds);
      nbrPrint++;
      compteurPrint++;
      cpt.Text = compteurPrint.ToString();
      e.HasMorePages = true; // Set this to true as long as there are more pages to print. It defaults to false.
    }
  }
  catch (Exception)
  {

  }
}

查看Microsoft文档的示例: PrintDocument.PrintPage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM