IE瀏覽器右鍵菜單插件開發(上篇)——自定義一個IE右鍵菜單項


要做一個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

            

 

             

 

 

          

           

 

 

 

 

 

 

 











免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM