2014-07-06 73 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