Microsoft KB Archive/317840

From BetaArchive Wiki

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

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:

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  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. Delete all of the code in the window, and then paste the following code into 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;
    }
                        
  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 additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

236777 How To Change Print Settings for Internet Explorer and WebBrowser Control Programmatically


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

Keywords: kbhowtomaster KB317840