在应用变换之前检查变换值

时间:2018-04-13 10:22:08

标签: wpf

我使用渲染变换来平移图像。我需要在某个时刻停止平移,所以我要检查图像的边界。问题是图像可以略微超出边界,因为我在应用变换之前检查边界框。有没有办法将变换应用到边界框,然后检查它是否在有效范围内?

感谢您的帮助

private void RecalculatePositions(MouseEventArgs e)
{
        TranslateTransform tt1 = (TranslateTransform)((TransformGroup)this.image1.RenderTransform).Children.First(tr => tr is TranslateTransform);
        TranslateTransform tt2 = (TranslateTransform)((TransformGroup)this.image2.RenderTransform).Children.First(tr => tr is TranslateTransform);
        Vector vector = this._startPoint - e.GetPosition(this.grid);

        double offsetX = this._originPoint.X - vector.X;
        double offsetY = this._originPoint.Y - vector.Y;

        Rect image1Bounds = this.image1.TransformToAncestor(this.viewBox1).TransformBounds(new Rect(this.image1.RenderSize));

        if (vector.X < 0)
        {
            //Moving image right
            if (offsetX <= 0)
            {
                tt1.X = offsetX;
                tt2.X = offsetX;
            }
        }
        else
        {
            //Moving image left
            if (image1Bounds.Right >= 0)
            {
                tt1.X = offsetX;
                tt2.X = offsetX;
            }
        }

        if (vector.Y < 0)
        {
            //Moving image down
            if (offsetY <= 0)
            {
                tt1.Y = offsetY;
                tt2.Y = offsetY;
            }
        }
        else
        {
            //Moving image up
            if (image1Bounds.Bottom >= this.ActualHeight)
            {
                tt1.Y = offsetY;
                tt2.Y = offsetY;
            }
        }
}

0 个答案:

没有答案