简体   繁体   English

当用户选择文本时,将自定义项目添加到右键菜单

[英]Add custom item to right click menu when user selects text

I want to add a custom menu item to the menu that appears when a user selects some text and right clicks. 我想向用户选择一些文本并单击鼠标右键时出现的菜单添加一个自定义菜单项。 I searched a lot but did not get any answer. 我搜索了很多,但没有得到任何答案。 Maybe I did not put in the right keyword. 也许我没有输入正确的关键字。

  1. Is it possible to do this? 是否有可能做到这一点?
  2. If this is possible, I want to do this for all applications in windows whenever the user selects text. 如果可能的话,无论何时用户选择文本,我都希望对Windows中的所有应用程序执行此操作。 Is there a universal way to do it or will have to write code specific to the application? 有通用的方法吗?还是必须编写特定于应用程序的代码?

I am using C#. 我正在使用C#。

I don't think you can do that universally in all windows apps. 我认为您无法在所有Windows应用程序中普遍做到这一点。 But it is certainly possible to do in your own app using a popup menu. 但是使用弹出菜单在您自己的应用程序中当然可以实现。

I've also tried a way to add an item to the standard ContextMenu and failed. 我也尝试了一种将项目添加到标准ContextMenu的方法,但失败了。 From my point of view, the only solution is to set the TextBox's ContextMenuStrip property to an instance of the ContextMenuStrip class and implement both standard and your custom menu items. 从我的角度来看,唯一的解决方案是将TextBox的ContextMenuStrip属性设置为ContextMenuStrip类的实例,并实现标准菜单项和自定义菜单项。 It is easy to mimic standard operation, since the TextBox class provides all required methods: Cut, Copy, Paste, SelectAll. 模仿标准操作很容易,因为TextBox类提供了所有必需的方法:剪切,复制,粘贴,SelectAll。

The universal way is basically writing a "custom shell extension". 通用方法基本上是编写“自定义外壳程序扩展”。 There is an article on CodeProject about how to do this. 在CodeProject上有一篇有关如何执行此操作的文章。

自定义外壳扩展

http://www.codeproject.com/Articles/174369/How-to-Write-Windows-Shell-Extension-with-NET-Lang http://www.codeproject.com/Articles/174369/How-to-Write-Windows-Shell-Extension-with-NET-Lang

The other way to do this is to build this into your application. 执行此操作的另一种方法是将其构建到您的应用程序中。 For example you might have a text editor and you'll want to highlight a word and get a definition for that word. 例如,您可能有一个文本编辑器,并且想要突出显示一个单词并获取该单词的定义。 To do this you'll need to implement a context menu. 为此,您需要实现一个上下文菜单。 The VERY BASIC concept is detailed over on CodeReview.StackExchange. 在CodeReview.StackExchange上详细介绍了非常基础的概念。

https://codereview.stackexchange.com/questions/3983/c-programatically-creating-context-menu https://codereview.stackexchange.com/questions/3983/c-programatically-creating-context-menu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM