[英]Modifying PrinterSettings after PrintDialog was shown
我正在嘗試修改System.Drawing.Printing.PrinterSettings對象,該對象是在向用戶顯示對話框后從System.Windows.Forms.PrintDialog獲取的。 雖然我可以在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對象就沒關系),我將非常感激。
不知道為什么你的問題得到了投票,對我來說相當合理????
無論如何,我注意到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 - 如果需要,可以從上面的dialgues中創建自己的包裝器代碼。
祝好運。
來自Aspose文檔:
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
awPrintDoc.PrinterSettings = printDlg.PrinterSettings;
因此,您似乎可以將yuor修改后的PrinterSettings對象傳遞給您嘗試打印的word文檔。 你能告訴我這是否有效嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.