【问题标题】:Update controls while printing using printdocument in Winforms在 Winforms 中使用 printdocument 打印时更新控件
【发布时间】:2018-12-28 14:28:51
【问题描述】:

我正在打印一个包含所有控件的面板。我的问题是我想在此面板内创建一个计数器并为打印的每一页增加其值。我尝试在我的 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 的标签(在 panel1 内)。

既然我这样做了:

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

我能做到吗:

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

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

【问题讨论】:

    标签: c# winforms printing


    【解决方案1】:

    您需要在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

    【讨论】:

    • 谢谢,我解决了我的问题,将 PrintDocument.DefaultPageSettings.PrinterSettings.Copies 设置为 1 和 if (nbrPrint >= nbrPages) e.HasMorePages = false;否则 e.HasMorePages = true;
    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多