在向用户显示对话框之后,我尝试修改从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对象进行打印。我使用第三方dll Aspose.Words来做这件事,因为我需要打印Word,但这似乎不是问题。似乎在显示对话框后,所有设置都已提交到打印机,更改PrinterSettings没有任何效果。你有什么办法让它工作吗?
编辑:我有一些解决方法。我在这里想要得到的是这些特定问题的答案:在显示对话框后是否可以更改PrinterSettings对象,以及在打印时是否考虑了这些更改。如果有人只知道这是如何工作的一种方法(您可以决定使用哪种API进行打印,只要使用PrinterSettings对象就无所谓了),我将非常感激。
发布于 2012-03-17 17:32:19
不知道为什么你的问题投了反对票,看起来很合理?
总之,我在使用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-15 00:20:15
来自Aspose文档:
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
awPrintDoc.PrinterSettings = printDlg.PrinterSettings;因此,您似乎可以将您或修改后的PrinterSettings对象传递给您正在尝试打印的word文档。你能告诉我这是否有效吗?
https://stackoverflow.com/questions/9704671
复制相似问题