Перемещение объекта мышкой - C#
Формулировка задачи:
public void Move(Keys key)
{
if (key == Keys.Left)
{
if (_123.X <= (_clientRectangle.Width + _QQSize.Width) && _123.X >= 0)
_123.Location = new Point(_123.Left - 20, _123.Top);
}
if (key == Keys.Right)
{
if (_123.X < (_clientRectangle.Width - _QQSize.Width))
_123.Location = new Point(_123.Left + 20, _123.Top);
}
}Решение задачи: «Перемещение объекта мышкой»
textual
Листинг программы
bool move;
Point current;
.................
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
move = true;
current = new Point(e.X, e.Y);
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (move)
{
Point newlocation = button1.Location;
newlocation.X += e.X - current.X;
newlocation.Y += e.Y - current.Y;
button1.Location = newlocation;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
move = false;
}