在鼠标移动中设置边界

时间:2018-10-10 00:59:19

标签: c# mouseevent

当在其中拖动图像时,如何防止鼠标取出我的pictureBox(500 x 500像素)

enter image description here

这是鼠标事件:

private void pictureBox_Canvass_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            drag = true;
            //start = new Point(e.Location.X + (int)imageRect.Location.X, e.Location.Y + (int)imageRect.Location.Y);
            start = new Point((int)Shape.center.X - ((int)imageRect.Location.X - e.X), (int)Shape.center.Y - ((int)imageRect.Location.Y - e.Y));
        }
    }
    private void pictureBox_Canvass_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && drag == true)
        {
            Point loc = new Point((int)((e.X - start.X) - (imageRect.Width / 2)), (int)((e.Y - start.Y) - (imageRect.Height / 2)));
            start.Offset(loc.X, loc.Y);
            imageRect.Location = start;
            Debug.WriteLine(start);
            pictureBox_Canvass.Invalidate();
        }
    }
    private void pictureBox_Canvass_MouseUp(object sender, MouseEventArgs e)
    {
        drag = false;
    }

0 个答案:

没有答案