如何向绘制的矩形添加上下文菜单?
How to add a context menu to a drawn rectangle?
我想为动态绘制的矩形添加上下文菜单。
所有矩形都存储在一个列表中。我该怎么做?
foreach (Rectangle item in PadRects)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
Pen mypen = new Pen(Color.White, 1);
g.DrawRectangle(mypen, item);
}
}
我想显示带有删除矩形按钮的上下文菜单。
谢谢!
在您的表单中添加 ContextMenuStrip
。
不要 select 它作为 pictureBox1
的 ContextMenuStrip
属性,因为这会自动打开上下文菜单。我们不想那样做,因为我们想测试鼠标是否真的指向绘制的矩形,然后才打开上下文菜单。
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Rectangle rect = PadRects
.Where(r => r.Contains(e.Location))
.FirstOrDefault();
if (!rect.IsEmpty) {
_clickedRectangle = rect; // Save the rectangle in a field to make it available
// to the context menu item handler.
contextMenuStrip1.Show(pictureBox1, e.Location);
}
}
我想为动态绘制的矩形添加上下文菜单。 所有矩形都存储在一个列表中。我该怎么做?
foreach (Rectangle item in PadRects)
{
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
Pen mypen = new Pen(Color.White, 1);
g.DrawRectangle(mypen, item);
}
}
我想显示带有删除矩形按钮的上下文菜单。
谢谢!
在您的表单中添加 ContextMenuStrip
。
不要 select 它作为 pictureBox1
的 ContextMenuStrip
属性,因为这会自动打开上下文菜单。我们不想那样做,因为我们想测试鼠标是否真的指向绘制的矩形,然后才打开上下文菜单。
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Rectangle rect = PadRects
.Where(r => r.Contains(e.Location))
.FirstOrDefault();
if (!rect.IsEmpty) {
_clickedRectangle = rect; // Save the rectangle in a field to make it available
// to the context menu item handler.
contextMenuStrip1.Show(pictureBox1, e.Location);
}
}