我是WPF的新手,經過相當多的遊戲後,我終於按照自己喜歡的方式安排了自己的控件。我有一個顯示圖像的矩形。鼠標滾輪用ScaleTransform
動畫縮放圖像,鼠標單擊/拖動可在屏幕上移動圖像。問題是,如果我向上或向下縮放圖像然後嘗試移動它,它會自動將它自回縮到1(原始大小)。縮放時是否有任何方法可以將其移動? 編輯:我嘗試將ScaleTransform和TranslateTransform設置爲TransformGroup,但仍然無法使用。TranslateTransform取消ScaleTransform
我遇到的另一個問題是如果圖像大於父容器,它超出了界限,我嘗試在父容器上設置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;
}
}
謝謝,這工作就像一個魅力! –