C# Programmer[ap]s Cookbook
Problem
You need to display an HTML page (or another type of document supported by Microsoft Internet Explorer) in a Windows-based application.
Solution
Use the Web browser ActiveX control included with Internet Explorer, using a runtime callable wrapper (RCW).
Discussion
The .NET Framework doesn't include any controls for rendering HTML content. However, this functionality would be useful, either to display some local HTML content (such as a rich help document) or some information from the Web (for example, a Web page that lists downloads a user might use to update an application).
To show an HTML page, you can add an Internet Explorer window to your Windows-based applications. This window not only supports HTML, but it also supports JavaScript and Microsoft Visual Basic Scripting Edition (VBScript) code, ActiveX controls, and various plug-ins depending on your system configuration (possibilities include Microsoft Word, Microsoft Excel, and Adobe Acrobat Reader). You can even use the Web browser control to browse the folders on a local drive or show the files on a File Transfer Protocol (FTP) site.
To add the Web browser to a project in Microsoft Visual Studio .NET, right- click the Toolbox and choose Add/Remove Items. Then select the COM Components tab, and check the Microsoft Web Browser control (shdocvw.dll). This will add the Microsoft Web Browser control to your Toolbox. When you drop this control onto a form, the necessary interop assemblies will be generated and added to your project. If you aren't using Visual Studio .NET, you can generate a wrapper using the Type Library Importer (Tlbimp.exe), as explained in recipe 15.6.
When using the Web browser control, you'll commonly use the following methods :
-
Navigate (and Navigate2 ) redirects the page to the URL you specify.
-
GoBack and GoForward navigate to pages in the history list.
-
GoHome navigates to the home page set on the current computer, and GoSearch shows the search page.
In addition, the user will be able to trigger page navigation by clicking page links (if they exist). You can retrieve the current URL from the LocationURL property and determine if the control is still rendering the page by examining the Busy property. In addition, you can react to a variety of events, including ones that fire when navigation starts and stops.
Figure 11.2 shows an application that allows the user to visit two pages (and follow any links they provide).
using System; using System.Drawing; using System.Windows.Forms; public class WebBrowser : System.Windows.Forms.Form { private AxSHDocVw.AxWebBrowser explorer; private System.Windows.Forms.Button cmdBack; private System.Windows.Forms.Button cmdHome; private System.Windows.Forms.Button cmdForward; // (Designer code omitted.) private void WebBrowser_Load(object sender, System.EventArgs e) { object nullObject = null; object uri = "http://www.microsoft.com"; explorer.Navigate2(ref uri, ref nullObject, ref nullObject, ref nullObject, ref nullObject); } private void cmdHome_Click(object sender, System.EventArgs e) { explorer.GoHome(); } private void cmdForward_Click(object sender, System.EventArgs e) { try { explorer.GoForward(); } catch { MessageBox.Show("Already on last page."); } } private void cmdBack_Click(object sender, System.EventArgs e) { try { explorer.GoBack(); } catch { MessageBox.Show("Already on first page."); } } }
Most of the Web browser control methods require several parameters. Because these methods aren't overloaded, and because C# doesn't support optional parameters, you must supply a value for every parameter. You can't simply use a null reference, however, because they are ref parameters. Instead, it's usually easiest to create an object variable that contains a null reference and supply it for parameters you don't need to use. This technique is demonstrated in more detail in recipe 15.8.
Категории