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.
Visual C++ .NET code to modify the registry key
This sample Managed C++ Application code describes how to modify the required registry key for the footer.Important This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base:
322756 How to back up and restore the registry in Windows
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In Visual Studio .NET 2002, click Visual C++ Projects in the Project Type box, and then click Managed C++ Application. Or, in Visual Studio .NET 2003, click Console Application (.NET) in the Templates box.
In Visual C++ 2005 or in Visual C++ 2008, click Visual C++ Projects, click CLR, and then click CLR Console Application in the Templates box. - In the Name box, type RegistryChange.
- In the Solution panel on the right, double-click the RegistryChange.cpp file to open the code window.
- In Visual Studio .NET 2003 or in earlier versions of Visual Studio, delete all of the code in the window, and then paste the
following code in the window:In Visual C++ 2005 or in Visual C++ 2008, delete all of the code in the window, and then paste the following code in the window.
#include "stdafx.h" #using <mscorlib.dll> #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 = "Software\\Microsoft\\Internet Explorer\\PageSetup"; bool bolWritable = true; String __gc* strName = "footer"; Object*oValue = S"Hello Footer"; RegistryKey __gc* oKey = regKey->OpenSubKey(strKey,bolWritable); Console::Write(strKey); oKey->SetValue(strName,oValue); oKey->Close(); return 0; }
Note This code uses the new C++/CLI syntax.#include "stdafx.h" #using <mscorlib.dll> #include <tchar.h> using namespace System; using namespace Microsoft::Win32; int main(array<System::String ^> ^args) { RegistryKey^ regKey = Registry::CurrentUser; String^ strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup"; bool bolWritable = true; String^ strName = "footer"; Object^ oValue = "Hello Footer"; RegistryKey^ oKey = regKey->OpenSubKey(strKey,bolWritable); Console::WriteLine(strKey); oKey->SetValue(strName,oValue); oKey->Close(); return 0; }
- Press F5 to run the application.