前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Support Horizontal Scrolling of TouchPad in WPF Application

Support Horizontal Scrolling of TouchPad in WPF Application

作者头像
walterlv
发布2018-09-18 13:02:30
7010
发布2018-09-18 13:02:30
举报

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.
代码语言:javascript
复制
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:

代码语言:javascript
复制
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)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Support Horizontal Scrolling of TouchPad in WPF Application
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档