gpt4 book ai didi

c# - 设置默认值后,从 WebBrowser 控件打印到错误的打印机

转载 作者:太空狗 更新时间:2023-10-29 23:19:11 26 4
gpt4 key购买 nike

我在 VB.NET WinForms 应用程序中有一个 WebBrowser 控件。我可以通过代码设置默认打印机并在不提示用户的情况下进行打印。但是,还有一个显示 PrintDialog 的打印按钮。如果此操作先完成,页面将打印出来。然后,如果我稍后尝试以编程方式打印(再次将默认打印机设置为其他打印​​机),它将打印到在 PrintDialog 框中选择的最后一台打印机,即使我正在重置默认值并看到默认打印机在 Windows 中被更改。

有什么想法吗?

它工作正常,除非 ShowPrintDialog 首先选择打印机。一旦发生这种情况,无论我做什么,它似乎总是使用那台打印机。

For Each strPrinter In PrinterSettings.InstalledPrinters
If strPrinter.Contains("My Printer") Then
wScript.SetDefaultPrinter(strPrinter)
End If
Next

browser.Print()

最佳答案

我能够使以下代码正常工作,而无需打开/关闭单独的表单。我从 IE6 开始就一直在寻找这个...

另请参阅这两个帖子。 Programmatically changing the destination printer for a WinForms WebBrowser control

Print html document from Windows Service without print dialog

 // Add references for: COM:  Microsoft Internet Controls; .NET: System.Management.dll
using System;
using System.Reflection;
using System.Threading;
using SHDocVw;
using System.Windows.Controls;
using System.Management;

namespace HTMLPrinting
{
public class HTMLPrinter
{
private bool documentLoaded;
private bool documentPrinted;
private string originalDefaultPrinterName;

private void ie_DocumentComplete(object pDisp, ref object URL)
{
documentLoaded = true;
}

private void ie_PrintTemplateTeardown(object pDisp)
{
documentPrinted = true;
}

public void Print(string htmlFilename, string printerName)
{
// Preserve default printer name
originalDefaultPrinterName = GetDefaultPrinterName();
// set new default printer
SetDefaultPrinter(printerName);
// print to printer
Print(htmlFilename);
}

public void Print(string htmlFilename)
{
documentLoaded = false;
documentPrinted = false;

InternetExplorer ie = new InternetExplorer();
ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);

object missing = Missing.Value;

ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
Thread.Sleep(100);

ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);

// Wait until the IE is done sending to the printer
while (!documentPrinted)
Thread.Sleep(100);

// Remove the event handlers
ie.DocumentComplete -= ie_DocumentComplete;
ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
ie.Quit();

// reset to original default printer if needed
if (GetDefaultPrinterName() != originalDefaultPrinterName)
{
SetDefaultPrinter(originalDefaultPrinterName);
}
}

public static string GetDefaultPrinterName()
{
var query = new ObjectQuery("SELECT * FROM Win32_Printer");
var searcher = new ManagementObjectSearcher(query);

foreach (ManagementObject mo in searcher.Get())
{
if (((bool?)mo["Default"]) ?? false)
{
return mo["Name"] as string;
}
}

return null;
}

public static bool SetDefaultPrinter(string defaultPrinter)
{
using (ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"))
{
using (ManagementObjectCollection objectCollection = objectSearcher.Get())
{
foreach (ManagementObject mo in objectCollection)
{
if (string.Compare(mo["Name"].ToString(), defaultPrinter, true) == 0)
{
mo.InvokeMethod("SetDefaultPrinter", null, null);
return true;
}
}
}
}
return true;
}
}
}

关于c# - 设置默认值后,从 WebBrowser 控件打印到错误的打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561078/

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