WPF - 滚动无焦点

发布于 2024-10-19 19:05:31 字数 219 浏览 3 评论 0原文

我想在应用程序窗口未聚焦时滚动 ScrollViewer 控件。我在 Windows 窗体中找到了一些解决方案,但在 WPF 中没有找到,因为我需要的一些东西不可用。

也许这里有人可以帮助我解决我的问题?

提前致谢。

编辑:

我想在鼠标位于 ScrollViewer 上方时使用鼠标滚轮滚动 ScrollViewer 控件,而且在应用程序窗口未聚焦时也是如此。

I want to scroll a ScrollViewer control while the application window isn't focused. I found some solutions in Windows Forms, but not in WPF because some of the things I need are not available.

Perhaps someone here could help me solve my problem?

Thanks in advance.

EDIT:

I want to scroll the ScrollViewer control with the mouse wheel while the mouse is over the ScrollViewer, but also while the application window isn't focused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2024-10-26 19:05:31

我找到了解决方案。这是一个示例:

XAML

<Window x:Class="Project.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="500">
    <ScrollViewer x:Name="ScrollViewer">
        <StackPanel>
            <Border Background="Blue" Width="100" Height="100" />
            <Border Background="Red" Width="100" Height="100" />
            <Border Background="Blue" Width="100" Height="100" />
            <Border Background="Red" Width="100" Height="100" />
            <Border Background="Blue" Width="100" Height="100" />
            <Border Background="Red" Width="100" Height="100" />
        </StackPanel>
    </ScrollViewer>
</Window>

代码隐藏

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace Project
{
    public partial class MainWindow : Window
    {
        private const int WH_MOUSE_LL = 14;
        private const int WM_MOUSEWHEEL = 0x020A;
        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

        private IntPtr _handle;

        public MainWindow()
        {
            InitializeComponent();

            _handle = new WindowInteropHelper(this).Handle;
            SetWindowsHookEx(WH_MOUSE_LL, MouseChanged, _handle, 0);

            ScrollViewer.PreviewMouseWheel += ScrollViewer_MouseWheel;
            Unloaded += MainWindow_Unloaded;
        }

        private void MainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            UnhookWindowsHookEx(_handle);
        }

        private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
        }

        private IntPtr MouseChanged(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (ScrollViewer.IsMouseOver && WM_MOUSEWHEEL == (int)wParam)
            {
                MouseData mouseData = (MouseData)Marshal.PtrToStructure(lParam, typeof(MouseData));
                ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset - mouseData.Z / 200000.0);
            }
            return CallNextHookEx(_handle, nCode, wParam, lParam);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MouseData
        {
            public int X;
            public int Y;
            public int Z;
        }

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll")]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll")]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
    }
}

I found a solution. Here an example:

XAML

<Window x:Class="Project.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="500">
    <ScrollViewer x:Name="ScrollViewer">
        <StackPanel>
            <Border Background="Blue" Width="100" Height="100" />
            <Border Background="Red" Width="100" Height="100" />
            <Border Background="Blue" Width="100" Height="100" />
            <Border Background="Red" Width="100" Height="100" />
            <Border Background="Blue" Width="100" Height="100" />
            <Border Background="Red" Width="100" Height="100" />
        </StackPanel>
    </ScrollViewer>
</Window>

Code Behind

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace Project
{
    public partial class MainWindow : Window
    {
        private const int WH_MOUSE_LL = 14;
        private const int WM_MOUSEWHEEL = 0x020A;
        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

        private IntPtr _handle;

        public MainWindow()
        {
            InitializeComponent();

            _handle = new WindowInteropHelper(this).Handle;
            SetWindowsHookEx(WH_MOUSE_LL, MouseChanged, _handle, 0);

            ScrollViewer.PreviewMouseWheel += ScrollViewer_MouseWheel;
            Unloaded += MainWindow_Unloaded;
        }

        private void MainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            UnhookWindowsHookEx(_handle);
        }

        private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
        }

        private IntPtr MouseChanged(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (ScrollViewer.IsMouseOver && WM_MOUSEWHEEL == (int)wParam)
            {
                MouseData mouseData = (MouseData)Marshal.PtrToStructure(lParam, typeof(MouseData));
                ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset - mouseData.Z / 200000.0);
            }
            return CallNextHookEx(_handle, nCode, wParam, lParam);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MouseData
        {
            public int X;
            public int Y;
            public int Z;
        }

        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll")]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll")]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文