使用鼠标UWP调整UI元素的大小

时间:2016-09-30 11:45:33

标签: c# win-universal-app uwp

我有Pinch to zoom缩放,但我也需要用鼠标做。我如何实现它,因为我也可以拖动我的图像。

这是我用手指缩放的代码

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

2 个答案:

答案 0 :(得分:0)

  

我有缩放缩放比例,但我也需要用鼠标做。

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

Code-Behind:

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>

答案 1 :(得分: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;
    }