【问题标题】:How to print a form in C#? [duplicate]如何在 C# 中打印表单? [复制]
【发布时间】:2014-07-06 18:19:38
【问题描述】:

我想打印一个表格。我有这个代码,但我无法选择要打印的打印机,它使用默认打印机打印。我该如何解决这个问题?

void PrintImage(object o, PrintPageEventArgs e)
{
    int x = SystemInformation.WorkingArea.X;
    int y = SystemInformation.WorkingArea.Y;
    int width = this.Width;
    int height = this.Height;
    Rectangle bounds = new Rectangle(x, y, width, height);
    Bitmap img = new Bitmap(width, height);
    this.DrawToBitmap(img, bounds);
    Point p = new Point(100, 100);          
    e.Graphics.DrawImage(img, p);
}
private void button1_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);

    pd.Print();        
}

【问题讨论】:

  • 这看起来像winforms,对吗?我还建议列出您尝试过的至少一种解决方案以及失败的原因/方式。

标签: c# winforms


【解决方案1】:

答案来自HERE

你必须使用 PrintDialog

 PrintDocument pd = new PrintDocument();
 pd.PrintPage += new PrintPageEventHandler(PrintPage);
 PrintDialog pdi = new PrintDialog();
 pdi.Document = pd;
 if (pdi.ShowDialog() == DialogResult.OK)
 {
     pd.Print();
 }
 else
 {
      MessageBox.Show("Print Cancelled");
 }

在 64 位 Windows 和某些版本的 .NET 上,您可能必须设置 pdi.UseExDialog = true;以显示对话窗口。

【讨论】:

  • @user3810124 什么不起作用?你有错误吗?还是只是不显示对话框?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多