如何捕获移动的自定义控件
我尝试解释我的问题:
我在面板上有许多相同类型的自定义控件。
在运行时,我可以使用 Bringtofront() 自由移动表单中的任何自定义控件( mousedown 和 mousemove 事件),将鼠标悬停在任何其他自定义控件上,以将其覆盖。
现在我需要知道我正在移动的控件何时高于任何其他控件以及位于哪个控件之下。
到目前为止,我已经尝试使用拖放事件,但没有成功,还有其他方法可以做到这一点,或者我必须用拖动事件重新详细说明它?
谢谢大家的帮助
,并对我的英语感到抱歉。
编辑:
控件是纸牌游戏,我的最终目标是使用纸牌根据纸牌价值和/或颜色的规则创建逻辑序列。
I try to explain my problem:
I got many customcontrols of same type on a panel.
At runtime I'm free to move any custumcontrol ( mousedown and mousemove events) in the form with mouse over any others customcontrols using bringtofront() to have it over all.
Now I need to know when the control I'm moving is over any other control and which control is under.
So far I've tryed to use drag-n-drop events but without success, there's some other way to do this or I've to re elaborate it with drag events?
thank you all for help
and sorry for my English.
EDIT:
The controls are cards game and my final goal is to use cards to create logical sequence following rules based on card value and/or color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法通过拖放来解决问题,因为您实际上并没有将一个控件拖放到另一个控件上。您只是在表单中移动它们。
您需要熟悉Z 顺序,这是虚拟 3D 空间中对象从前到后的排列方式。要调整 Z 顺序,您可以调用 <代码>BringToFront和从
System.Windows.Forms.Control
派生的任何对象的SendToBack
方法。Z 顺序也由父级(容器)控件的
控件
集合。使用GetChildIndex
方法,您可以确定Z 顺序中任何控件的位置。所以现在,您需要做的就是弄清楚您所拖动的控件是哪个控件结束的。通过比较
位置属性
。当您知道两个控件的位置重叠时,请检查它们各自的 Z 顺序索引以查看哪一个位于顶部。
You can't solve the problem with drag-and-drop because you're not actually dropping one control onto the other control. You're merely moving them around in the form.
You need to become familiar with Z order, which is the front-to-back arrangement of objects in a virtual 3D space. To adjust the Z order, you can call the
BringToFront
andSendToBack
methods of any object that derives fromSystem.Windows.Forms.Control
.The Z order is also exposed by the parent (container) control's
Controls
collection. Using theGetChildIndex
method, you can determine the position of any control within the Z order.So now, all you need to do is figure out which control the control you're dragging is over. Do that by comparing the
Location
properties of the two controls. When you know that the locations of two controls overlap, check their respective Z order indices to see which one is on top.