3

我是 WPF 的新手,在玩了很多之后,我终于按照我正在工作的项目中想要的方式安排了我的控件。我有一个显示图像的矩形。鼠标滚轮用动画缩放图像,ScaleTransform鼠标单击/拖动在屏幕上移动图像。问题是如果我放大或缩小图像然后尝试移动它,它会自动将其自身缩放回 1(原始大小)。有没有办法在缩放时移动它?编辑:我尝试将 ScaleTransform 和 TranslateTransform 设置为 TransformGroup,但仍然没有用。

我遇到的另一个问题是,如果图像大于父容器,它会超出范围,我尝试ClipToBounds = True在父容器上进行设置,但它不起作用。

代码:

private void Window_MouseWheel_1(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
    {
        ScaleTransform scaleP = new ScaleTransform();
        scaleP.CenterX = e.GetPosition(this).X;
        scaleP.CenterY = e.GetPosition(this).Y;

        rect.RenderTransform = scaleP;

        DoubleAnimation dblAnimX = new DoubleAnimation();
        dblAnimX.From = scaleXFrom;
        dblAnimX.To = scaleXTo + 0.1;
        scaleXFrom = scaleXTo +0.1;
        scaleXTo += 0.1;
        Duration = new Duration(TimeSpan.FromSeconds(0.15));

        DoubleAnimation dblAnimY = new DoubleAnimation();
        dblAnimY.From = scaleYFrom;
        dblAnimY.To = scaleYTo + 0.1;
        scaleYFrom = scaleYTo +0.1;
        scaleYTo += 0.1;
        Duration = new Duration(TimeSpan.FromSeconds(0.15));

        scaleP.BeginAnimation(ScaleTransform.ScaleXProperty, dblAnimX);
        scaleP.BeginAnimation(ScaleTransform.ScaleYProperty, dblAnimY);
    }
    else
    {
        ScaleTransform scaleM = new ScaleTransform();
        scaleM.CenterX = e.GetPosition(this).X;
        scaleM.CenterY = e.GetPosition(this).Y;

        rect.RenderTransform = scaleM;                           

        DoubleAnimation dblAnimX = new DoubleAnimation();
        dblAnimX.From = scaleXFrom;
        dblAnimX.To = scaleXTo -0.1;
        scaleXFrom = scaleXTo -0.1;
        scaleXTo -= 0.1;
        dblAnimX.Duration = new Duration(TimeSpan.FromSeconds(0.15));

        DoubleAnimation dblAnimY = new DoubleAnimation();
        dblAnimY.From = scaleYFrom;
        dblAnimY.To = scaleYTo - 0.1;
        scaleYFrom = scaleYTo -0.1;
        scaleYTo -= 0.1;
        Duration = new Duration(TimeSpan.FromSeconds(0.15));

        scaleM.BeginAnimation(ScaleTransform.ScaleXProperty, dblAnimX);
        scaleM.BeginAnimation(ScaleTransform.ScaleYProperty, dblAnimY);
    }
}

private void rect_MouseMove_1(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        System.Windows.Point p = e.GetPosition(this);
        TranslateTransform tt = new TranslateTransform();
        tt.X = (p.X - mouseDownX);
        tt.Y = (p.Y - mouseDownY);

        rect.RenderTransform = tt;

    }
}
4

3 回答 3

5

或者您使用MatrixTransform

var matrix = Matrix.Identity;
matrix.Scale(1.5, 2.5);
matrix.Translate(30, 60);
rect.RenderTransform = new MatrixTransform(matrix);

甚至更短:

var matrix = new Matrix(1.5, 0, 0, 2.5, 30, 60);
rect.RenderTransform = new MatrixTransform(matrix);

或者更好的是,您避免每次都设置一个新的 RenderTransform 并且只更新变换矩阵:

// set RenderTransform once in constructor
rect.RenderTransform = new MatrixTransform()

...

// update matrix in event handler
((MatrixTransform)rect.RenderTransform).Matrix = new Matrix(...);
于 2013-01-05T21:33:56.840 回答
1

我没有根据您的情况调整以下示例,因为您提供的代码很长。如果可以的话,请考虑只发布它的关键部分。

要组合不同类型的Transform,您可以使用TransformGroup, 以这种方式。

void Button_Click_1(object sender, RoutedEventArgs e)
{
    var button = sender as Button;
    var transformGroup = new TransformGroup();
    var scale = new ScaleTransform(1.5, 2.5);
    var translate = new TranslateTransform(30, 60);
    transformGroup.Children.Add(scale);
    transformGroup.Children.Add(translate);
    button.RenderTransform = transformGroup;
}
于 2013-01-05T19:26:42.930 回答
0

我还尝试将 MatrixTransform 与鼠标事件一起使用,并成功使其像这样工作:

public partial class MainWindow : Window
{
    ....
    // MyMatrixTransform could directly be defined in the xaml file        
    private MatrixTransform MyMatrixTransform = new MatrixTransform();

    public MainWindow()
    {
        InitializeComponent();
        MyMatrixTransform= new MatrixTransform();
        // MyCanvasArea is a canvas defined in the xaml file
        MyCanvasArea.RenderTransform = MyMatrixTransform;
        .....
    }

    // canvas_MouseWheel is attached to canvas in the xaml file as follow
    // <Canvas x:Name="MyCanvasArea" MouseWheel="canvas_MouseWheel" />
    void canvas_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta > 0)
        {
            Matrix matrix = MyMatrixTransform.Matrix;
            matrix.Scale(1.1, 1.1);
            MyMatrixTransform.Matrix = matrix;
        }
        else
        {
            Matrix matrix = MyMatrixTransform.Matrix;
            matrix.Scale(0.9, 0.9);
            MyMatrixTransform.Matrix = matrix;
        }
    }
    ....
}

可能可以直接访问 MatrixTransform 中的 Matrix 字段,但我找不到方法。

于 2019-03-26T21:14:27.930 回答