【问题标题】:WPF Listview does not scroll (with mouse wheel) when Application not in focus当应用程序不在焦点时,WPF Listview 不滚动(使用鼠标滚轮)
【发布时间】:2013-04-09 23:06:44
【问题描述】:

我有一个 WPF 应用程序(用 C# 编写),它有一个 Listview 控件,当应用程序处于焦点时,该控件可以用鼠标滚轮完美滚动。 但是,当应用程序不在焦点上时,即使鼠标指针位于应用程序和列表视图区域上,Listview 也不会滚动。我继续在应用程序上看到与鼠标悬停相关的效果,但没有收到鼠标滚轮事件。这与大多数其他应用程序在我的桌面上的工作方式是一致的,但是其中一些(如 Facebook Messenger)支持无焦点滚动,我想在我的 WPF 应用程序中模仿。

我搜索了 MSDN 论坛和 Stackoverflow,并看到了 Windows 窗体的多种解决方案,但它们是 5 年前提出的问题,我想知道是否有人在 .net 4.5 上相对轻松地做到了,并且可以指出可能的解决方案.

---编辑---

感谢这个帖子C# ListView mouse wheel scroll without focus,我能够在这方面取得一定程度的进步

这是接收鼠标滚轮的函数的外观

private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 &&
MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam)
        {
            MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
            Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y);
            Console.WriteLine((short)((hookStruct.mouseData)>>16));                
            MouseWheelEventArgs myArgs = new MouseWheelEventArgs(System.Windows.Input.Mouse.PrimaryDevice, (int)hookStruct.time, (short)((hookStruct.mouseData)>>16));
            myMainFrame.SidePanelControl.ScrollTheListView(myArgs);
            }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

如您所见,我正在初始化一个 MouseWheelEventArgs 实例并具有时间、增量和鼠标设备属性。

如何将此鼠标滚轮事件传递给我的列表视图滚动查看器?

【问题讨论】:

    标签: wpf listview focus mousewheel low-level


    【解决方案1】:

    设法让这个工作。这是我的课。 使用该类所需要做的就是

    1. 初始化 InterceptMouse,将 app/listview/etc 指针传递给它
    2. 当应用不在焦点但发生了相应的 mouseenter 事件时开始拦截鼠标。
    3. 只要事件是 mousewheel,listview 的 scrollviewer 就会收到 mouseWheel 事件。
    4. 当应用程序被激活或 mouseleave 被调用时停止拦截鼠标。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    using System.Windows.Forms;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Input;
    using System.Diagnostics;
    using System.Windows.Forms.Integration;
    namespace myApp.HelperClasses
    {
        public class InterceptMouse
        {
            public static System.Windows.Controls.ListView myListview;
            private static LowLevelMouseProc _proc = HookCallback;
            private static IntPtr _hookID = IntPtr.Zero;
    
    
            public InterceptMouse(System.Windows.Controls.ListView myListviewParam)
            {
                myListview = myListviewParam;
            }
    
            public static void StartIntercepting()
            {
                _hookID = SetHook(_proc);
            }
            public static void StopIntercepting()
            {
                UnhookWindowsHookEx(_hookID);
            }
    
            private static IntPtr SetHook(LowLevelMouseProc proc)
            {
                using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                {
                    return SetWindowsHookEx(WH_MOUSE_LL, proc,
                        GetModuleHandle(curModule.ModuleName), 0);
                }
            }
    
            private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
    
    
            private static IntPtr HookCallback(
                int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 &&
                    MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam)
                {
                    MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                    //Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y + "," + hookStruct.mouseData);
    
                    var delta = (short)((hookStruct.mouseData) >> 16);
                    var mouse = InputManager.Current.PrimaryMouseDevice;
                    var args = new MouseWheelEventArgs(mouse, Environment.TickCount, delta);
                    args.RoutedEvent = WindowsFormsHost.MouseWheelEvent;
    
                    Decorator border = VisualTreeHelper.GetChild(myListview, 0) as Decorator;
                    // Get scrollviewer
                    ScrollViewer scrollViewer = border.Child as ScrollViewer;
                    scrollViewer.RaiseEvent(args);
                }
                return CallNextHookEx(_hookID, nCode, wParam, lParam);
            }
    
    
    
            private const int WH_MOUSE_LL = 14;
    
            private enum MouseMessages
            {
                WM_LBUTTONDOWN = 0x0201,
                WM_LBUTTONUP = 0x0202,
                WM_MOUSEMOVE = 0x0200,
                WM_MOUSEWHEEL = 0x020A,
                WM_RBUTTONDOWN = 0x0204,
                WM_RBUTTONUP = 0x0205
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct POINT
            {
                public int x;
                public int y;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct MSLLHOOKSTRUCT
            {
                public POINT pt;
                public uint mouseData;
                public uint flags;
                public uint time;
                public IntPtr dwExtraInfo;
            }
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr SetWindowsHookEx(int idHook,
                LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
                IntPtr wParam, IntPtr lParam);
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr GetModuleHandle(string lpModuleName);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2013-04-20
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      相关资源
      最近更新 更多