我尝试拖放应用程序。我在运行时绘制了矩形,我想检测用户是否尝试移动此矩形
这是我的代码
private bool Mouse_Down = false;
Rectangle re = new Rectangle(100, 100, 60, 60);
private void DrawRegion_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.RoyalBlue), re);
}
private void DrawRegion_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse_Down == true)
{
re.Location = e.Location;
if (re.Right > DrawRegion.Width)
{
re.X = DrawRegion.Width - re.Width;
}
if (re.Top < 0)
{
re.Y = 0;
}
if (re.Left < 0)
{
re.X = 0;
}
if (re.Bottom > DrawRegion.Height)
{
re.Y = DrawRegion.Height - re.Height;
}
Refresh();
}
}
private void DrawRegion_MouseUp(object sender, MouseEventArgs e)
{
Mouse_Down = false;
}
private void DrawRegion_MouseDown(object sender, MouseEventArgs e)
{
Mouse_Down = true;
}
现在更多细节这个矩形移动用户点击这个矩形或任何空白空间所以我想检测点击的位置颜色像素是否是矩形颜色像素,然后移动矩形怎么做?
Note:DrawRegion is a picturebox
抱歉英文不好
答案 0 :(得分:3)
您可以使用Rect.Contains()
来检测您的Rectaingle是否包含您当前的位置
private void DrawRegion_MouseClick(object sender,MouseEventArgs e)
{
if (re.Contains(e.Location))
Mouse_Down = true;
else
Mouse_Down = false;
}
检查此https://msdn.microsoft.com/en-us/library/ms557979(v=vs.110).aspx