取消

Support Horizontal Scrolling of TouchPad in WPF Application

Finally, Microsoft started to support touchpad like Apple did years ago. As Microsoft never do well in touchpad, WPF application even doesn’t support horizontal scrolling of touchpad. Also, WPF uses MouseWheel to handle vertical scrolling, not a particular method.

This article contains my method to support horizontal scrolling of touchpad in a WPF application. It uses MouseWheel indeed, but horizontals and verticals are all supported.



▲ Precision Touchpad

We need to fetch WM_MOUSEHWHEEL message from our WPF window. Yes! That mouse wheel message. We fetch vertical data from it before, but we now fetch horizontal data from it.

At first, we should hook the window message.

  • override OnSourceInitialized method of a Window.
  • If you could not write code in Window, SourceInitialized event of a Window is also a choice. (You can get the Window instance by using Window.GetWindow(DependencyObject) method.)
  • If you cannot get the opportune moment, you can also write code after SourceInitialized event such as Loaded event or others.
1
2
3
4
5
6
7
8
9
10
11
protected override void OnSourceInitialized(EventArgs e)
{
    var source = PresentationSource.FromVisual(_board);
    ((HwndSource) source)?.AddHook(Hook);
}

private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    // Handle window message here.
    return IntPtr.Zero;
}

Next, let’s handle WM_MOUSEHWHEEL:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const int WM_MOUSEHWHEEL = 0x020E;

private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case WM_MOUSEHWHEEL:
            int tilt = (short) HIWORD(wParam);
            OnMouseTilt(tilt);
            return (IntPtr) 1;
    }
    return IntPtr.Zero;
}

/// <summary>
/// Gets high bits values of the pointer.
/// </summary>
private static int HIWORD(IntPtr ptr)
{
    var val32 = ptr.ToInt32();
    return ((val32 >> 16) & 0xFFFF);
}

/// <summary>
/// Gets low bits values of the pointer.
/// </summary>
private static int LOWORD(IntPtr ptr)
{
    var val32 = ptr.ToInt32();
    return (val32 & 0xFFFF);
}

private void OnMouseTilt(int tilt)
{
    // Write your horizontal handling codes here.
}

You can write horizontal scrolling code in OnMouseTilt method.

Better yet, you could pack all the codes above in a more common class and raise a MouseTilt event just like raising MouseWheel event.

By the way, Microsoft Sculpt Comfort Mouse support horizontal scrolling also, and my codes above here support this kind of mouse.

References

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/handle-horizontal-scrolling-of-touchpad-en.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])

登录 GitHub 账号进行评论