Change number of copies displayed in PrintDialog box

别说谁变了你拦得住时间么 提交于 2020-01-05 04:58:28

问题


I am working on Crystal Reports for Visual Studio 2005. I need to change the default printer, and the number of copies to 2 as compared to the default of 1.

I have succeeded to change the default printer using below code.

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;                
        }
    }
}

How can I change the number of copies printed?


回答1:


.Net Framework doesn't provide any mechanism to override the default print functionality. So I disabled the default print button, and added a button name Print.Code for the Event Handler follows below.

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); 
}


来源:https://stackoverflow.com/questions/1186970/change-number-of-copies-displayed-in-printdialog-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!