Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

c# - Printing From WebBrowser control prints to wrong printer after setting default

I have a WebBrowser control in a VB.NET WinForms app. I am able to set the default printer from code and print without prompting the user. However, there is also a print button that shows the PrintDialog. If this action is done first the page will print. Then if I try to programmatically print later (again setting the default printer to some other printer) the it will print to the last printer selected in the PrintDialog box even though I am resetting the default and see the default printer being changed in Windows.

Any ideas?

It works fine unless ShowPrintDialog has a printer chosen first. Once that occurs it seems to always use that printer no matter what I do.

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

        browser.Print()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was able to get the following code to work, without having to open/close a separate form. I've only been looking for this since IE6...

See also these two posts. 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;
      }
   }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...