【问题标题】:How do I detect if a Windows device is touch-enabled如何检测 Windows 设备是否支持触控
【发布时间】:2015-01-06 17:29:39
【问题描述】:

如何检测设备是否在 c# 中为 WinForms 应用程序(非 WPF)启用了触摸功能。

我找到了有关 GetSystemMetrics 的信息...在 c# 中找不到如何使用它。

我尝试使用 System.Windows.Input.Tablet 类.. 没有出现在 c# 中,即使我使用的是 .Net Framework 4.5。

我尝试使用 System.Windows.Devices...没有出现在 c# 中,即使我使用的是 .Net Framework 4.5。

我还检查了ThisThis,这似乎使这个问题重复了。但是,这些都不能回答我的问题。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    GetSystemMetrics 似乎是正确的方法。应该像这样通过 p/invoke 访问它:

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int GetSystemMetrics(int nIndex);
    
    public static bool IsTouchEnabled()
    {
         const int MAXTOUCHES_INDEX = 95;
         int maxTouches = GetSystemMetrics(MAXTOUCHES_INDEX);
    
         return maxTouches > 0;
    }
    

    【讨论】:

    • 这是答案,但我是从下面的 JCL 得到的。谢谢!
    • 您是在谈论引用 WPF 主程序集吗?这可能不像他所说的那样是个好主意,但是是的,它也应该有效。
    • 不,我使用了 GetSystemMetrics。这是他的一个 cmets 中的链接。
    • 抱歉,没看到。由于评论线程有点太长,它的评论被隐藏了。
    • 我相信 MAXTOUCHES_INDEX 应该是 95,而不是 0x95
    【解决方案2】:

    取自this answer

    var hasTouch = Windows.Devices.Input
                  .PointerDevice.GetPointerDevices()
                  .Any(p => p.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch);
    

    【讨论】:

    • 我并不是要争论,但“不允许”不是对问题的技术描述。您是否收到编译器错误?如果是这样,它们是什么?您是否添加了对 System.Windows.Input 命名空间的引用? (右键项目,添加引用)
    • 我认为(虽然我不是 100% 肯定)Windows.Devices.Input 正在编组WinRT,但我认为它不适用于Win32 应用程序。
    • 事实上,如果您检查msdn.microsoft.com/en-us/library/…,在“要求”上会清楚地显示Windows Store Apps Only。这个答案显然是错误的(链接的答案是metro apps
    • Ahmed:当我浏览程序集时,没有找到 System.Windows.Input 或 System.Windows.Devices。
    • @RickInWestPalmBeach 如果对在 .NET 中使用任何 Win32 函数有疑问,我推荐 pinvoke.net 。在这种情况下,请检查:pinvoke.net/default.aspx/user32.getsystemmetrics
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多