gpt4 book ai didi

c# - TranslateTransform 取消 ScaleTransform

转载 作者:行者123 更新时间:2023-11-30 15:01:28 28 4
gpt4 key购买 nike

我是 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;

}
}

最佳答案

或者您使用 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(...);

关于c# - TranslateTransform 取消 ScaleTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14175404/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com