在MouseDown上使对象跟随鼠标并在MouseUp上“粘贴”

时间:2011-04-14 06:07:21

标签: wpf vb.net cursor-position onmousedown onmouseup

我正在使用WPF和VB.net的项目。我想在视觉上模拟“拖动”一个对象(虽然我不想出于目的而使用标准的拖放)。

基本上,我有一个标签对象,在它的MouseDown事件中,我希望它跟随640x480实体大小网格内的鼠标光标(但不在它之外!)。请注意,此网格位于全屏窗口的中心。同样,对象不应该在网格之外跟随鼠标(我猜这里是“ClipToBounds = True”)

然后,在标签的MouseUp事件上,我希望它保持在当前位置或返回其原始位置,由另一个对象的MouseEnter属性设置的布尔变量的值确定。

请注意,如果它更容易使用,我可以将网格更改为cinch中的画布。我猜这是可取的。

所以,在那个冗长的解释之后,这是我的问题(双重):

  1. 如何使对象(标签)跟随网格/画布内的鼠标光标,但不在其外部?这需要在标签的MouseDown事件上发生。

  2. 如何让对象“粘住”在当前位置? (由此,我可以弄清楚如何让它自己回到原来的位置。:D)

  3. 我支持任何可以帮助我最有效地实现这一目标的人!非常感谢你们。

3 个答案:

答案 0 :(得分:5)

这样的事情怎么样:

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>

CODE-BEHIND:

    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))
            {
                r.SetValue(Canvas.LeftProperty, e.GetPosition(canv).X - (r.Width / 2));
                r.SetValue(Canvas.TopProperty, e.GetPosition(canv).Y - (r.Height / 2));
            }
        }
    }

    private void Rectangle_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        rec.ReleaseMouseCapture();
        isDragging = false;
    }

此代码可以增强,但我认为你有了这个想法;)

答案 1 :(得分:0)

基于@ Bruno's,这是我的解决方案:

double maxX;
double maxY;

private void OnRectMouseDown(object sender, MouseButtonEventArgs e)
{
    maxX = canv.ActualWidth - rect.Width;
    maxY = canv.ActualHeight - rect.Height;

    rect.CaptureMouse();
    rect.MouseMove += OnRectMouseMove;
    rect.MouseUp += OnRectMouseUp;
}

private void OnRectMouseMove(object sender, MouseEventArgs e)
{
    var pos = e.GetPosition(canv);
    var newX = pos.X - (rect.Width / 2);
    var newY = pos.Y - (rect.Height / 2);

    if (newX < 0) newX = 0;
    if (newX > maxX) newX = maxX;

    if (newY < 0) newY = 0;
    if (newY > maxY) newY = maxY;

    rect.SetValue(Canvas.LeftProperty, newX);
    rect.SetValue(Canvas.TopProperty, newY);

    xVal.Content = (newX / maxX).ToString("F3");
    yVal.Content = (newY / maxY).ToString("F3");
}

private void OnRectMouseUp(object sender, MouseButtonEventArgs e)
{
    rect.ReleaseMouseCapture();
    rect.MouseMove -= OnRectMouseMove;
    rect.MouseUp -= OnRectMouseUp;
}

答案 2 :(得分:0)

试试这个:

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
    (name).Location = new Point(e.X,e.Y);
}

这样,如果点击该对象就会显示在那里