2013-10-18 103 views
0

如何使用幾何和ScaleTransform實現縮放到Silverlight中的特定點?你能提出一些算法,以便我可以做到這一點?我的邏輯錯了。我想通過設置縮放的中心點我遇到了一些麻煩。Silverlight幾何縮放問題

這是在XAML文件中我的幾何數據

<Canvas x:Name="LayoutRoot" Background="Orchid" MouseWheel="PathDraw_OnMouseWheel"> 
    <Path x:Name="PathDraw" Stroke="Brown" StrokeThickness="1" Margin="200,200,0,0"> 
    <Path.Data> 
     <RectangleGeometry x:Name="rect" Rect="80 80 80 80"/> 
    </Path.Data> 
    </Path> 
</Canvas> 

及以下的縮放邏輯。

private static int ZoomSteps = 0; 

    private static double centerX = 0; 
    private static double centerY = 0; 
    private double zoomCoeff = 1.1; 

    private void PathDraw_OnMouseWheel(object sender, MouseWheelEventArgs e) 
    { 
     double delta = e.Delta; 
     if (scaleX != 1) 
     { 
      scaleX = delta >= 0 ? scaleX * zoomCoeff : (scaleX <= 1 ? 1 : scaleX/zoomCoeff); 
      scaleY = delta >= 0 ? scaleY * zoomCoeff : (scaleY <= 1 ? 1 : scaleY/zoomCoeff); 
     } 
     else 
     { 
      scaleX = scaleY = delta >= 0 ? zoomCoeff : 1; 
     } 

     scale = new ScaleTransform { ScaleX = scaleX, ScaleY = scaleY, 
            CenterX = e.GetPosition(PathDraw).X, 
            CenterY = e.GetPosition(PathDraw).Y 

     }; 
     rect.Transform = scale; 
    } 

由於

+0

我有一個包含在畫布中的xaml文件中的矩形。 <路徑x:名稱=「PathDraw」Stroke =「Brown」StrokeThickness =「1」頁邊距=「200,200,0,0」 >

+0

如果(將scaleX = 1!){的scaleX =增量> = 0? scaleX * zoomCoeff :(scaleX <= 1?1:scaleX/zoomCoeff); scaleY = delta> = 0? scaleY * zoomCoeff :(scaleY <= 1?1:scaleY/zoomCoeff); } 否則 { scaleX = scaleY = delta> = 0? zoomCoeff:1; } scale = new ScaleTransform {ScaleX = scaleX,ScaleY = scaleY, CenterX = e.GetPosition(PathDraw).X, CenterY = e.GetPosition(PathDraw).Y }; rect.Transform = scale; –

+0

但這個作品不正確 –

回答

0

代替具有在路徑中的保證金和(在80,80矩形左上角)具有固有的偏移應分離通過使用兩個變換從翻譯縮放幾何形狀:

<Canvas x:Name="LayoutRoot" Background="AliceBlue" MouseWheel="PathDraw_OnMouseWheel"> 
    <Path x:Name="PathDraw" Stroke="Brown" StrokeThickness="1"> 
     <Path.Data> 
      <RectangleGeometry x:Name="rect" Rect="0 0 80 80"> 
       <RectangleGeometry.Transform> 
        <TransformGroup> 
         <ScaleTransform x:Name="scaleTransform"/> 
         <TranslateTransform x:Name="translateTransform" 
              X="100" Y="100"/> 
        </TransformGroup> 
       </RectangleGeometry.Transform> 
      </RectangleGeometry> 
     </Path.Data> 
    </Path> 
</Canvas> 

有了這個鼠標滾輪事件處理程序變得簡單,如下:

private double zoomCoeff = 1.1; 

private void PathDraw_OnMouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    var deltaScale = (e.Delta > 0) ? zoomCoeff : (1d/zoomCoeff); 
    var position = e.GetPosition((UIElement)sender); 
    var dx = position.X - translateTransform.X; 
    var dy = position.Y - translateTransform.Y; 

    translateTransform.X = position.X - deltaScale * dx; 
    translateTransform.Y = position.Y - deltaScale * dy; 
    scaleTransform.ScaleX *= deltaScale; 
    scaleTransform.ScaleY *= deltaScale; 
} 

您現在可以獨立操作MouseMove處理程序中的翻譯以拖動矩形。