当鼠标按下并“粘住”时使对象跟随鼠标鼠标松开时
我正在处理一个 WPF 和 VB.net 项目。我想在视觉上模拟“拖动”对象(尽管出于目的原因我不想使用标准拖放)。
基本上,我有一个标签对象,在其 MouseDown 事件中,我希望它在 640x480 实体大小网格内跟随鼠标光标(但不在其外部!)。请注意,该网格位于全屏窗口的中心。同样,该对象不应跟随网格外部的鼠标(我猜这里是“ClipToBounds = True”)
然后,在标签的 MouseUp 事件中,我希望它保持在当前位置或返回到其原始位置,由另一个对象的 MouseEnter 属性设置的布尔变量的值确定。
请注意,如果使用起来更容易,我可以轻松地将网格更改为画布。我猜这将是可取的。
因此,在冗长的解释之后,这是我的问题(两个):
如何使对象(标签)跟随网格/画布内的鼠标光标,而不是在其外部?这需要在标签的 MouseDown 事件上发生。
如何使对象“粘”在当前位置? (由此,我大概可以弄清楚如何让它回到原来的位置。:D)
我投票给任何能帮助我最有效地完成这个目标的人!非常感谢大家。
I'm working with a project that is WPF and VB.net. I want to visually simulate "dragging" an object (though I do not want to use standard drag and drop for reason of purpose).
Basically, I have a label object that, on its MouseDown event, I want it to follow the mouse cursor inside a 640x480 solid-size grid (but not outside of it!). Mind you, this grid is centered inside a full-screen window. Again, the object should not follow the mouse outside of the grid (I'm guessing a "ClipToBounds = True" here)
Then, on the label's MouseUp event, I want it to either stay in its current position or return to its original position, as determined by the value of a boolean variable set by another object's MouseEnter property.
Note, if it would be easier to work with, I can change the grid to a canvas in a cinch. I'm guessing that would be desirable.
So, after that long-winded explanation, here is my question (two-fold):
How do I make the object (label) follow the mouse cursor inside the grid/canvas, but not outside of it? This needs to happen on the MouseDown event of the label.
How do I make the object "stick" in its current position? (From this, I can probably figure out how to make it return to its original position on my own. :D )
My upvote to whoever can help me accomplish this goal the most efficiently! Thank you all very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西怎么样:
XAML:
CODE-BEHIND:
可以增强此代码,但我认为您已经明白了;)
How about something like this :
XAML :
CODE-BEHIND :
This code could be enhanced, but I think you got the idea ;)
基于@Bruno,这是我的解决方案:
Based on @Bruno's, this is my solution:
试试这个:
这样,如果您单击该对象就会出现在那里
try this:
so that will make it so if you click the object will appear there