打印前显示打印对话框

use*_*581 7 c# printdialog

我想在打印文档之前显示打印对话框,因此用户可以在打印前选择其他打印机.打印代码是:

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);
        }
Run Code Online (Sandbox Code Playgroud)

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

KF2*_*KF2 16

你必须使用 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");
 }
Run Code Online (Sandbox Code Playgroud)

编辑(来自评论)

64-bitWindows和某些版本的.NET上,您可能需要设置pdi.UseExDialog = true; 用于显示对话框窗口.

  • 在64位Windows和某些版本的.NET上,您可能需要设置`pdi.UseExDialog = true;`才能显示对话框窗口.有关详细信息,请参见http://stackoverflow.com/q/6385844/202010. (2认同)
  • 不知道为什么我是唯一遇到此问题的人,但是pdi(PrintDialog)对我来说没有Document属性... (2认同)
  • @Shumii 这可能是因为您使用的是来自“PresentationFramework.dll”的“System.Windows.Controls.PrintDialog”,而答案是来自“System.Windows.Forms.dll”的“System.Windows.Forms.PrintDialog”。 (2认同)