WPF 图像平移约束 [英] WPF Image Panning Constraints

查看:19
本文介绍了WPF 图像平移约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何约束平移,以便图像完全保持在其包含边框的范围内.在这方面的任何帮助将不胜感激.谢谢....

I'm trying to figure out how to constrain a pan so that the image remains entirely within the bounds of it's containing border. Any help this regard would be greatly appreciated. Thanks....

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MapTest.Window1"
    Title="PanTest"
    Width="484"
    Height="400"
    WindowStartupLocation="CenterScreen">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock Margin="8,8,8,0"
               TextWrapping="Wrap">
        To pan the image, simply click on it then drag.  I'd like to 
        constrain the pan so that the image remains entirely within 
        the border, but without resorting to ClipToBounds.  Any help 
        in this regard would be greatly appreciated.  Thanks....</TextBlock>
    <Border x:Name="border"
            Margin="8"
            BorderBrush="Black"
            BorderThickness="1"
            Grid.Row="1">
        <Image x:Name="image"
               Source="Penguins.jpg"
               Opacity="1"
               Width="300"
               Height="300"
               Stretch="Uniform"
               RenderTransformOrigin="0.5,0.5" />
    </Border>
</Grid>

using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace MapTest
{
    public partial class Window1 : Window
    {
        private Point origin;
        private Point start;

        public Window1()
        {
            InitializeComponent();

            var group = new TransformGroup();

            group.Children.Add(new TranslateTransform());

            image.RenderTransform = group;

            image.MouseLeftButtonDown += image_MouseLeftButtonDown;
            image.MouseMove += image_MouseMove;
            image.MouseLeftButtonUp += image_MouseLeftButtonUp;
        }

        private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            image.ReleaseMouseCapture();
        }

        private void image_MouseMove(object sender, MouseEventArgs e)
        {
            if (!image.IsMouseCaptured) 
                return;

            var tt = (TranslateTransform)((TransformGroup)image.RenderTransform).
                Children.First(tr => tr is TranslateTransform);

            var vector = start - e.GetPosition(border);

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

        private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            image.CaptureMouse();

            var tt = (TranslateTransform)((TransformGroup)image.RenderTransform).
                Children.First(tr => tr is TranslateTransform);

            start = e.GetPosition(border);

            origin = new Point(tt.X, tt.Y);
        }
    }
}

推荐答案

以下代码以相对于 Border 控件的坐标为您提供转换后的 Image 控件的边界.您可以轻松检查边界是否位于 Border 控件内.

The following code gives you the bounds of the transformed Image control in coordinates relative to the Border control. You could easily check if the bounds are located inside the Border control.

var rect = new Rect(image.RenderSize);
var bounds = image.TransformToAncestor(border).TransformBounds(rect);

这篇关于WPF 图像平移约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆