我有一个Canvas是WPF,其中包含很多形状和线条。我在画布中包含了“缩放和平移”功能。我有一个类似搜索的功能,它以X和Y作为输入,当我请求搜索时,画布应放大到该点并以(X,Y)位置为中心的方式平移画布。缩放效果很好,但是我无法实现Pan。
我在this post的帮助下将以下代码用于PanBorder.cs。在这里,我想使用PanToPosition(double x,double y)进行翻译,使中心位于(X,Y)处。

class PanBorder : Border
{
    private UIElement child = null;
    private Point origin;
    private Point start;private TranslateTransform GetTranslateTransform(UIElement element)
    {
        return (TranslateTransform)((TransformGroup)element.RenderTransform)
          .Children.First(tr => tr is TranslateTransform);
    }

    private ScaleTransform GetScaleTransform(UIElement element)
    {
        return (ScaleTransform)((TransformGroup)element.RenderTransform)
          .Children.First(tr => tr is ScaleTransform);
    }

    public override UIElement Child
    {
        get { return base.Child; }
        set
        {
            if (value != null && value != this.Child)
                this.Initialize(value);
            base.Child = value;
        }
    }

    public void Initialize(UIElement element)
    {
        this.child = element;
        if (child != null)
        {
            TransformGroup group = new TransformGroup();
            ScaleTransform st = new ScaleTransform();
            group.Children.Add(st);
            TranslateTransform tt = new TranslateTransform();
            group.Children.Add(tt);
            child.RenderTransform = group;
            child.RenderTransformOrigin = new Point(0.0, 0.0);

            this.MouseLeftButtonDown += child_MouseLeftButtonDown;
            this.MouseLeftButtonUp += child_MouseLeftButtonUp;
            this.MouseMove += child_MouseMove;
        }
    }

    public void Reset()
    {
        if (child != null)
        {
            // reset pan
            var tt = GetTranslateTransform(child);
            tt.X = 0.0;
            tt.Y = 0.0;
        }
    }

    public void PanToPosition(double x, double y) {
        if (child != null)
        {
            var tt = GetTranslateTransform(child);

            //Pan such that center is at (X,Y)

        }
    }
    #region Child Events

    private void child_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (child != null)
        {
            var tt = GetTranslateTransform(child);
            start = e.GetPosition(this);
            origin = new Point(tt.X, tt.Y);
            this.Cursor = Cursors.Hand;
            child.CaptureMouse();
        }
    }

    private void child_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (child != null)
        {
            child.ReleaseMouseCapture();
            this.Cursor = Cursors.Arrow;
        }
    }

    private void child_MouseMove(object sender, MouseEventArgs e)
    {
        if (child != null)
        {
            if (child.IsMouseCaptured)
            {
                var tt = GetTranslateTransform(child);
                Vector v = start - e.GetPosition(this);
                tt.X = origin.X - v.X;
                tt.Y = origin.Y - v.Y;
            }
        }
    }

    #endregion
}`


我有以下代码可放大到画布:

//for zooming control on the window
    void window_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        Point p = e.MouseDevice.GetPosition(canvasWaSNA);

        Matrix m = canvasWaSNA.RenderTransform.Value;
        if (e.Delta > 0)
            m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
        else
            m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);

        canvasWaSNA.RenderTransform = new MatrixTransform(m);
    }


当我输入点(X,Y)时,我会调用以下函数:

public void moveToPosition(double x, double y) {
        resize();
        border.PanToPosition(x, y);
        Point p = new Point(x, y);
        Matrix m = canvasWaSNA.RenderTransform.Value;
        m.ScaleAtPrepend(6, 6, p.X, p.Y);
        canvasWaSNA.RenderTransform = new MatrixTransform(m);
    }


要调整画布的大小,我有以下代码:

private void resize()
    {
        Matrix m = canvasWaSNA.RenderTransform.Value;
        m.SetIdentity();
        canvasWaSNA.RenderTransform = new MatrixTransform(m);
        border.Reset();
    }


我需要帮助。提前感谢。

最佳答案

好吧,我终于做到了。我应用了平移点的简单概念,因为我的问题是将Point(X,Y)平移到屏幕中心并应用缩放。这是更新的PanToPosition函数:

public void PanToPosition(double x, double y, Point center) {
        if (child != null)
        {
            var tt = GetTranslateTransform(child);
            start = new Point(x,y);
            Point p = new Point(center.X, center.Y);
            origin = new Point(tt.X, tt.Y);
            Vector v = start - p;

            tt.X = origin.X - v.X;
            tt.Y = origin.Y - v.Y;
        }
    }


我将此函数称为:

public void moveToPosition(double x, double y) {
        resize();
        Point center = new Point(this.Width / 2, this.Height / 2);
        border.PanToPosition(x, y, center);
        Point p = new Point(x, y);
        Matrix m = canvasWaSNA.RenderTransform.Value;
        m.ScaleAtPrepend(6, 6, p.X, p.Y);
        canvasWaSNA.RenderTransform = new MatrixTransform(m);
    }


在这里,我通过了屏幕的中心点和要平移到中心的点。起点是要翻译的点,p是目的地。因此,我计算了翻译比例因子。

关于c# - 平移 Canvas ,使Point(x,y)居中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35307216/

10-13 03:18