INFO: WebOCHostCSharp.exe Hosts the WebBrowser Control in C# .NET (326217)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q326217

SUMMARY

The WebOCHostCSharp.exe sample demonstrates how to host the WebBrowser control in C# .NET.

MORE INFORMATION

The following file is available for download from the Microsoft Download Center:
Release Date: Oct 30, 2002

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. The WebOCHostCSharp.exe program implements a Single Document Interface (SDI) application that enables Web browsing through the Internet Explorer WebBrowser control. The code in this sample uses a class design wherein the WebBrowser is encapsulated in a custom Windows Forms UserControl, which in turn is hosted on the main form of your SDI application. You can then encapsulate some of the lengthy Internet Explorer command syntax and reuse the WebBrowser control in other forms in this project and other projects.

For example, the WebOCHostCSharp.exe sample reduces the method call to copy text from the WebBrowser control from this:
object o = new object();
axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_COPY, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref o, ref o);
				
to this:
webOCWrapper.Copy();
				

Techniques that the Sample Demonstrates

The WebOCHostCSharp.exe sample demonstrates the following techniques:
  • How to browse by means of an address bar or by means of a custom Open dialog box that permits documents to be loaded both from a URL and from the local file system.
  • How to implement simple editing commands (such as cut, copy, and paste), and how to receive notification of their status through the CommandStateChange event of the WebBrowser control.
  • How to persist typed URLs in the address bar. The sample mimics the Internet Explorer combo box address bar with a simple test in the DWebBrowserEvents2::NavigateComplete2 event handler. The URL that a user types in the address bar is frequently not the final URL, for one of the following reasons:
    • The user types a partial URL (such as www.microsoft.com instead of http://www.microsoft.com),
    • The server issues an HTTP redirect to another file.
    Therefore, this sample waits until the WebBrowser control throws the NavigateComplete2 event to guarantee the final, canonical form of the URL for the current page.

    This samples uses the KeyDown event handler to set a Boolean value, wasUrlTyped, whenever the user types a URL. If this value is true inside the NavigateComplete2 event handler, the URL is added as a list item to the sorted combo box.
  • How to implement the Find command, and the View Source Internet Explorer menu commands.

    To do this, the sample implements a COM interop declaration for IOleCommandTarget.
  • How to print. The WebBrowser host control in this sample defines the following two overloaded Print methods:
        public void Print(bool doUI);
        public void PrintFormatted(string tmplPath);
    						
    Overloaded methods are a feature of object-oriented languages (for example, Visual C# and Visual Basic), that support multiple definitions of a method if the methods differ in the numbers and types of their parameters. (The cardinality and the type of a method's parameters are usually known as its signature).

    In this case, the Print(Boolean) method prints a document that is hosted in a WebBrowser control. Whether this method displays a dialog box depends on the parameter. The Print(String) method prints a document and does not display a dialog box. This method uses a Microsoft HTML (MSHTML) print template to automatically control print options such as paper size, orientation, and number of copies.

REFERENCES

For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/11/2006
Keywords:kbdownload kbfile kbGrpDSInet kbhowto KB326217