打印从WebBrowser控件在设置默认值后打印到错误的打印机

时间:2010-04-01 14:27:02

标签: c# .net vb.net winforms webbrowser-control

我在VB.NET WinForms应用程序中有一个WebBrowser控件。我能够在不提示用户的情况下从代码和打印设置默认打印机。但是,还有一个显示PrintDialog的打印按钮。如果首先执行此操作,则将打印页面。然后,如果我尝试以后以编程方式打印(再次将默认打印机设置为其他打印​​机),它将打印到PrintDialog框中选择的最后一台打印机,即使我正在重置默认打印机并看到在Windows中更改了默认打印机。 / p>

有什么想法吗?

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

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

        browser.Print()

2 个答案:

答案 0 :(得分:5)

我能够使用以下代码,而无需打开/关闭单独的表单。 自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;
      }
   }
}

答案 1 :(得分:0)

我创建了一个没有任何内容的表单。启动/关闭此表单的操作使其转到正确的打印机...我不知道为什么这样可行,但必须有一些函数可以调用来模拟那里发生的任何操作。