Я пытаюсь создать пользовательский элемент управления, который я буду перетаскивать и перемещать в графическое окно.
Прямо сейчас мне удается сделать это с помощью некоторого класса, который я нашел в Интернете. Но я заметил, что могу перетаскивать пользовательский элемент управления, когда мой курсор находится в прозрачной области, а не когда я щелкаю объект в нем? Есть ли другой подход к этому?
ControlMover.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GridSnap
{
class ControlMover
{
public enum Direction
{
Any,
Horizontal,
Vertical
}
public static void Init(Control control)
{
Init(control, Direction.Any);
}
public static void Init(Control control, Direction direction)
{
Init(control, control, direction);
}
public static void Init(Control control, Control container, Direction direction)
{
bool Dragging = false;
Point DragStart = Point.Empty;
control.MouseDown += delegate (object sender, MouseEventArgs e)
{
Dragging = true;
DragStart = new Point(e.X, e.Y);
control.Capture = true;
};
control.MouseUp += delegate (object sender, MouseEventArgs e)
{
Dragging = false;
control.Capture = false;
};
control.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
}
};
}
}
}
Gate.cs (контроль пользователя)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace GridSnap
{
public partial class Gates : UserControl
{
public Gates()
{
InitializeComponent();
GridSnap.ControlMover.Init(this,ControlMover.Direction.Any);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Gates_Load(object sender, EventArgs e)
{
}
}
}
![enter image description here](https://i.stack.imgur.com/ELEAh.png)