2016-09-30 53 views
0

調整大小UI元素我有Pinch to zoom縮放,但我也需要用鼠標做。我如何實現它,因爲我也可以拖動我的圖像。鼠標UWP

這是我的代碼,用手指

private void Image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) 
    { 
     var ct = (CompositeTransform)image.RenderTransform; 
     ct.ScaleX *= e.Delta.Scale; 
     ct.ScaleY *= e.Delta.Scale; 
    } 

回答

0

它不是一個最終的結果,你需要釋放鼠標按鈕後完成調整

private void Image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) 
    { 
     var border = sender as Border; 
      if (mousePos.X> pos.X|| mousePos.Y > pos.Y) 
      { 
       ct.ScaleX += e.Delta.Translation.X; 
       ct.ScaleY += e.Delta.Translation.Y; 
      } 
      if (mousePos.X < pos.X || mousePos.Y < pos.Y) 
      { 
       ct.ScaleX += e.Delta.Translation.X; 
       ct.ScaleY += e.Delta.Translation.Y; 
      } 
    } 
    private void ImageBorder_PointerPressed(object sender, PointerRoutedEventArgs e) 
    { 
     pos = Window.Current.CoreWindow.PointerPosition; 
    } 

    private void ImageBorder_PointerReleased(object sender, PointerRoutedEventArgs e) 
    { 
     pos = Window.Current.CoreWindow.PointerPosition; 
    } 
0

我捏縮放比例,但我也需要用鼠標做縮放。

您可以使用UIElement.PointerWheelChanged事件改變比例:

代碼隱藏:

private void Image_PointerWheelChanged(object sender, PointerRoutedEventArgs e) 
{ 
    int delta=e.GetCurrentPoint((UIElement)sender).Properties.MouseWheelDelta; 
    var ct = (CompositeTransform)img.RenderTransform; 
    ct.ScaleX += delta/120;//you can set 120 to other value to change the sensitivity 
    ct.ScaleY += delta/120; 
} 

的XAML:

<Canvas Width="800" Height="800"> 
    <Image Name="img" Source="ms-appx:///Assets/profiler.jpeg" PointerWheelChanged="Image_PointerWheelChanged"></Image> 
</Canvas> 
+0

我看到了這個變體,但我想用鼠標來調整窗口的大小,如調整窗口的大小 – SmiLe

+0

你是什麼意思「用鼠標調整窗口的大小」? –

+0

我的意思是,點擊'Image'的邊框,你可以拖動它並調整它的大小 – SmiLe