execCommand(“Print”, false, IDon'tUnderstandTh

2019-07-26 17:47发布

问题:

What I'm trying to do is get this to print in landscape mode without showing the dialog box. This is what I have so far:

((mshtml.IHTMLDocument2)Browser.Document.DomDocument).execCommand("Print", true, 0);

I know that the instruction to print in landscape mode needs to be sent through the third argument, but I don't know how to construct the third argument to do this. Can anyone give me some help on how to make this last argument accomplish my goal?

回答1:

ADDITIONS: This is from Microsoft http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.defaultpagesettings%28v=vs.71%29.aspx

public void Printing()
{
   try
   {
      streamToPrint = new StreamReader (filePath);
      try
      {
         printFont = new Font("Arial", 10);
         PrintDocument pd = new PrintDocument(); 
         pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
         pd.PrinterSettings.PrinterName = printer;
         // Set the page orientation to landscape.
         pd.DefaultPageSettings.Landscape = true;
         pd.Print();
      } 
      finally
      {
         streamToPrint.Close() ;
      }
   } 
   catch(Exception ex)
   { 
      MessageBox.Show(ex.Message);
   }
}

So you would probably just replace pd in the example above with your document.


ORIGINAL ANSWER: Possibly integrate:

DefaultPageSettings.Landscape = true;

So maybe something like this:

((mshtml.IHTMLDocument2)Browser.Document.DomDocument).DefaultPageSettings.Landscape = true;
((mshtml.IHTMLDocument2)Browser.Document.DomDocument).execCommand("Print", true, 0);

Taking a little bit of a stab in the dark since C# is a weak spot but I believe this is how I once did it from an C# application.



回答2:

I mostly stick to desktop apps and I don't know how to do what you want. But I did take a quick look at the msdn documentation, and it seems the "Print" command (IDM_EXECPRINT) may not be what you're looking for? According to the documentation for the method, the second parameter should be false if you do not want to display a user interface. That sounds fine except that it looks like the IDM_EXECPRINT command always displays a dialog regardless ("User interface: Yes. Set parameter to true or omit").

msdn documentation: IHTMLDocument2::execCommand Method, IDM_EXECPRINT

Someone please correct me if I'm wrong, but I think you may want to look for another command.

Edit: You might have better luck with the IDM_PRINT command from mshtmcid.h, the dialog is optional for this command. Here's a sample application (in C++): http://msdn.microsoft.com/en-us/library/bb250434(VS.85).aspx