Adding context menu to IE to execute my program

坚强是说给别人听的谎言 提交于 2019-12-23 16:22:43

问题


I wanted to know how can I add a new item to IE context menu (right click menu), so that the selected text from a website is copied, my winform application C# is opened and the text is pasted into a text box in my application.


回答1:


You can add an entry to IE standard context menu to open your program. To do so, follow these steps:

  1. Open registry and go to:

    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt
    
  2. Create a new key, and set the name of key to the text you want displayed in the context menu as the name, for example: Open My App

  3. Right click on (Default) and choose Modify... and set the value to path of an html file which will contains the command to open your application. For example: C:\OpenMyApp.html

  4. Add a new DWORD value named Context and set it's value to hexadecimal 11 or decimal 17. To see more options read documentation. Also in documentations said to add binary but I tried DWORD instead and it worked. Also other extensions that I've seen use DWORD.

  5. Use this content for your C:\OpenMyApp.html:

    <script type="text/javascript">
        function getSelectionText(w) {
            var text = "";
            if (w.getSelection) {
                text = w.getSelection().toString();
            } else if (w.document.selection && w.document.selection.type != "Control") {
                text = w.document.selection.createRange().text;
            }
            return text;
        }
    
        var parentwin = external.menuArguments;
        var selection = getSelectionText(parentwin);
        var oShell = new ActiveXObject("Shell.Application");
        var commandtoRun = "C:\\MyApp.exe"; 
        oShell.ShellExecute(commandtoRun,"\""+selection+"\"","","open","1");
    </script>
    
  6. Then it's enough to copy your application to C:\MyApp.exe. Your application should handle command line arguments by accepting string[] args as input parameters for Main entry point or using Environment.GetCommandLineArgs(). Then it's enough to pass the argument to your form and show it on your text box.

For more information:

  • Adding Entries to the Standard Context Menu
  • Browser Extensions


来源:https://stackoverflow.com/questions/36473703/adding-context-menu-to-ie-to-execute-my-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!