Microsoft KB Archive/317840

= How To Change Print Settings for Internet Explorer and the WebBrowser Control Programmatically in Managed C++ =

Article ID: 317840

Article Last Modified on 4/21/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Internet Explorer 5.5

-



This article was previously published under Q317840



This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * Microsoft.Win32

IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry

IN THIS TASK
SUMMARY
 * Description of the Technique
 * Visual C++ .NET Code to Modify the Registry Key

REFERENCES



SUMMARY
This step-by-step article describes how to programmatically set the page margins, header, and footer to print from Internet Explorer and the WebBrowser control in Managed C++ Application code.

back to the top

Description of the Technique
Users can easily change Internet Explorer printer settings for the page margins, header, and footer by using the Internet Explorer user interface. However, Internet Explorer and the WebBrowser control do not include methods to change these settings programmatically.

Note that you cannot use an ExecWB call to set the page margins, header, or footer. These values are stored in the registry.

To programmatically change the printer settings for Internet Explorer or the WebBrowser control, you can change only the page margins, the header information, and the footer information. You cannot programmatically change other settings such as the page orientation or the default printer.

This is how Internet Explorer accesses the printer settings:  Internet Explorer tries to obtain the values from the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

 If this key does not exist, Internet Explorer tries to create this key by copying the values from the following key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\PageSetup

 If that key does not exist, default values are provided.

Note These registry values affect all instances of the WebBrowser control and Internet Explorer for the current user.

back to the top

Visual C++ .NET Code to Modify the Registry Key
WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.

This sample Managed C++ Application code describes how to modify the required registry key for the footer:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the Project Type box, click Visual C++ Projects, and then click Managed C++ Applicationfor Visual Studio .NET 2002 or Console Application (.NET) for Visual Studio .NET 2003 in the Templates box.</li> In the Name box, type RegistryChange .</li> In the Solution panel on the right, double-click the RegistryChange.cpp file to open the code window.</li>  Delete all of the code in the window, and then paste the following code into the window:
 * 1) include &quot;stdafx.h&quot;
 * 2) using <mscorlib.dll>
 * 3) include <tchar.h>

using namespace System; using namespace Microsoft::Win32;

// This is the entry point for this application. int _tmain(void) {   RegistryKey __gc* regKey = Registry::CurrentUser; String __gc* strKey =  &quot;Software\\Microsoft\\Internet Explorer\\PageSetup&quot;; bool bolWritable = true; String __gc* strName = &quot;footer&quot;; Object*oValue = S&quot;Hello Footer&quot;; RegistryKey __gc* oKey = regKey->OpenSubKey(strKey,bolWritable);

Console::Write(strKey); oKey->SetValue(strName,oValue); oKey->Close; return 0; }                   </li> Press F5 to run the application.</li></ol>

NOTE: Your application must have Read and Write permissions for the registry key.

back to the top

<div class="references_section">