在form1构造函数中我做了:
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Save Rectangle");
cm.MenuItems.Add("Reset Rectangle");
cm.MenuItems.Add("View Rectangle Information");
pictureBoxSnap.ContextMenu = cm;
然后我在pictureBoxSnap上绘制了一个矩形,然后如果我在pictureBox上右键单击鼠标,我看到了菜单项,但是我绘制的矩形被删除了。如何才能使矩形保持不变?
这是油漆事件:
private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
{
if (pictureBoxSnap.Image != null)
{
if (ClearGraphics == false)
{
if (rectangles[listBoxSnap.SelectedIndex] != Rectangle.Empty)
{
e.Graphics.DrawRectangle(Pens.Firebrick, rectangles[listBoxSnap.SelectedIndex]);
}
}
if (cropRect == true)
{
if (recttest.Width > 10 && recttest.Height > 10)
{
pnt = PointToScreen(pictureBoxSnap.Location);
e.Graphics.Clear(Color.White);
e.Graphics.CopyFromScreen(pnt.X + rect.X, pnt.Y + rect.Y, rect.X, rect.Y, new Size(rect.Width, rect.Height));
}
}
}
}