【问题标题】:Show Print Dialog before printing打印前显示打印对话框
【发布时间】:2013-04-05 20:09:04
【问题描述】:

我想在打印文档之前显示打印对话框,以便用户在打印之前选择另一台打印机。打印代码为:

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(PrintImage);
                pd.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ToString());
            }
        }
        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);
        }

这段代码能打印当前的表格吗?

【问题讨论】:

    标签: c# printdialog


    【解决方案1】:

    你必须使用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-bit Windows 和某些版本的 .NET 上,您可能必须设置 pdi.UseExDialog = true;以显示对话窗口。

    【讨论】:

    • 按下按钮时,打印对话框不打开,但显示打印取消的消息框
    • @user2257581:我现在测试它,它工作,创建一个新应用程序并再次测试它,看看它工作
    • 在 64 位 Windows 和某些 .NET 版本中,您可能必须设置 pdi.UseExDialog = true; 才能显示对话框窗口。详情请见stackoverflow.com/q/6385844/202010
    • 不知道为什么我是唯一遇到这种情况的人,但 pdi (PrintDialog) 没有适合我的 Document 属性...
    • @Shumii 这可能是因为您使用的是来自PresentationFramework.dllSystem.Windows.Controls.PrintDialog,而答案是来自System.Windows.Forms.dllSystem.Windows.Forms.PrintDialog
    【解决方案2】:

    为了完整起见,代码应包含 using 指令

    using System.Drawing.Printing;
    

    如需进一步参考,请转到 PrintDocument Class

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多