显示打印对话框打印前(Show Print Dialog before printing)

2019-08-22 16:41发布

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

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);
        }

将这段代码可以打印当前的形式?

Answer 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 ; 对于出现的对话框窗口。



Answer 2:

为了完整起见,该代码应包括使用指令

using System.Drawing.Printing;

进一步参考请转到PrintDocument类



文章来源: Show Print Dialog before printing