首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF 禁用实时触摸

WPF 禁用实时触摸

作者头像
林德熙
发布2018-09-18 17:16:16
1.4K0
发布2018-09-18 17:16:16
举报
文章被收录于专栏:林德熙的博客林德熙的博客

微软想把 WPF 作为 win7 的触摸好用的框架,所以微软做了很多特殊的兼容。为了获得真实的触摸消息,微软提供了 OnStylusDown, OnStylusUp, 和 OnStylusMove 事件。 本文告诉大家如何使用代码禁用 WPF 的触摸消息,解决一些问题。

在 win7 还提供了多点触摸 windows 消息 WM_TOUCH ,通过这两个 API 一个是 OnStylusDown 这些事件,另一个就是 WM_TOUCH ,用户可以拿到触摸消息。

这两个 API 是相互独立,依靠相同的 HWND 。

那么为什么需要禁用 WPF 的 RealTimeStylus ,因为在 WPF 触摸平台会禁用 WM_TOUCH 消息。如果想要使用 WM_TOUCH ,在 WPF 需要禁用 WPF 的触摸事件。

如果没有禁用,就无法拿到 WM_TOUCH 消息,这个方法可以让自己定义自己的触摸。

禁用的方法使用下面代码

	public static void DisableWPFTabletSupport()
{
    // Get a collection of the tablet devices for this window.  
    TabletDeviceCollection devices = System.Windows.Input.Tablet.TabletDevices;

    if (devices.Count > 0)
    {   
        // Get the Type of InputManager.
        Type inputManagerType = typeof(System.Windows.Input.InputManager);
        
        // Call the StylusLogic method on the InputManager.Current instance.
        object stylusLogic = inputManagerType.InvokeMember("StylusLogic",
                    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, InputManager.Current, null);

        if (stylusLogic != null)
        {
            //  Get the type of the stylusLogic returned from the call to StylusLogic.
            Type stylusLogicType = stylusLogic.GetType();
            
            // Loop until there are no more devices to remove.
            while (devices.Count > 0)
            {
                // Remove the first tablet device in the devices collection.
                stylusLogicType.InvokeMember("OnTabletRemoved",
                        BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
                        null, stylusLogic, new object[] { (uint)0 });
            }                
        }
               
    }
}

代码直接可以直接放在项目,代码是在微软文档复制。

虽然禁用微软提供的触摸事件,可以修复很多坑,但是禁用了也是有很多新的坑,不过我就不在这里告诉大家。自己尝试运行下面代码,然后试试程序。

Disable the RealTimeStylus for WPF Applications


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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档