c# – 打印前显示打印对话框

我想在打印文档之前显示打印对话框,因此用户可以在打印前选择其他打印机.打印代码是:
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);
        }

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

解决方法

你必须使用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;用于显示对话框窗口.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...