WinForm:将 ContextMenuStrip MenuItem 与 VS2010 中的现有菜单合并

发布于 2024-11-14 10:11:55 字数 213 浏览 4 评论 0原文

我已将一些自定义 MenuItem 对象添加到 ContextMenuStrip 控件中。

然而,添加我的对象完全破坏了控件中已经存在的非常有用的菜单项(TreeViewTextBoxDataGridView)。

有人能够成功合并这些项目吗?

I have added a few custom MenuItem objects to a ContextMenuStrip control.

However, adding my objects has completely blown away the very useful menu items that already existed in the controls (TreeView, TextBox, and DataGridView).

Has anyone been able to successfully merge these items?

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

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

发布评论

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

评论(1

捂风挽笑 2024-11-21 10:11:55

不,你不能合并它们。您必须通过添加适当的菜单项并在其 Click 事件处理程序方法中编写必要的代码来自己复制该功能。

通用控件默认提供该菜单。如果您希望覆盖它,您可以这样做,但是没有内置的规定可以将您的自定义项目与内置菜单相结合。有些人将此作为一项功能,将空的 ContextMenuContextMenuStrip 分配给控件,以防止标准菜单被显示(谈论令人困惑的 UI 设计!)。

特别是对于 TextBox 控件,我强烈建议您按原样保留默认上下文菜单,而不是尝试重新创建它。它为 IME 之类的东西提供了很多选项,而这些选项您自己很难正确设置。如果您的应用程序面向公众,他们会非常感激您的应用程序中存在预期的此类功能。

最好将您的自定义功能完全放置在其他地方。右键单击常用控件并期望看到自定义选项列表是非常不直观的。正是由于这个原因,大多数其他应用程序都不会以这种方式工作。


尽管我的建议与此相反,如果您坚持要让它工作,您可能会利用这样的知识:内置上下文菜单只是一个标准的 Win32 弹出菜单,由所有常见的控件,并且该控件向其父级发送 WM_INITMENUPOPUP当弹出菜单即将激活时消息

掌握了这些知识后,您可能会决定从 Windows API P/Invoke 某些功能并修改该上下文菜单。特别是 AppendMenuSetMenuIteminfo 函数。

但接下来,惊喜就会等着你。事实证明,本机编辑控件(以 TextBox 为代表)不发送WM_INITMENUPOPUP消息!我不确定 TreeView 控件,并且 DataGridView 控件不会执行任何操作,因为它是为 WinForms 编写的自定义类。它根本不基于任何本机 Win32 控件,因此它不会发送您可以处理的任何此类消息。

No, you can't merge them. You'll have to duplicate the functionality yourself by adding the appropriate menu items and writing the necessary code in their Click event handler methods.

The common controls provide that menu by default. If you wish to override it, you can do so, but there is no built-in provision for combining your custom items with the built-in menu. Some have taken advantage of this as a feature, assigning an empty ContextMenu or ContextMenuStrip to the control in order to prevent the standard menu from ever being displayed (talk about confusing UI design!).

For the TextBox control in particular, I highly recommend that you leave the default context menu as-is rather than trying to re-create it. There are lots of options that it exposes for things like IME that are extremely difficult to get right yourself. If you're targeting your application to the general public, they'll very much appreciate that such functionality exists in your app as expected.

It's best to place your custom functionality somewhere else entirely. Right-clicking a common control and expecting to see a list of custom options is pretty unintuitive. Most other applications don't work that way for precisely this reason.


If, despite my recommendations to the contrary, you were quite insistent upon getting this to work, you might take advantage of the knowledge that the built-in context menu is just a standard Win32 pop-up menu that is displayed by all of the common controls, and that controls send their parent a WM_INITMENUPOPUP message when a pop-up menu is about to become active.

Armed with that knowledge, you might decide to P/Invoke some functions from the Windows API and modify that context menu. In particular, the AppendMenu or SetMenuIteminfo functions.

But then, a surprise will await you. It turns out that the native Edit control (which the TextBox is a representative of) does not send a WM_INITMENUPOPUP message! I'm not sure about the TreeView control, and the DataGridView control won't do any of this because it's a custom class written for WinForms. It's not based on any of the native Win32 controls at all, so it won't be sending any such messages that you can handle.

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