Search code examples
c#scrollmousewheelwinui-3

PointerPoint.Properties.MouseWheelDelta tilt or scroll


I have a function that listenes to PointerWheelChanged events of my UIElement.
I can retreive the MouseWheelDelta but this doesn't tell me if the mouse wheel was tilted or moved up/down.

How can I get this info?

This is my code to get the delta:

private void TestScrollViewer_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
    PointerPoint ptrPt = e.GetCurrentPoint((UIElement)sender);
    int delta = ptrPt.Properties.MouseWheelDelta;
    // positive: forward/right motion;  negative: backward/left motion
}

Solution

  • You can use IsHorizontalMouseWheel inside Pointers. See this example below:

    MainWindow.xaml

    <Window
        x:Class="MouseWheelTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="using:MouseWheelTests"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid RowDefinitions="Auto,*">
            <StackPanel Grid.Row="0">
                <TextBlock x:Name="WheelHorizontal" />
                <TextBlock x:Name="WheelVertical" />
            </StackPanel>
            <Grid
                Grid.Row="1"
                Background="Transparent"
                PointerWheelChanged="Grid_PointerWheelChanged" />
        </Grid>
    
    </Window>
    
    

    MainWindow.xaml.cs

    using Microsoft.UI.Xaml;
    using Microsoft.UI.Xaml.Input;
    
    namespace MouseWheelTests;
    
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }
    
        private int WheelHorizontalValue { get; set; }
    
        private int WheelVerticalValue { get; set; }
    
        private void Grid_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
        {
            var properties = e.GetCurrentPoint((UIElement)sender).Properties;
    
            if (properties.IsHorizontalMouseWheel is true)
            {
                WheelHorizontalValue += properties.MouseWheelDelta;
                this.WheelHorizontal.Text = $"Horizontal: {WheelHorizontalValue}";
            }
            else
            {
                WheelVerticalValue += properties.MouseWheelDelta;
                this.WheelVertical.Text = $"Vertical: {WheelVerticalValue}";
            }
        }
    }