将画布平移,使点(x,y)位于中心。

14 浏览
0 Comments

将画布平移,使点(x,y)位于中心。

我在WPF中有一个包含很多形状和线条的Canvas。我在Canvas中加入了缩放和平移功能。我有一个类似搜索的功能,可以通过输入X和Y来搜索。当我请求搜索时,Canvas应该缩放到该点并将Canvas平移到这样一个位置,使得(X,Y)位置在中心。缩放功能运行良好,但我无法实现平移。

我使用了以下代码来实现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)
        {
            // 重置平移
            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);
            // 平移到中心点 (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
}

我有以下代码来缩放Canvas:

// 用于窗口的缩放控件
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);
}

为了调整Canvas的大小,我有以下代码:

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

我需要帮助。提前谢谢。

0