2014-07-06 35 views
0

我想打印一份表格。我有这个代码,但我不能选择要打印的打印机,而是使用默认打印机打印。我该如何解决这个问题?如何在C#中打印表单?

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); 
} 
private void button1_Click(object sender, EventArgs e) 
{ 
    PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += new PrintPageEventHandler(PrintImage); 

    pd.Print();   
} 
+0

这看起来像winforms,是正确的?我还建议列出至少一个您尝试过的解决方案,以及为什么/如何失败。 – clarkitect

回答

0

答案是从HERE

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

+0

它dosnt工作:( – user3810124

+0

@ user3810124什么不行?你得到一个错误?或者它只是不显示对话框? – coolerfarmer