5

在向用户显示对话框后,我正在尝试修改从 System.Windows.Forms.PrintDialog 获得的 System.Drawing.Printing.PrinterSettings 对象。尽管我能够更改 PrinterSettings 对象的属性值,但在打印文档时实际上并未考虑我在显示对话框后所做的任何更改。

这是我的意思的一个例子:

//Show the printdialog and retreive the printersettings    
var printDialog = new PrintDialog();
if (printDialog.ShowDialog() != DialogResult.OK) 
            return;
var printerSettings = printDialog.PrinterSettings;

//Now modify the printersettings object
printerSettings.ToPage = 8;

现在使用printerSettings 对象进行打印。我为此使用了 3rd Party dll Aspose.Words,因为我需要打印 Word,但这似乎不是问题。似乎在显示对话框后,所有设置都已提交给打印机,而更改 PrinterSettings 没有任何效果。关于如何让它发挥作用的任何想法?

编辑:我有一些解决方法。我想在这里得到这些特定问题的答案:是否可以在显示对话框后更改 PrinterSettings 对象,以及在打印时是否考虑了这些更改。如果有人只知道一种工作方式(您可以决定要使用什么 API 进行打印,只要使用 PrinterSettings 对象就没有关系),我将非常感激。

4

2 回答 2

2

不知道为什么你的问题被否决了,对我来说似乎很合理????

无论如何,我注意到 PrintDialog 的一些事情(可能会或可能不会回答您的问题)。

首先,它只是 windows com 对话的一个包装类。

[DllImport("comdlg32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern bool PrintDlg([In, Out] NativeMethods.PRINTDLG lppd);

第二,关于你的问题,我猜是最重要的: PrintDialog 类有这个例程,在 PrintDlg 调用关闭后调用

if (!UnsafeNativeMethods.PrintDlg(data))
                return false;

            IntSecurity.AllPrintingAndUnmanagedCode.Assert();
            try { 
                UpdatePrinterSettings(data.hDevMode, data.hDevNames, data.nCopies, data.Flags, settings, PageSettings); 
            }
            finally { 
                CodeAccessPermission.RevertAssert();
            }

. . .

// VSWhidbey 93449: Due to the nature of PRINTDLGEX vs PRINTDLG, separate but similar methods 
// are required for updating the settings from the structure utilized by the dialog.
// Take information from print dialog and put in PrinterSettings
private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, int flags, PrinterSettings settings, PageSettings pageSettings) {
        // Mode 
        settings.SetHdevmode(hDevMode);
        settings.SetHdevnames(hDevNames); 

        if (pageSettings!= null)
            pageSettings.SetHdevmode(hDevMode); 

        //Check for Copies == 1 since we might get the Right number of Copies from hdevMode.dmCopies...
        //this is Native PrintDialogs
        if (settings.Copies == 1) 
            settings.Copies = copies;

        settings.PrintRange = (PrintRange) (flags & printRangeMask); 
    }

这里还有一个相当有趣的相互作用(记住您设置了 PrinterSettings.ToPage):

public PrinterSettings PrinterSettings {
   get { 
        if (settings == null)
        {
            settings = new PrinterSettings(); 
        }
        return settings; 
    } 
    set {
        if (value != PrinterSettings) 
        {
            settings = value;
            **printDocument = null;**
        } 
    }
} 

进而

public PrintDocument Document {
            get { return printDocument;}
            set {
                printDocument = value; 
                **if (printDocument == null)
                    settings = new PrinterSettings();** 
                else 
                    settings = printDocument.PrinterSettings;
            } 
        }

我知道这不是一个直接的答案,但我认为应该为您指出为什么它不起作用的正确方向。在我看来,在对话的使用过程中,它可以愉快地取消更改设置,因为它将在完成时重新创建,但是当对话完成时,更改设置实际上会使文档打印设置无效,直到再次设置。可以手动执行此操作,或者它可能会被 M$ 以通常的内部/私有方式锁定在许多内部。

当然有一个选项(不是我知道的那么好)在调用之后直接使用 Win API - 如果需要,可以从上面的对话中提取代码来构建自己的包装器。

祝你好运。

于 2012-03-17T09:32:19.193 回答
1

来自 Aspose 文档:

AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
awPrintDoc.PrinterSettings = printDlg.PrinterSettings;

因此,您似乎可以将修改后的 PrinterSettings 对象传递给您尝试打印的 word 文档。你能告诉我这是否有效吗?

于 2012-03-14T16:20:14.777 回答