0

我遇到了一些问题,希望有人能回答这个问题。我有一个 Deep Zoom 项目,我使用了标准(deep zoom composer)项目,它在 MultiScaleImage 控件上放置了 DeepZommInitializer 行为。我试图限制拖动以确保用户不会将图像拖出屏幕(因此无法找到图像)。我确实添加了一个主页按钮,它将以 1 的缩放将图像带回起始位置。无论如何,这是我目前拥有的代码(已经在互联网上搜索以寻找答案)。

        // msi is the multiscale image
        msi.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
        {
            lastMouseDownPos = e.GetPosition(msi); // class level var
            lastMouseViewPort = msi.ViewportOrigin; // class level var

            mouseDown = true; // class level var

            msi.CaptureMouse();
        };

        msi.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            lastMousePos = e.GetPosition(msi);

            if (duringDrag) 
            {
                Point newPoint = lastMouseViewPort;

                newPoint.X += (lastMouseDownPos.X - lastMousePos.X) / msi.ActualWidth * msi.ViewportWidth;
                newPoint.Y += (lastMouseDownPos.Y - lastMousePos.Y) / msi.ActualWidth * msi.ViewportWidth;

                var limits = new Rect(new Point(1, 1 / msi.AspectRatio), new Point(-1, -1 / msi.AspectRatio));

                if (newPoint.X > limits.Right * .999)
                {
                    newPoint.X += (-2 * (lastMouseDownPos.X - lastMousePos.X)) / msi.ActualWidth * msi.ViewportWidth; // Reverses direction when going off left

                }

                if (newPoint.Y > limits.Bottom * .999)
                {
                    newPoint.Y += (-2 * (lastMouseDownPos.Y - lastMousePos.Y)) / msi.ActualWidth * msi.ViewportWidth; // Reverses direction when going off top of screen
                }

                msi.ViewportOrigin = lastMouseViewPort = newPoint;
                lastMouseDownPos = lastMousePos;
            }
        };

我真的需要一个适用于右下角的解决方案,但是当我放大所有值时,所有值都会发生变化。如果缩放级别为 1,我的限制代码有效。我不敢相信这在互联网上无处可寻!但是,当缩放发生变化时,一切都会消失(newPoint 的值不在我期望的范围内)。任何帮助都会很棒!

4

2 回答 2

0

就这样,我自己想通了(终于在几周后)。

答案如下(简单改变鼠标移动事件如下):

        msi.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            lastMousePos = e.GetPosition(msi);

            if (duringDrag) 
            {
                Point newPoint = lastMouseViewPort;

                newPoint.X += (lastMouseDownPos.X - lastMousePos.X) / msi.ActualWidth * msi.ViewportWidth;
                newPoint.Y += (lastMouseDownPos.Y - lastMousePos.Y) / msi.ActualWidth * msi.ViewportWidth;
                var limits = new Rect(new Point(1, 1 / msi.AspectRatio), new Point(-1 / Settings.ZoomLevel, -1 / msi.AspectRatio / Settings.ZoomLevel));

                if (newPoint.X < limits.Left * .999)
                {
                    newPoint.X += (-2*(lastMouseDownPos.X - lastMousePos.X)) / msi.ActualWidth * msi.ViewportWidth;  // we went off right so reverse X direction

                }
                if (newPoint.X > limits.Right * .999)
                {
                    newPoint.X += (-2 * (lastMouseDownPos.X - lastMousePos.X)) / msi.ActualWidth * msi.ViewportWidth; // we went off left so reverse X direction
                }
                if (newPoint.Y < limits.Top * .999)
                {
                    newPoint.Y += (-2*(lastMouseDownPos.Y - lastMousePos.Y)) / msi.ActualWidth * msi.ViewportWidth;  // we went off Bottom so reverse direction
                }
                if (newPoint.Y > limits.Bottom * .999)
                {
                    newPoint.Y += (-2 * (lastMouseDownPos.Y - lastMousePos.Y)) / msi.ActualWidth * msi.ViewportWidth; // we went off Top so reverse direction
                }
                msi.ViewportOrigin = lastMouseViewPort = newPoint;
                lastMouseDownPos = lastMousePos;

                msi.SendMovedZoomMsg(Settings.ZoomLevel, newPoint, myClassName);
            }
        };

这可能不会完全限制图像,但它会将其保留在屏幕上,并且用户很可能会停止尝试朝错误的方向移动

于 2010-06-18T16:22:03.113 回答
0

我相信下面的代码正确地确定了 ViewportOrigin 是否在屏幕上留下了图像的某些部分。(ImageCtrl 是 MultiScaleImage 的类变量)。

    private bool IsValidOrigin(Point point)
{
  var limits = new Rect(
                 new Point(-0.95 * ImageCtrl.ViewportWidth, -0.85 * ImageCtrl.ViewportWidth/ ImageCtrl.AspectRatio), 
                 new Point(0.95, 0.85/ImageCtrl.AspectRatio));
  return limits.Contains(point);
}
于 2011-04-08T04:06:10.937 回答