为 Internet Explorer 添加按钮、菜单项和浏览器栏的类






4.51/5 (14投票s)
一个简单的类,通过编辑注册表来为 Internet Explorer 设置工具栏按钮、菜单项和浏览器栏。
引言
这是一个 C# 类,用于在 Internet Explorer 中添加按钮、菜单项和浏览器栏。它应该适用于 Internet Explorer 5 及更高版本,但仅在 Internet Explorer 7 上进行过测试。它被编写为一个独立的类,可以由 Windows 窗体应用程序或 ASP.NET 应用程序引用。下载的源代码中包含两个测试应用程序;一个用于 Windows 窗体,一个用于 ASP.NET,以及该类的完整源代码。
背景
MSDN 库中有一篇关于向注册表添加这些功能的教程。我想创建一个应用程序来完成这项工作,这样我就不必手动搜索和编辑注册表了。此外,如果您构建了一个浏览器工具栏,您可以在安装时使用此类添加按钮,而不是为创建的每个栏重写功能。该文章可以在 这里 找到,我建议您阅读它以了解该类的工作原理以及它如何编辑您的注册表。
Using the Code
我已经将所有功能添加到了 InternetExplorerExtender
类中。要使用该类,只需将对象添加为引用并实例化该对象。
using CorKat.RegistrySettings;
InternetExplorerExtender iee = new InternetExplorerExtender();
您还需要引用 Microsoft.Win32
来访问 RegistryKey
类。这使您能够设置要输入设置的注册表的根目录,即 CurrentUser
或 LocalMachine
。
using Microsoft.Win32;
类名
CorKat.RegistrySettings.IEEConstants;
CorKat.RegistrySettings.IEEenums;
CorKat.RegistrySettings.InternetExplorerExtender;
方法
CreateRegistryEntry_MenuItem()
CreateRegistryEntry_Button()
CreateNewExplorerBar()
DeleteKey(string key)
DeleteIECache()
属性
有很多属性。大多数都有默认值。您只需要根据要添加的项目填充某些属性。我以这种方式设置了对象,因此您在调用 create
函数之前填充属性,它将使用这些值来创建注册表项。
// set the properties
iee.ButtonText = "MyButton";
iee.ApplicationPath = "c:/path/filename.exe";
iee.HotIcon = "c:/path/hoticon.ico";
iee.Icon = "c:/path/icon.ico";
// call the registry creation function for a button
iee.CreateRegistryEntry_Button();
下面是使用最少属性来**添加菜单项**的示例。
iee.MenuText = "myMenuItem;
iee.ApplicationPath = "c:/path/filename.exe";
// call the registry creation function for a menu item
iee.CreateRegistryEntry_MenuItem();
默认情况下,菜单会添加到 Internet Explorer 的“工具”菜单中。您也可以将其添加到“帮助”菜单。要执行此操作,请将 InternetExplorerExtender.MenuItem
设置为 IEEenums.MenuItem.help
。
下面是使用最少属性来**添加浏览器栏**的示例。
//set the properties
iee.MenuText = "MyExplorerBar";
// if the explorer bar is a DLL, populate the DLL property.
//NB if left blank the default is "Shdocvw.dll". this is used for HTML pages
if (txtdll.Text != "")
iee.Dll = txtdll.Text;
// if the explorer bar is an HTML page or file, populate the URL property
if (txtURL.Text != "")
iee.URL = txtURL.Text;
iee.CreateNewExplorerBar();
这会在“查看 | 浏览器栏”菜单中创建一个新项,并将新的 CLSID 存储在一个属性中。要立即为 HTML 页面/DLL 创建一个按钮,请调用 CreateButton
函数。
//set the registry paths
iee.BaseRegistryKey = Registry.CurrentUser;
iee.RegistryPath = IEEConstants.Extensions;
iee.TypeOfDetail = IEEenums.DetailType.explorerbar;
// set the properties
iee.ButtonText = "MyButton"
iee.HotIcon = "c:/path/hoticon.ico";
iee.Icon = "c:/path/icon.ico";
// call the registry creation function for a button
iee.CreateRegistryEntry_Button();
有三个核心函数用于创建注册表项:
CreateRegistryEntry_MenuItem()
CreateRegistryEntry_Button()
CreateNewExplorerBar()
如果您想为浏览器栏创建工具栏按钮,请先调用 CreateNewExplorerBar()
函数。此函数会创建在注册表中为新浏览器栏存储的 guid,并将其存储在一个属性中供按钮注册表项使用。这是该函数的完整代码。
public void CreateNewExplorerBar()
{
CreateNewGuid(); //populates AppGuid with a new id
string SubKey = IEEConstants.ExplorerBarPath + AppGuid;
//Create a registry entry for the guid
RegistryKey rk = Registry.ClassesRoot.CreateSubKey(SubKey);
//set the default value to the name that will appear
//in the Internet Explorer menu
rk.SetValue("", MenuText);
// create a new sub key for the horizontal or vertical bar
string impCat = SubKey + "\\Implemented Categories\\";
if (Vertical)
impCat += VerticalCatId;
else
impCat += HorizontalCatId;
rk = Registry.ClassesRoot.CreateSubKey(impCat);
// create a new sub key for InProcServer32
string inproc = SubKey + "\\InProcServer32";
rk = Registry.ClassesRoot.CreateSubKey(inproc);
//set its default value to Shdocvw.dll for an html page or *.dll
//for a custom file
rk.OpenSubKey(inproc);
rk.SetValue("", Dll);
//Create a new string value under InProcServer32
rk.SetValue("ThreadingModel", "Apartment");
// create a new sub key for Instance
string instance = SubKey + "\\Instance";
rk = Registry.ClassesRoot.CreateSubKey(instance);
rk.OpenSubKey(instance);
//Create a new string value under Instance
rk.SetValue("CLSID", "{4D5C8C2A-D075-11d0-B416-00C04FB90376}");
// create a new sub key for Instance
string InitPropertyBag = instance + "\\InitPropertyBag";
rk = Registry.ClassesRoot.CreateSubKey(InitPropertyBag);
rk.OpenSubKey(InitPropertyBag);
//Create a new string value under Instance
rk.SetValue("URL", URL); // the html page to open in the explorer bar
DeleteIECache();
SubKey = IEEConstants.ExplorerBars + AppGuid;
//Create a registry entry for the guid
rk = BaseRegistryKey.CreateSubKey(SubKey);
// store the BandCLSID for making a button
BandCLSID = AppGuid;
}
然后您可以调用 CreateRegistryEntry_Button
函数。
public void CreateRegistryEntry_Button()
{
// Create a new id for the button
CreateNewGuid();
//set the path to the new registry entry
//string SubKey = RegistryPath + AppGuid;
string SubKey = IEEConstants.Extensions + AppGuid;
//Create the registry entry for the guid
RegistryKey rk = BaseRegistryKey.CreateSubKey(SubKey);
//set the default class id used for 3 details.
//gets reset if an explorerbar type is used
string strCLSID = ToolbarExtensionForExecutablesCLSID;
//create the common registry string values and set them
rk.SetValue("Default Visible", Visible);
rk.SetValue("ButtonText", ButtonText);
rk.SetValue("HotIcon", HotIcon); //path to *.ico file
rk.SetValue("Icon", Icon); //path to *.ico file
//CorKat.RegistrySettings.regProperties.
switch (TypeOfDetail)
{
case IEEenums.DetailType.executable:
{
rk.SetValue("Exec", ApplicationPath);
break;
}
case IEEenums.DetailType.script:
{
rk.SetValue("Script", ApplicationPath);
break;
}
case IEEenums.DetailType.com:
{
rk.SetValue("ClsidExtension", ComCLSID);
break;
}
case IEEenums.DetailType.explorerbar:
{
strCLSID = ToolbarExtensionForBandsCLSID;
rk.SetValue("BandCLSID", BandCLSID);
break;
}
}
//set the class id
rk.SetValue("CLSID", strCLSID);
}
第三个函数用于创建菜单项。注意:默认情况下,浏览器栏注册表项会在“查看 | 浏览器栏”中创建一个菜单项。此函数会在“工具”菜单(如果未指定,则为默认值)或“帮助”菜单中创建一个菜单项。
public void CreateRegistryEntry_MenuItem()
{
// Create a new id for the menu item
CreateNewGuid();
//set the path to the new registry entry
//string SubKey = RegistryPath + AppGuid;
string SubKey = IEEConstants.Extensions + AppGuid;
//Create the registry entry for the guid
RegistryKey rk = BaseRegistryKey.CreateSubKey(SubKey);
//create the common registry string values and set them
rk.SetValue("CLSID", ToolbarExtensionForExecutablesCLSID);
rk.SetValue("MenuText", MenuText);
//set the default menu to tools and switch if help is selected
string strMenu = "tools";
if (MenuItem == IEEenums.MenuItem.help)
strMenu = "help";
rk.SetValue("MenuCustomize", strMenu);
//create the detailed registry string values and
//set them for the application type details
switch (TypeOfDetail)
{
case IEEenums.DetailType.executable:
{
rk.SetValue("Exec", ApplicationPath);
break;
}
case IEEenums.DetailType.script:
{
rk.SetValue("Script", ApplicationPath);
break;
}
case IEEenums.DetailType.com:
{
rk.SetValue("ClsidExtension", ComCLSID);
break;
}
// no case for explorer tool bars as they appear on the
// view menu automatically when set up
// as their set up is different they use a different function
}
}
所有新的注册表项都需要一个唯一的 GUID。CreateNewGuid
函数为此提供了支持,并在创建新的浏览器栏、按钮或菜单项时被调用。
private string CreateNewGuid()
{
AppGuid = Guid.NewGuid().ToString().ToUpper();
return AppGuid;
}
有三个函数用于检索按钮菜单和浏览器栏的注册表项。在 Windows 示例中,它们用于填充列表视图以选择和删除这些项。
/// Retrieve the subkeys names at the current key.
/// returns a string array
public string[] SubKeyNames()
{
try
{
// Setting
RegistryKey rk = BaseRegistryKey;
rk = rk.OpenSubKey(RegistryPath);
string[] strKeys = rk.GetSubKeyNames();
return strKeys;
}
catch
{
return null;
}
}
/// return an array containing all the sub values of a given key
public string[] SubKeyNames(string key)
{
try
{
RegistryKey rk = BaseRegistryKey;
RegistryKey regSubKey = rk.OpenSubKey(RegistryPath + "\\" + key);
string[] strKeys = regSubKey.GetValueNames();
return strKeys;
}
catch
{
return null;
}
}
/// Retrieve the value of the subkeys at the provided subkey key.
public string GetValue(string ValueName, string key)
{
try
{
// Setting
RegistryKey rk = BaseRegistryKey;
RegistryKey regSubKey = rk.OpenSubKey(RegistryPath + "\\" + key);
// If the RegistryKey exists...
if (regSubKey != null)
return regSubKey.GetValue(ValueName).ToString();
else
return "";
}
catch
{
return "";
}
}
还有一个用于删除键及其任何子键的函数。
public void DeleteKey(string key)
{
string subkey = RegistryPath + key;
RegistryKey rk = BaseRegistryKey;
RegistryKey regSubKey = rk.OpenSubKey(subkey);
// If the RegistryKey exists, I delete it
if (regSubKey != null)
rk.DeleteSubKeyTree(subkey);
}
最后一个函数 DeleteIECache
处理 Internet Explorer 缓存浏览器栏菜单的方式,在创建或删除浏览器栏时需要调用它。否则,当您打开 Internet Explorer 时,由于缓存的原因,看起来什么都没有发生。
public void DeleteIECache()
{
//delete the registry entry for enums so the
//explorer bars aren't cached and the new ones show
RegistryPath = ComponetCat1Cache;
RegistryKey rk = BaseRegistryKey;
RegistryKey regSubKey = rk.OpenSubKey(RegistryPath);
// If the RegistryKey exists, I delete it
if (regSubKey != null)
rk.DeleteSubKeyTree(RegistryPath);
RegistryPath = ComponetCat2Cache;
//rk = Registry.CurrentUser;
regSubKey = rk.OpenSubKey(RegistryPath);
// If the RegistryKey exists, I delete it
if (regSubKey != null)
rk.DeleteSubKeyTree(RegistryPath);
}
关注点
当您通过编辑注册表添加新的浏览器栏时,它不会显示在 Internet Explorer 菜单中,这是因为 Internet Explorer 缓存其菜单的方式。MSDN 教程 添加浏览器栏 告诉您删除注册表项。
HKEY_CLASSES_ROOT\Component Categories\{00021493-0000-0000-C000-000000000046}\Enum
HKEY_CLASSES_ROOT\Component Categories\{00021494-0000-0000-C000-000000000046}\Enum
这是错误的位置。您需要删除:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Discardable\
PostSetup\Component Categories\{00021493-0000-0000-C000-000000000046}\Enum
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Discardable\
PostSetup\Component Categories\{00021494-0000-0000-C000-000000000046}\Enum
但是,您无需担心这个问题。一旦创建了新的浏览器栏,该函数就会为您完成。但是,我已将 DeleteIECache
函数设置为 public
,以便您可以根据需要调用它。
历史
- 版本 1.0(初始版本)- 2007 年 1 月 22 日
- 添加了 Visual Studio 2003 zip - 2007 年 2 月 20 日 - 注意:它包含与 Visual Studio 2005 版本相同的类,但测试应用程序仅向您展示如何添加按钮。