Let me explain my dilemma. In my C++/MFC application I'm using a WebBrowser control (which is basically an IE active-x control) to display HTML content.
My goal is to provide a one-click Print
button for a user, which will print the HTML content from the WebBrowser control
, assuming specific print options, such as page size, margins, orientation, print quality, etc. that could be configured in the Options
page for my app.
Unfortunately WebBrowser control
doesn't seem to be supporting a lot of printing customization. So I have to resort to "workarounds":
I can achieve some of the WebBrowser control
's customization via changes in the system registry (I know, it's not a perfect solution, but at least it's a working one.) And while I'm at it, I can also quickly change the default printer to the one requested in the user settings for my program, print the page, and reset the default printer back to what it used to be.
It's all done pretty much in a method as such:
//Pseudo-code
//1. Remember old default printer by calling GetDefaultPrinter()
//2. Remember old values in 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup' registry key
//3. Set page margins by changing 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup' registry key
//4. Set default printer by calling SetDefaultPrinter()
//5. Print contents of the WebBrowser control to a default printer
// w/o showing IE's print window, but make sure to call it synchronously:
COleVariant varNull;
COleVariant var1;
var1.vt = VT_I2;
var1.iVal = PRINT_WAITFORCOMPLETION; //Value of 2
m_browser.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, var1, varNull);
//6. Restore values in 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup' registry key
//7. Restore old default printer by calling SetDefaultPrinter()
So this works for paper margins. What I need to accomplish is to change print settings for a default printer, stuff such as paper size, orientation and quality. I know that it's usually done via the DEVMODE struct.
So can I change DEVMODE
-type settings for a default printer on a global scale?