7
我想在打印文档前显示打印对话框,因此用户可以在打印前选择另一台打印机。打印的代码是:打印前显示打印对话框
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);
}
此代码是否能够打印当前表单?
时按下按钮,打印对话框不开,但在MessageBox显示打印取消显示 – user2257581
@ user2257581:我现在测试它,它工作,创建一个新的应用程序,并再次测试,看到它的工作 – KF2
在64位Windows和一些版本的.NET,你可能必须设置'pdi.UseExDialog = true;'出现对话窗口。有关详细信息,请参阅http://stackoverflow.com/q/6385844/202010。 –