View RSS Feed

benrwb

Winforms WebBrowser control

Rate this Entry
by , 11th April 2011 at 01:06 PM (5934 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"...
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);
To show the "Page Setup" dialog...
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:
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);
(The first three lines are same every time, it's only the last line that's different).
Tags: coding, visual c#
Categories
Uncategorized

Comments

Trackbacks

Total Trackbacks 0
Trackback URL: