使对象在MouseDown上跟随鼠标并在屏幕上“粘贴".在MouseUp上 [英] Make Object Follow Mouse On MouseDown and "Stick" On MouseUp
问题描述
我正在使用WPF和VB.net的项目.我想在视觉上模拟拖动"一个对象(尽管出于目的,我不想使用标准的拖放操作.)
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).
基本上,我有一个标签对象,在其MouseDown事件上,我希望它跟随640x480实心尺寸网格内的鼠标光标(但不在其外部!).请注意,此网格位于全屏窗口的中央.同样,该对象不应跟随鼠标超出网格之外(我猜这里是"ClipToBounds = True")
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)
然后,在标签的MouseUp事件上,我希望它要么停留在当前位置,要么返回到其原始位置,该位置由另一个对象的MouseEnter属性设置的布尔变量的值确定.
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):
-
如何使对象(标签)跟随鼠标光标在网格/画布内部,而不是在网格/画布外部?这需要在标签的MouseDown事件上发生.
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.
如何使对象粘"在其当前位置? (由此,我大概可以弄清楚如何使它自己恢复到原始位置.:D)
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.
推荐答案
这样的事情如何:
XAML:
<Canvas x:Name="canv" ToolTip="tt one" Width="400" Height="400" Background="Blue">
<Rectangle x:Name="rec" Fill="Red" Height="50" Width="50" MouseDown="Rectangle_MouseDown" MouseMove="Rectangle_MouseMove" MouseUp="Rectangle_MouseUp" />
</Canvas>
代码隐藏:
private bool isDragging;
private void Rectangle_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
rec.CaptureMouse();
isDragging = true;
}
private void Rectangle_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (isDragging)
{
Point canvPosToWindow = canv.TransformToAncestor(this).Transform(new Point(0, 0));
Rectangle r = sender as Rectangle;
var upperlimit = canvPosToWindow.Y + (r.Height / 2);
var lowerlimit = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2);
var leftlimit = canvPosToWindow.X + (r.Width / 2);
var rightlimit = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2);
var absmouseXpos = e.GetPosition(this).X;
var absmouseYpos = e.GetPosition(this).Y;
if ((absmouseXpos > leftlimit && absmouseXpos < rightlimit)
&& (absmouseYpos > upperlimit && absmouseYpos < lowerlimit))
{
Canvas.SetLeft(r, e.GetPosition(canv).X - (r.Width / 2));
Canvas.SetTop(r, e.GetPosition(canv).Y - (r.Height / 2));
}
}
}
private void Rectangle_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
rec.ReleaseMouseCapture();
isDragging = false;
}
可以增强此代码,但我想您已经明白了;)
This code could be enhanced, but I think you got the idea ;)
这篇关于使对象在MouseDown上跟随鼠标并在屏幕上“粘贴".在MouseUp上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!