Winforms WebBrowser control
by , 11th April 2011 at 01:06 PM (3040 Views)
I've been playing around with the WinForms WebBrowser control this morning, and after lots of experimentation have finally figured out how to use some of the printing functionality.
First of all, Add a Reference to SHDocVw. (Project > Add Reference > Browse > C:\Windows\System32\shdocvw.dll)
Secondly load a document into the browser using webBrowser1.Navigate(url).
Then,
to do "Print Preview"...
To show the "Page Setup" dialog...Code:IWebBrowser2 browser = (IWebBrowser2)webBrowser1.ActiveXInstance; object pvaIn = Missing.Value; object pvaOut = Missing.Value; browser.ExecWB(OLECMDID.OLECMDID_PRINTPREVIEW, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref pvaIn, ref pvaOut);
Code:IWebBrowser2 browser = (IWebBrowser2)webBrowser1.ActiveXInstance; object pvaIn = Missing.Value; object pvaOut = Missing.Value; browser.ExecWB(OLECMDID.OLECMDID_PAGESETUP, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref pvaIn, ref pvaOut);
To Print straight away, without prompting the user:
(The first three lines are same every time, it's only the last line that's different).Code:IWebBrowser2 browser = (IWebBrowser2)webBrowser1.ActiveXInstance; object pvaIn = Missing.Value; object pvaOut = Missing.Value; browser.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref pvaIn, ref pvaOut);



Email Blog Entry

