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

本文关键字:元素 UI 鼠标 UWP 调整 | 更新日期: 2023-09-27 18:09:34

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

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

我有缩放缩放,但我还需要用鼠标。

可以使用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>

这不是最终结果,您需要在完成后释放鼠标按钮调整大小

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