如何在控件中添加具有原始上下文菜单的上下文菜单
本文关键字:上下文 菜单 原始 添加 控件 | 更新日期: 2023-09-27 18:18:51
我将添加自定义contextMenuStrip到已经默认contextMenuStrip的控件。
代码片段:
DBDisplay imageControl = new DBDisplay(); // This is third-party object
imageControl.ContextMenuStrip = this.contextMenuStripTableLayout;
然而,默认的contextMenu已被更改为新的contextMenu。我想使用默认+新上下文菜单。有什么提示或帮助吗?
谢谢格兰特。我还有一个问题。
更新编辑
List<DBDisplay> m_pImage = new List<DBDisplay>();
for (int i = 0; i < 10; i++)
{
DBDisplay imageControl = new DBDisplay();
imageControl.Location = new System.Drawing.Point(0, 0);
imageControl.Dock = System.Windows.Forms.DockStyle.Fill;
imageControl.Size = new Size(columnWidth, rowHeight);
foreach (ToolStripItem tsItem in contextMenuStripTableLayout.Items)
imageControl.ContextMenuStrip.Items.Add(tsItem);
m_pImage.Add(imageControl);
}
int index = 0;
for (int i = 0; i < columnCount; i++)
{
//First add a column
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, columnWidth));
for (int j = 0; j < rowCount; j++)
{
if (i == 0)
{
//defining the size of cell
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
}
tableLayoutPanel1.Controls.Add(m_pImage[index], i, j);
index++;
}
}
我还有一个问题。如果我使用上面的代码,那么imageControl contextMenuStrip有10次重复。如何解决这个问题?
基本上,m_pImage列表将被添加到tableLayoutPanel1控件中。我将使用每个列和行上下文strip。接近对不对?
Update2好的,这里是详细的源代码。DBDisplay为DBCogDisplay
using System.ComponentModel;
using System.Windows.Forms;
using Cognex.VisionPro.Display;
namespace DBSubClass
{
public partial class DBCogDisplay : Cognex.VisionPro.Display.CogDisplay
{
public DBCogDisplay()
{
InitializeComponent();
SetLayoutSettings();
SetStyleSettings();
}
public DBCogDisplay(IContainer container)
{
container.Add(this);
InitializeComponent();
SetLayoutSettings();
SetStyleSettings();
}
private void SetLayoutSettings()
{
base.AllowDrop = true;
base.Dock = System.Windows.Forms.DockStyle.Fill;
base.Location = new System.Drawing.Point(0, 0);
base.MouseWheelMode = CogDisplayMouseWheelModeConstants.Zoom1;
base.ContextMenuStrip.AllowMerge = true;
}
private void SetStyleSettings()
{
base.DoubleBuffered = true;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();
}
}
}
下面是CogDisplay类的对象描述。http://www.hyperbook.com/cognex/vpro/ja/Cognex.VisionPro.Display.CogDisplay.html
对于ContextMenuStrip
,尝试使用ToolStripManager.Merge。我有没有的想法,如果这将在您的情况下工作,但通常它需要在第一个菜单中的所有项目,并将它们合并到第二个菜单。我以前在ContextMenuStrip
的两个实例上使用过这个,但我不知道DBDisplay
是什么,也不知道它是如何内部设计的。
if (ToolStripManager.Merge(imageControl.ContextMenuStrip, newContextMenuStrip))
imageControl.ContextMenuStrip = newContextMenuStrip;
首先你说ContextMenu
,但随后你的控制似乎实际上实现了ContextMenuStrip
。以防万一,对于ContextMenu
,您可以尝试使用菜单。MergeMenu:
imageControl.ContextMenu.MergeMenu(newContextMenu);
编辑:(在Changju更新了原来的问题之后)
您正在将所有10个imageControl
菜单合并到contextMenuStripTableLayout
中,因此您将看到菜单项重复10次。
我认为你只需要使用循环来添加它们:
foreach (ToolStripItem tsItem in contextMenuStripTableLayout.Items)
imageControl.ContextMenuStrip.Items.Add(tsItem);