我的WinForms应用程序中有一个画布(面板控件),用户可以在其中拖动文本框,标签等内容.但我想让它们更容易更精确地对齐对象.我已经读过它并且Adorners似乎是要走的路?但是,显然它只适
我想要完成的是每次用户在画布中拖动对象时都会弹出行…它们是如何在Windows窗体设计器视图中工作的.
我会感激任何帮助.
谢谢.
谢谢大家的答案.我已经设法提出了自己的解决方案.代码在下面.还没有“线”,但是我会继续它…
Label l = (Label)sender; foreach (Control control in Canvas.Controls) { if (l.Location.X > control.Location.X + control.Size.Width && l.Location.X < control.Location.X + control.Size.Width + 5) l.Location = new Point(control.Location.X + control.Size.Width + 5, l.Location.Y); else if (l.Location.X < control.Location.X - l.Size.Width && l.Location.X > control.Location.X - l.Size.Width - 5) l.Location = new Point(control.Location.X - l.Size.Width - 5, l.Location.Y); else if (l.Location.Y > control.Location.Y + control.Size.Height && l.Location.Y < control.Location.Y + control.Size.Height + 5) l.Location = new Point(l.Location.X, control.Location.Y + control.Size.Height + 5); else if (l.Location.Y < control.Location.Y - control.Size.Height && l.Location.Y > control.Location.Y - control.Size.Height - 5) l.Location = new Point(l.Location.X, l.Location.Y - 5); this.Update(); }
上面的代码必须放在Control_MouseMove事件中,当然,你仍然需要自己的代码来实际移动控件.
上面的代码将捕捉您将5像素拖动到最近控件的右侧,左侧,顶部或底部的控件.