How do I detect if a Windows device is touch-enabled

这一生的挚爱 提交于 2021-02-18 12:13:07

问题


How do I detect if a device is touch-enabled in c# for a WinForms app (Not WPF).

I found information on GetSystemMetrics...can't find how to use this in c#.

I Tried using System.Windows.Input.Tablet class..not coming up in c#, even though I am using .Net Framework 4.5.

I Tried using System.Windows.Devices...not coming up in c#, even though I am using .Net Framework 4.5.

I have also checked This and This, which would seem to make this question a duplicate. However, neither of these answers my question.


回答1:


GetSystemMetrics seems to be the right way to go. It should be accessed through p/invoke like this:

[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;
}



回答2:


As taken from this answer

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


来源:https://stackoverflow.com/questions/27803965/how-do-i-detect-if-a-windows-device-is-touch-enabled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!