更改PrintDialog框中显示的份数

时间:2009-07-27 08:00:07

标签: c# winforms printing crystal-reports

我正在使用Crystal Reports for Visual Studio 2005.我需要更改默认打印机,并将副本数更改为2,而默认值为1。

我已成功使用以下代码更改默认打印机。

static int SetAsDefaultPrinter(string printerDevice)
{
    int ret = 0;
    try
    {   
        string path = "win32_printer.DeviceId='" + printerDevice + "'";
        using (ManagementObject printer = new ManagementObject(path))
        {
            ManagementBaseObject outParams =
            printer.InvokeMethod("SetDefaultPrinter",
            null, null);
            ret = (int)(uint)outParams.Properties["ReturnValue"].Value;                
        }
    }
}

如何更改打印的份数?

1 个答案:

答案 0 :(得分:1)

.Net Framework不提供任何覆盖默认打印功能的机制。所以我禁用了默认的打印按钮,并为事件处理程序添加了一个按钮名称Print.Code,如下所示。

private void Print_Click(object sender, EventArgs e)
{
    try
    {
        PrintDialog printDialog1 = new PrintDialog();
        PrintDocument pd = new PrintDocument();

        printDialog1.Document = pd;
        printDialog1.ShowNetwork = true;
        printDialog1.AllowSomePages = true;
        printDialog1.AllowSelection = false;
        printDialog1.AllowCurrentPage = false;
        printDialog1.PrinterSettings.Copies = (short)this.CopiesToPrint;
        printDialog1.PrinterSettings.PrinterName = this.PrinterToPrint;
        DialogResult result = printDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            PrintReport(pd);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void PrintReport(PrintDocument pd)
{
    ReportDocument rDoc=(ReportDocument)crvReport.ReportSource;
    // This line helps, in case user selects a different printer 
    // other than the default selected.
    rDoc.PrintOptions.PrinterName = pd.PrinterSettings.PrinterName; 
    // In place of Frompage and ToPage put 0,0 to print all pages,
    // however in that case user wont be able to choose selection.
    rDoc.PrintToPrinter(pd.PrinterSettings.Copies, false, pd.PrinterSettings.FromPage,
       pd.PrinterSettings.ToPage); 
}