Guide to developing Internet Explorer extensions
Create a working Internet Explorer extension
Prerequisites:
- Visual Studio
- .NET Framework 4.0 or higher
- Internet Explorer
Steps:
-
Create a class library project:
- Create a new class library project and name it "IEExtension" or your favorite name.
-
Add citation:
- Add references to the "Interop.SHDocVw" and "Microsoft.mshtml" assemblies.
-
Create source file:
- Create two source files: "IEAddon.cs" and "Interop.cs".
IEAddon.cs:
<code class="language-csharp">using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using mshtml;
using SHDocVw;
namespace IEAddon
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("D40C654D-7C51-4EB3-95B2-1E23905C2A2D")]
[ProgId("MyBHO.WordHighlighter")]
public class WordHighlighterBHO : IObjectWithSite, IOleCommandTarget
{
//... (此处省略部分代码)
}
}</code>
Copy after login
Interop.cs:
<code class="language-csharp">using System;
using System.Runtime.InteropServices;
namespace IEAddon
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")]
public interface IObjectWithSite
{
//... (此处省略部分代码)
}
}</code>
Copy after login
-
Create configuration form:
- Create a new form called "HighlighterOptionsForm" that contains a TextBox (for entering search terms) and an "OK" button.
-
Configure project properties:
- Sign the assembly with a strong key.
- Set "Start External Program" to "iexplore.exe" in the "Debug" tab.
- Set the "Command Line Arguments" to an external URL in the "Debug" tab.
- Add a post-build event to register the assembly using RegAsm.exe.
-
Build and run:
- Build the project and run it using Internet Explorer.
Usage:
- This add-on will highlight all instances of the specified word (default is "browser") in a web page using a yellow background.
- Clicking on the highlighted text will call a JavaScript function.
- To change the highlighted word, click the button, set the value in the text box and press OK.
Other information:
- The "IEAddon" class implements the "IObjectWithSite" and "IOleCommandTarget" interfaces to implement the BHO function.
- The "Interop" class defines the "IObjectWithSite" interface to support COM interop.
- The post-generation script registers the BHO in the Windows registry and Internet Explorer extensions menu.
- To disable or uninstall a BHO, use RegAsm to unregister it.
The above is the detailed content of How can I create an Internet Explorer extension using Visual Studio and COM?. For more information, please follow other related articles on the PHP Chinese website!