拖放功能与绘制矩形C#.NET - 表格 [英] Drag and Drop Function with Drawing Rectangle C# .net - Forms
本文介绍了拖放功能与绘制矩形C#.NET - 表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想拖,我已经画在形式下降。这里是我的绘制矩形代码。一个能正常工作。
I want to drag and drop that I have drawn on the forms. Here is my code for drawing the rectangle. An this works fine.
Rectangle rec = new Rectangle(0, 0, 0, 0);
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Aquamarine, rec);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rec = new Rectangle(e.X, e.Y, 0, 0);
Invalidate();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rec.Width = e.X - rec.X;
rec.Height = e.Y - rec.Y;
Invalidate();
}
}
现在我想拖放矩形中不同的地方。
Now I want to drag and drop that rectangle in to a different place.
请帮助如何做到这一点。
Pls help How to do that
感谢您
YOHAN
推荐答案
我写的这些东西一个辅助类:
I have written a helper class for this kind of stuff:
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);
}
};
}
}
然后我初始化它在我的窗体加载事件我的控制:
Then I just Initialize it in my form load event with my control:
ControlMover.Init(myControl, myContainer, ControlMover.Direction.Any);
好了,你没有控制移动。这是一个矩形。但我们希望,你会得到的想法结果
更新:你检查出这个页面中列出的相关问题?尝试:结果
拖放在C#中下降矩形
这篇关于拖放功能与绘制矩形C#.NET - 表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文