问题
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:
Open registry and go to:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExtCreate 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 AppRight click on
(Default)and chooseModify...and set the value to path of an html file which will contains the command to open your application. For example:C:\OpenMyApp.htmlAdd a new
DWORDvalue namedContextand set it's value to hexadecimal11or decimal17. To see more options read documentation. Also in documentations said to add binary but I triedDWORDinstead and it worked. Also other extensions that I've seen useDWORD.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>Then it's enough to copy your application to
C:\MyApp.exe. Your application should handle command line arguments by acceptingstring[] argsas input parameters forMainentry point or usingEnvironment.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