Adding context menu to IE to execute my program

时间:2016-04-07 10:38:59

标签: c# winforms internet-explorer regedit

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 个答案:

答案 0 :(得分:4)

您可以在IE标准上下文菜单中添加条目以打开您的程序。为此,请按照下列步骤操作:

  1. 打开注册表并转到:

    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt
    
  2. 创建一个新密钥,并将密钥名称设置为您希望在上下文菜单中显示的文本作为名称,例如:Open My App

  3. 右键单击(Default)并选择Modify...并将值设置为html文件的路径,该文件将包含打开应用程序的命令。例如:C:\OpenMyApp.html

  4. 添加名为DWORD的新Context值,并将其值设置为十六进制11或十进制17。要查看更多选项,请阅读documentation。另外在文档中说要添加二进制文件,但我尝试了DWORD而且它有效。我见过的其他扩展程序也使用了DWORD

  5. 将此内容用于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. 然后将您的应用程序复制到C:\MyApp.exe就足够了。您的应用程序应通过接受string[] args作为Main入口点的输入参数或使用Environment.GetCommandLineArgs()来处理命令行参数。然后就足以将参数传递给您的表单并将其显示在文本框中。

  7. 了解更多信息: