您的位置:首页 > 其它

IE浏览器右键菜单插件开发(上篇)——自定义一个IE右键菜单项

2017-06-29 16:20 363 查看
要做一个IE右键浏览器插件,得3步走。第一,在IE右键菜单上添加自定义菜单名称,是通过注册表实现的,如下:
1   string regkey = @"Software\Microsoft\Internet Explorer\MenuExt\KnowledgeSave";
2                 string scriptPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"getcurrenturl.htm");
3                 RegistryKey root = Registry.CurrentUser.OpenSubKey(regkey);
4
5                 if (null == root)
6                 {
7                     root = Registry.CurrentUser.CreateSubKey(regkey);
8                     root.SetValue("", scriptPath, RegistryValueKind.String);
9                     root.SetValue("Contexts", 0x000000f3, RegistryValueKind.DWord);
10                 }
菜单名称:KnowledgeSave
菜单指向的文件:getcurrenturl.htm,右键点击菜单项”KnowledgeSave”,则会执行此页面:getcurrenturl.htm

第二,看看getcurrenturl.htm页面的构成,它是桥梁,通过activex对象实例化了c#对象。如下:
1 <script type="text/javascript">
2     try {
3         var proxy = new ActiveXObject("myLib.MyClass");
4         var num = proxy.Add();
5         alert(num);
6     }
7     catch (e) {
8         alert(e.message);
9     }
10 </script>
其中,myLib.MyClass是c#编写的com组件暴露出来的类。第三,com组件编写。下面的guid是随机生成的。
1  [ComVisible(true)]
2     [Guid("317E81A0-C55C-36B2-B259-BEB1A4F3919E")]
3     public class MyClass
4     {
5         public int Add()
6         {
7             int a = 1;
8             int b = 2;
9             return a + b;
10         }
11     }
这个组件公布了一个Add方法,同时,它必须注册,方能使用,见下图。看看我的测试结果,打开百度页面,右键点击 KnowledgeSave弹出一个结果:3
,注意:1、在com组件,操作本地文件(比如新建)时,会有限制,这里只是简单地做了个加法测试。2、com组件需要注册。(上图中勾选了com互操作,所以在生成时,微软帮我们做了这一步),com注册成功后,会在注册表HKEY_CLASSES_ROOT下有记录。3、IE右键菜单的注册表项位置:HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: