使用 C# 创建和安装 Internet Explorer 上下文菜单






3.67/5 (8投票s)
使用安装程序类添加 Internet Explorer 上下文菜单
引言
本文主要说明如何使用 C# 在安装时创建 Internet Explorer 上下文菜单项。通常,此类项目不是独立的,而是包含在其他功能中,但本文将展示创建上下文菜单项的分步指南。
背景
首先,您需要了解如何手动添加 Internet Explorer 上下文菜单项,这是一个有趣的链接,可以查看一下。
创建项目
此类项目的材料是
- C# 类库
- 设置项目
对于 C# 类库,删除默认类,右键单击项目名称,然后单击添加新项并选择安装程序类。
对于安装项目,只需添加安装程序类项目的主输出,并将此输出添加到所有四个默认自定义操作。(右键单击安装项目名称,选择视图>自定义操作。)
最后,您的解决方案应如下所示

非常简单。现在,在处理代码之前,我们只需要调整安装项目中的一件事。 基本上,我们需要获取用户在安装时提交的目标目录,为此,请执行以下操作。
选择在“安装操作”下添加的主输出,然后键入如下面图片中所示的 CustomActionData

创建安装程序类
安装程序的主要目的是创建注册表项,如下所示
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
//DON'T FORGET TO ADD THIS ONE - used for the Registry Class
using Microsoft.Win32;
namespace IEContextInstallerClass
{
[RunInstaller(true)]
public partial class IEInstaller : Installer
{
public IEInstaller()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
//grab the target director and add to install.installstate
//note that the grabbed parameters are exactly the same
//as the one we set above
stateSaver.Add("TargetDir", Context.Parameters["DP_TargetDir"].ToString());
}
public override void Commit(System.Collections.IDictionary savedState)
{
//retrieve the saved state
base.Commit(savedState);
//adding items to the context menu of IE
RegistryKey key;
//1,2,4,8,10,20 in heximal are 1,2,4,8,16,32 in decimal (respectively)
string keyValueInt = "16";
//the location of our key
string subKey =
@"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";
//create it
key = Registry.CurrentUser.CreateSubKey(subKey);
//set the value
key.SetValue("Contexts", Convert.ToInt32(keyValueInt),
RegistryValueKind.DWord);
//set the path to the action file
key.SetValue(null, "file://" + savedState["TargetDir"].ToString() +
"\\action.html");
//and close
key.Close();
}
protected override void OnBeforeUninstall
(System.Collections.IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
//removing items from the context menu of IE
string subKey =
@"SOFTWARE\Microsoft\Internet Explorer\MenuExt\Sample Action";
Registry.CurrentUser.DeleteSubKey(subKey);
}
}
}
现在基本上,安装程序类执行以下操作
- 安装时,获取目标目录并保存它,以便我们可以在以后的阶段使用它
- 提交时,创建注册表项
- 如果用户希望卸载,只需删除键
创建操作文件
此操作文件是我们在单击上下文菜单时运行的实际代码
<SCRIPT LANGUAGE = "JavaScript">
// Get the window object where the context menu was opened.
var oWindow = window.external.menuArguments;
// Get the document object exposed through oWindow.
varoDocument = oWindow.document;
// Get the selection from oDocument.
// in oDocument.
varoSelect = oDocument.selection;
// Create a TextRange from oSelect.
varo SelectRange = oSelect.createRange();
// Get the text of the selection.
var sNewText = oSelectRange.text;
// If something is selected, alert showing the selected text.
if(sNewText.length != 0){
alert(sNewText);
}
</SCRIPT>
以上是一个简单的 JavaScript,它会获取选定的文本并在消息框中显示它...... 您可以根据需要执行其他操作。(上面的代码基于这个。)
安装
完成以上所有操作后,构建您的解决方案并安装。
您应该已将以下键添加到您的注册表中

如果您打开一个新的 Internet Explorer 实例并右键单击选定的文本,您应该会得到

回顾
所以基本上,您现在应该能够在安装时创建和删除注册表项,而不会出现任何问题。
注意
现在这绝对不是什么创新或与众不同的东西,但我无法在 Code Project 上找到类似的东西,所以我才想到添加它。
此外,这是我在“谷歌”搜索时发现的项目汇编...