使用打印对话框设置在winforms中打印所需的选项

huus2vyu  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(110)

我想在打印面板之前显示打印对话框,然后在打印文档中实现选定的选项。这些选项是像方向,副本数量,页面大小(leter,A4,A3,...)。我如何使用选定的optionS?我的代码现在就像我的Winforms C#项目中的以下内容:

PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();
    
private void button2_Click_1(object sender, EventArgs e)
{
    dialog.Document = document;
    if (dialog.ShowDialog() == DialogResult.OK)
    {
    
          PrintDocument doc = new PrintDocument();
          doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
          doc.Print();
    }
    else
    {
        MessageBox.Show("Print Cancelled!");
    }
    
}
    
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
     Panel grd = splitContainer1.Panel2;
     Bitmap bmp = new Bitmap(grd.Width, grd.Height, grd.CreateGraphics());
     grd.DrawToBitmap(bmp, new Rectangle(0, 100, grd.Width, grd.Height));
     System.Drawing.RectangleF bounds = e.PageSettings.PrintableArea;
     float factor = ((float)bmp.Height / (float)bmp.Width);
     e.Graphics.DrawImage(bmp, bounds.Left + 20, bounds.Top, bounds.Width -40, factor * bounds.Width);
}

字符串
实际上,上面的代码显示了打印对话框,但在打印页面中没有实现任何内容。

pkbketx9

pkbketx91#

要获取对话框窗口中的任何更改,您可以查看PrintDialog。然后您必须在doc.Print行之前设置PrintDocument中的更改,如下所示:

doc.PrinterSettings = dialog.PrinterSettings;

字符串

相关问题