gpt4 book ai didi

c# - 在显示 PrintDialog 后修改 PrinterSettings

转载 作者:太空狗 更新时间:2023-10-29 23:33:31 27 4
gpt4 key购买 nike

在向用户显示对话框后,我试图修改从 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 对象就没关系),我将非常感激。

最佳答案

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

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

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

关于c# - 在显示 PrintDialog 后修改 PrinterSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9704671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com