C# 向项目添加右键菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9823883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a right click menu to an item
提问by Marshal
I have been searching for a while for a simple right-click menu for a single item. For example if I right-click on a picture I want a little menu to come up with my own labels: Add, Remove etc. If anyone could help I would be most greatful.
我一直在为单个项目寻找一个简单的右键单击菜单。例如,如果我右键单击一张图片,我需要一个小菜单来显示我自己的标签:添加、删除等。如果有人可以提供帮助,我将非常感激。
Thanks for looking.
谢谢你看。
Here is the completed code:
这是完成的代码:
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Item 1", new EventHandler(Removepicture_Click));
cm.MenuItems.Add("Item 2", new EventHandler(Addpicture_Click));
pictureBox1.ContextMenu = cm;
采纳答案by Yuki Kutsuya
Add a contextmenu to your form and then assign it in the control's properties under ContextMenuStrip. Hope this helps :).
向窗体添加上下文菜单,然后在 ContextMenuStrip 下的控件属性中分配它。希望这可以帮助 :)。
Hope this helps:
希望这可以帮助:
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Item 1");
cm.MenuItems.Add("Item 2");
pictureBox1.ContextMenu = cm;
回答by Cheesus Toast
This is a comprehensive answer to this question. I have done this because this page is high on the Google search results and the answer does not go into enough detail. This post assumes that you are competent at using Visual Studio C# forms. This is based on VS2012.
这是对这个问题的全面回答。我这样做是因为此页面在 Google 搜索结果中排名靠前,而答案没有提供足够详细的信息。这篇文章假设您有能力使用 Visual Studio C# 表单。这是基于VS2012的。
Start by simply dragging a ContextMenuStrip onto the form. It will just put it into the top left corner where you can add your menu items and rename it as you see fit.
You will have to view code and enter in an event yourself on the form. Create a mouse down event for the item in question and then assign a right click event for it like so (I have called the ContextMenuStrip "rightClickMenuStrip"):
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Right: { rightClickMenuStrip.Show(this, new Point(e.X, e.Y));//places the menu at the pointer position } break; } }Assign the event handler manually to the form.designer (you may need to add a "using" for System.Windows.Forms; You can just resolve it):
this.pictureBox1.MouseDown += new MouseEventHandler(this.pictureBox1_MouseDown);All that is needed at this point is to simply double click each menu item and do the desired operations for each click event in the same way you would for any other button.
首先将 ContextMenuStrip 拖到窗体上。它只会将它放在左上角,您可以在其中添加菜单项并根据需要重命名。
您必须查看代码并自己在表单上输入事件。为相关项目创建一个鼠标按下事件,然后为它分配一个右键单击事件,如下所示(我将 ContextMenuStrip 称为“rightClickMenuStrip”):
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Right: { rightClickMenuStrip.Show(this, new Point(e.X, e.Y));//places the menu at the pointer position } break; } }将事件处理程序手动分配给 form.designer(您可能需要为 System.Windows.Forms 添加“使用”;您可以解决它):
this.pictureBox1.MouseDown += new MouseEventHandler(this.pictureBox1_MouseDown);此时所需要的只是简单地双击每个菜单项,并以与任何其他按钮相同的方式为每个单击事件执行所需的操作。
This is the basic code for this operation. You can obviously modify it to fit in with your coding practices.
这是此操作的基本代码。您显然可以修改它以适应您的编码实践。
回答by Dorku
If you are using Visual Studio, there is a GUI solution as well:
如果您使用的是 Visual Studio,还有一个 GUI 解决方案:
- From Toolbox add a ContextMenuStrip
- Select the context menu and add the right click items
- For each item set the click events to the corresponding functions
- Select the form / button / image / etc (any item) that the right click menu will be connected
- Set its ContextMenuStripproperty to the menu you have created.
- 从 Toolbox 添加一个ContextMenuStrip
- 选择上下文菜单并添加右键单击项
- 对于每个项目,将点击事件设置为相应的功能
- 选择右键菜单将连接的表单/按钮/图像/等(任何项目)
- 将其ContextMenuStrip属性设置为您创建的菜单。
回答by Paul
Having just messed around with this, it's useful to kjnow that the e.X / e.Y points are relative to the control, so if (as I was) you are adding a context menu to a listview or something similar, you will want to adjust it with the form's origin. In the example below I've added 20 to the x/y so that the menu appears slightly to the right and under the cursor.
刚刚弄乱了这个,知道 eX / eY 点是相对于控件是很有用的,所以如果(就像我一样)你正在向列表视图或类似的东西添加上下文菜单,你会想要调整它表格的来源。在下面的示例中,我将 20 添加到 x/y,以便菜单稍微向右并位于光标下方。
cmDelete.Show(this, new Point(e.X + ((Control)sender).Left+20, e.Y + ((Control)sender).Top+20));

