将上下文菜单项添加到操作系统范围内的所有文本框

发布于 2024-10-17 13:10:52 字数 133 浏览 1 评论 0原文

我知道可以扩展标准文件的上下文菜单,但是有没有办法将项目添加到系统范围的文本框上下文菜单中?

这样新项目就会出现在用户可以在窗口中的任何位置键入的每个文本框中吗?

例如,允许添加诸如根据拼写检查器查找所选文本之类的功能。

I know it is possible to extend the context menu of the standard file however is there a way to add items to the system-wide text box context menu?

This would be so the new item appears in every text box that a user can type in anywhere in windows?

For example to allow to add such features as look up the selected text against a spell checker.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞取 2024-10-24 13:10:52

这对于 C# 在技术上是不可能的。
让我们想象一下接下来的情况。假设您已成功编写托管扩展,并且它“扩展”了系统中的任何文本框上下文菜单。当然,您应该处理添加的每个新菜单项。为了实现这一点,您应该将托管代码注入到每个具有文本框的进程并替换其 WindProc。好的,假设系统中有一个托管应用程序,它是为 .net 版本编写的,与您用于扩展的版本不同。因此,在这种情况下,您将在扩展注入期间遇到严重错误,因为每个进程只能加载一个版本的 .net。因此,无法编写健壮的托管低级钩子或 shell 扩展。来自此处

不支持全局挂钩
.NET框架

除了
WH_KEYBOARD_LL 低级钩子和
WH_MOUSE_LL 低级钩子,你不能
在中实现全局钩子
微软.NET框架。要安装一个
全局钩子,钩子必须有本地钩子
DLL 导出以将自身注入到另一个中
需要有效的过程,
调用的函数一致。这
行为需要 DLL 导出。这
.NET Framework 不支持 DLL
出口。托管代码没有概念
函数的值一致
指针,因为这些函数
指针是构建的代理
动态地。

调用低级挂钩过程
在安装钩子的线程上。
低级钩子不需要
钩子程序可以在a中实现
DLL。

您还可以查看< /a> 文章并探讨为什么不应该编写托管 shell 扩展。

This is technically impossible with C#.
Lets imagine next situation. Suppose you have sucessfuly write your managed extention and it "extends" any textbox context menu in the system. And of course you should handle each new menu item you have added. and to achieve this you should inject your managed code to each process which has textbox and replace its WindProc. OK, suppose there is managed app in the system which was writen for .net version different from version you used for your extention. So in this case you would got critical error during extension injection because ONLY one version of .net could be loaded per process. SO there is no way to write robust managed low level hook or shell extension. From here:

Global hooks are not supported in the
.NET Framework

Except for the
WH_KEYBOARD_LL low-level hook and the
WH_MOUSE_LL low-level hook, you cannot
implement global hooks in the
Microsoft .NET Framework. To install a
global hook, a hook must have a native
DLL export to inject itself in another
process that requires a valid,
consistent function to call into. This
behavior requires a DLL export. The
.NET Framework does not support DLL
exports. Managed code has no concept
of a consistent value for a function
pointer because these function
pointers are proxies that are built
dynamically.

Low-level hook procedures are called
on the thread that installed the hook.
Low-level hooks do not require that
the hook procedure be implemented in a
DLL.

You also may check this article and explore why you shouldnt write managed shell extensions.

嗼ふ静 2024-10-24 13:10:52

从您尝试使用的文本框继承并在其中添加默认值。使用您的类而不是文本框类。

Inherit from the text box you're trying to use and add the default value there. Use your class instead of the text box class.

丶视觉 2024-10-24 13:10:52

您必须添加鼠标挂钩

并且然后当您单击的窗口/控件是文本框时弹出上下文菜单。

这可能(并且可能会)干扰已经存在的上下文菜单,因为您将隐藏它们或与它们发生冲突。

(虽然这是一个可怕的计划......)

You would have to add a Mouse Hook

And then pop up a context menu when the window/control you are clicking is a textbox.

This might (and probably will) interfere with already present context menus because you will be hiding them or clashing with them.

(A horrible plan though...)

七七 2024-10-24 13:10:52

使用表单和上下文菜单调用 Bind 方法。

  void Bind(Control c, ContextMenu menu)
  {
     foreach (Control subcontrol in c.Controls)
        Bind(subcontrol);

     TextBox textBox = c as TextBox;
     if (textBox != null)
     {
         textBox.ContextMenu = menu;
     }
  }

如果添加动态文本框,则将其放在 Bind 方法的开头:

         c.ControlAdded += (s, e) => Bind(e.Control, menu);

每当将控件添加到窗体或另一个控件时,这都会调用 Bind 方法。

Call the Bind method with the form and context menu.

  void Bind(Control c, ContextMenu menu)
  {
     foreach (Control subcontrol in c.Controls)
        Bind(subcontrol);

     TextBox textBox = c as TextBox;
     if (textBox != null)
     {
         textBox.ContextMenu = menu;
     }
  }

If you adding a dynamic textbox, then place this at the beggining of the Bind method:

         c.ControlAdded += (s, e) => Bind(e.Control, menu);

This causes the Bind method to called, whenever a control is added to the form or another control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文