Support Horizontal Scrolling of TouchPad in WPF Application

Support Horizontal Scrolling of TouchPad in WPF Application

发布于 2017-11-23 14:09 更新于 2018-08-12 08:02

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.


中文 English

▲ 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.
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:

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://walterlv.com/post/handle-horizontal-scrolling-of-touchpad-en.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

Mono 2.8发布:C#4.0和更好的性能

在社区很多人不看好的微软.NET开源实现Mono发布了Mono 2.8,这是一个重要的版本更新,有着显著的改善,Mono 2.8包括C#4.0的支持(也是现在的...

1999
来自专栏张善友的专栏

Mono运行于IPhone之上

今晚看到Mono老大的blog上有一个Mono on the iPhone, 就是最近Sun公司还在为iPhone上运行Java 苹果没兴趣 Sun单干。 .N...

1956
来自专栏Windows Community

New UWP Community Toolkit

概述 UWP Community Toolkit 是一个 UWP App 自定义控件、应用服务和帮助方法的集合,能够很大程度的简化和指引开发者的开发工作,相信广...

35511
来自专栏.NET开发者社区

​(码友推荐)2018-07-23 .NET及相关开发资讯速递

9.Advisory on July 2018 .NET Framework Updates

1505
来自专栏张善友的专栏

每周.NET前沿技术文章摘要(2017-06-07)

汇总国外.NET社区相关文章,覆盖.NET ,ASP.NET等内容: .NET .NET Core and .NET Framework Working To...

2095
来自专栏张善友的专栏

Visual Studio 2008 Service Pack 1 - BETA发布了

Visual Studio 2008 SP1 提供了对Visual Studio 2008的各项改进,支持SQL Server 2008和ADO.NET的新特性...

2056
来自专栏c#开发者

黑苹果安装日记

Haswell Early Reboot I removed 1 memory stick, and disable some CPU configuratio...

41216
来自专栏程序员叨叨叨

【转】从零开始手敲次世代游戏引擎(一)

况且,考虑到项目的庞大,和剩下的时日(可能也就1~2万天了),基于已经过去的1万多天的经验,这过程当中比尔盖茨一定会死去,冯氏架构也可能成为遗物。所以,保持代码...

2382
来自专栏张善友的专栏

每周.NET前沿技术文章摘要(2017-05-17)

汇总国外.NET社区相关文章,覆盖.NET ,ASP.NET等内容: .NET .NET Framework 4.7正式发布 链接: http://www....

1836
来自专栏walterlv - 吕毅的博客

各个版本 Windows 10 系统中自带的 .NET Framework 版本

发布于 2017-10-17 02:49 更新于 2018-06...

5792

扫码关注云+社区