Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to change print settings for Internet Explorer and the WebBrowser control programmatically in Managed C++


View products that this article applies to.

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.

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.

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
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. In the Name box, type RegistryChange.
  5. In the Solution panel on the right, double-click the RegistryChange.cpp file to open the code window.
  6. 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:
    #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;
    }
    					
    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.

    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;
    }
    
  7. Press F5 to run the application.
Note Your application must have Read and Write permissions for the registry key.

↑ Back to the top


References

For other top-hit Visual C++ .NET Microsoft Knowledge Base articles, visit the following Microsoft Web site: For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites: For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
236777 How to change print settings for Internet Explorer and the WebBrowser control programmatically in Internet Explorer
313723 How to programmatically change printer settings for Internet Explorer and WebBrowser control by using Visual C# .NET
311280 How to programmatically change printer settings for Internet Explorer and WebBrowser control by using Visual Basic .NET

↑ Back to the top


Article Info
Article ID : 317840
Revision : 3
Created on : 1/1/0001
Published on : 1/1/0001
Exists online : False
Views : 1038