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