2013-01-05 57 views
2

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

    } 
} 

回答

4

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

謝謝,這工作就像一個魅力! –

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