我尝试了一个全局变量标志bool name draw。但它不工作,它永远不会显示消息离开事件。
我用一个断点进行了检查,它确实到达了if行的leave事件,但没有产生我想要的效果。
我想要的效果,只有当按住鼠标左键,同时移动鼠标,然后如果离开pictureBox2区域,然后显示消息。
private bool draw = false;
public Form1()
{
InitializeComponent();
InitializeForm();
}
private void InitializeForm()
{
textBox2.Text = Properties.Settings.Default.CroppedImagesFolder;
selectedPath = textBox2.Text;
if (!string.IsNullOrEmpty(textBox1.Text))
{
pictureBox2.Image = Image.FromFile(textBox1.Text);
}
checkBoxDrawBorder.Checked = true;
checkBoxClearRectangles.Checked = true;
checkBoxSaveRectangles.Checked = true;
LoadFile(textBox2, "folder.txt");
if (!string.IsNullOrEmpty(selectedPath) && Directory.Exists(selectedPath))
{
LoadFile(textBox1, "image.txt");
LoadRectangles();
}
}
public class DrawingRectangle
{
public Rectangle Rect => new Rectangle(Location, Size);
public Size Size { get; set; }
public Point Location { get; set; }
public Control Owner { get; set; }
public Point StartPosition { get; set; }
public Color DrawingcColor { get; set; } = Color.LightGreen;
public float PenSize { get; set; } = 3f;
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && pictureBox2.Image != null && selectedPath != null)
{
currentRect = new DrawingRectangle()
{
Location = e.Location,
Size = Size.Empty,
StartPosition = e.Location,
Owner = (Control)sender,
DrawingcColor = SelectedColor
};
}
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (currentRect != null && e.Button == MouseButtons.Left)
{
draw = true;
Point mouseLocation = pictureBox2.PointToClient(Cursor.Position);
int width = mouseLocation.X - currentRect.Location.X;
int height = mouseLocation.Y - currentRect.Location.Y;
width = Math.Max(width, 0);
height = Math.Max(height, 0);
currentRect.Size = new Size(width, height);
pictureBox2.Invalidate();
}
}
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
if (currentRect != null)
{
draw = false;
DrawingRects.Add(currentRect);
currentRect = null;
ProcessRectangles();
}
}
private void pictureBox2_MouseLeave(object sender, EventArgs e)
{
if (draw)
{
MessageBox.Show("You Leave At: " + MousePosition.X.ToString() + " --- " + MousePosition.Y.ToString());
}
}
字符串
1条答案
按热度按时间ccgok5k51#
你看到这种行为(或没有这种行为)的原因是鼠标处于Capture状态。如果你不需要捕获它,你可以在鼠标按下时取消捕获它,让它按照你描述的方式工作:
的数据
字符串
或者,你可以不去管
Capture
,自己实现鼠标离开行为:型