9

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John
  • 91
  • 1
  • 2

4 Answers4

3

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.

joshcomley
  • 28,099
  • 24
  • 107
  • 147
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
2

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.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
1

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...)

Emond
  • 50,210
  • 11
  • 84
  • 115
0

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.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73