简体   繁体   English

如何编写自己的ContextMenu? C#

[英]How can I write my own ContextMenu? C#

I feel quite limited by the default ContextMenuStrip, as it only can contain buttons, and no Controls. 我感觉受到默认ContextMenuStrip的限制,因为它只能包含按钮,而不能包含控件。 I was wondering that for a long time, and I already tried it, using forms, but it never really worked out. 我想知道很长一段时间,并且我已经使用表格尝试了它,但从未真正解决过。

I already have I idea on how to set the whole thing up, with events and items. 我已经对如何设置事件和项目的整体想法有所了解。 The only problem I have is the paint method. 我唯一的问题是绘画方法。 When you open a ContextMenu (ContextMenuStrip) you can set its position on the mouse cursor, and it will be there, even if that means that it goes beyond the active form. 当您打开ContextMenu(ContextMenuStrip)时,可以在鼠标光标上设置其位置,即使它超出了活动窗体,它也将位于那里。 (So I can't use the Controls Class as inheritance, as they can only draw themself as a part of a form. Now I thought to use the Form Class as a base for my ContextMenu, but those where placed on the screen randomly. (因此,我不能将Controls类用作继承,因为它们只能将自身绘制为表单的一部分。现在,我想将Form类用作ContextMenu的基础,但是将其随机放置在屏幕上。

So what I actually need is a class (or something similar) that can draw itself, without problems, and can be placed accurately on the screen. 因此,我真正需要的是一类(或类似的东西),它可以自己绘制而不会出现问题,并且可以准确地放置在屏幕上。

Any hint would be nice, thanks. 任何提示都会很好,谢谢。

Greg the Mad 疯狂的格雷格

Your first statement is false -- you can have a TextBox or a ComboBox in a ContextMenuStrip. 您的第一个语句为假-ContextMenuStrip中可以有一个TextBox或ComboBox。

MSDN ToolStripComboBox MSDN ToolStripComboBox
MSDN ToolStripTextBox MSDN工具条文本框

From the designer there is a small drop-down arrow when your mouse is in the "Type Here" box (sometimes hard to click) that will allow you to change the type. 当您的鼠标位于“此处键入”框中(有时很难单击)时,从设计器中会有一个小的下拉箭头,您可以通过该箭头更改类型。

If you are looking to allow for any type of control to be displayed in a top down fashion inside of a container to be positionable... you could always make a custom control using FlowLayoutPanel. 如果您希望允许在容器内部以自上而下的方式显示任何类型的控件,并且可以放置...,您始终可以使用FlowLayoutPanel进行自定义控件。 With it's properties FlowDirection=TopDown and WrapContents=False to keep a vertical approach. 通过它的属性FlowDirection = TopDown和WrapContents = False可以保持垂直方法。 This will handle your "menu" basics and your new control can expose whichever events you wish from each Control. 这将处理您的“菜单”基础知识,并且新控件可以公开您希望从每个控件获得的任何事件。 You will have to handle the logic of showing the panel and positioning with it's Location property as well. 您还必须使用其Location属性处理显示面板和定位的逻辑。

I forgot to address the issue with drawing outside of the parent form. 我忘了用父窗体外部的图形解决问题。 Notice that ContextMenus are smart and when they reach a boundary of their parent they draw away from it. 请注意,ContextMenus很聪明,当它们到达其父级的边界时,它们便远离了它。 You should logically be able to draw in the correct direction (Up/Down or Left/Right) from any right mouse click. 从逻辑上讲,您应该可以通过鼠标右键单击按正确的方向(上/下或左/右)进行绘制。 Per your attempt with a Form, set StartPosition=Manual then prior to calling Show() or ShowDialog() set it's Location property respective to the X and Y parameters provided in the event args of MouseClick. 根据您对Form的尝试,设置StartPosition = Manual,然后在调用Show()或ShowDialog()之前,将其Location属性分别设置为MouseClick事件参数中提供的X和Y参数。

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

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