.NET 单个 ContextMenuItem 中的多个 ToolStripButton
我正在尝试创建一个上下文菜单,其中菜单中的某些项目包含多个项目。您可以将其视为尝试组合 ToolStrip 和 ContextMenuItem。 我尝试过使用 ToolStripControlHost,但这会产生焦点问题。它基本上要求您单击 ToolStrip 中的所有内容两次。
ContextMenuStrip m = new ContextMenuStrip();
ToolStrip tStrip = new ToolStrip(new ToolStripDropDownButton(), new ToolStripButton());
ToolStripControlHost tsHost = new ToolStripControlHost(tStrip);
m.Items.Add(tsHost);
关于如何实现这一点有什么想法吗?
I'm trying to create a ContextMenu where some items in the menu contain more than a single item. You could see it as trying to combine a ToolStrip and ContextMenuItem.
I've tried using a ToolStripControlHost, but this creates problems with the focus. It basically requires you to click everything in the ToolStrip twice..
ContextMenuStrip m = new ContextMenuStrip();
ToolStrip tStrip = new ToolStrip(new ToolStripDropDownButton(), new ToolStripButton());
ToolStripControlHost tsHost = new ToolStripControlHost(tStrip);
m.Items.Add(tsHost);
Any thoughts on how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于不像上下文菜单的自定义弹出窗口来说,ContextMenuStrip 太有吸引力了。这是可取的,因为当用户单击菜单外部时它会自动弹出。但它也有局限性,它只是不是一个很好的控制主机。点击问题是典型的问题,CMS 捕获鼠标来检测用户何时在窗口外点击。
这确实应该是一种形式。不过,要使其具有与 CMS 相同的行为,需要做一些工作。您必须检测窗口外的鼠标点击,以便使窗口消失。像CMS那样捕获鼠标是行不通的。一个技巧是使用 IMessageFilter,它可以让您在将输入消息传递到具有焦点的窗口之前查看它们。下面是实现此功能的示例表单:
正常使用设计器来设计表单。您至少想给它一个不同的 FormBorderStyle。使用提供的 Show() 方法重载,就像在 CMS 中使用它一样。请注意,与 CMS 不同,只有当您单击应用程序拥有的窗口时,该表单才会弹出。功能,而不是错误。
A ContextMenuStrip is too attractive a target for custom popup windows that don't act like a context menu. It is desirable because it automatically pops down when the user clicks outside of the menu. It has limitations though, it just isn't a very good control host. The click problem is classical one, CMS captures the mouse to detect when the user clicks outside of the window.
This really ought to be a form. To give it the same behavior as a CMS requires a bit of work though. You have to detect mouse clicks that are off the window so you can make the window disappear. Capturing the mouse like CMS does doesn't work. One trick is using IMessageFilter, it lets you take a peek at input messages before they are delivered to the window with the focus. Here's a sample form that implements this:
Use the designer as normal to design the form. You want to at least give it a different FormBorderStyle. Use the provided Show() method overload the same way you use it for CMS. Do note that the form pops down only when you click on a window that's owned by the application, unlike a CMS. Feature, not a bug.