简体   繁体   English

打印前显示打印对话框

[英]Show Print Dialog before printing

I want to show the print dialog box before printing the document, so the user can choose another printer before printing. 我想在打印文档之前显示打印对话框,因此用户可以在打印前选择其他打印机。 The code for printing is: 打印代码是:

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

will this code be able to print the current form? 这段代码能够打印当前表格吗?

You have to use PrintDialog 你必须使用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");
 }

Edited (from Comment) 编辑 (来自评论)

On 64-bit Windows and with some versions of .NET you may have to set pdi.UseExDialog = true ; 64-bit Windows和某些版本的.NET上,您可能必须设置pdi.UseExDialog = true ; for the dialog window to appear. 用于显示对话框窗口。

For the sake of completeness, the code should include a using directive 为了完整起见,代码应包含using指令

using System.Drawing.Printing;

for further reference please goto PrintDocument Class 如需进一步参考,请转到PrintDocument类

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

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