问题描述
我有一个应用程序,用户可以在表单上拖动控件。但他们
请求我的Snap-To行,使控制的对齐更容易。我不知道snep-to行和如何实现它们 - 我看过:
I have an app where users can drag controls around on a Form. But theyre asking me for Snap-To lines to make the alignment of controls easier. I have no idea about the snep-to lines and how to implement them - I have looked at:
Adorner的,但它说它只适用于WPF 。
http://msdn.microsoft.com/en-us/library/ms752100.aspx Adorner's, but it says it's only for WPF. And I tried it in WinForms but (as expected) didn't work.
如何在我的应用程序中获取snap-to行(类似VS中的行) ?
How can I get snap-to lines (something like the ones in VS) in my app?
谢谢
Bael
推荐答案
在你的移动控制中,你可以分开调整左
和顶部
乘以你的线宽:
In your move control you could adjust the Left
and Top
by dividing and then multiplying by the width of your lines:
left = (left/10)*10;
top = (top/10)*10;
这不是完美的,但它很简单。当然,由于控件没有MoveEnd事件,你必须跟踪MouseButton状态或类似的东西。
It's not perfect but it it simple. Of course since controls don't have a MoveEnd event you'll have to track the MouseButton state or something similiar.
编辑:实施将适当地舍入除法结果,因此134 = 130和136 = 140。
A better implementation would properly round the division results so 134 = 130 and 136 = 140.
这篇关于在运行时对齐控件时的对齐线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!