WPF 能点不能滑

在写WPF触摸时,时常发现能点不能滑--多半是出现在跟随系统启动 或多次插拔USB引起(3 4 5是阅卖wuty007的博客记录-并未验证)

1 触摸设备数 > 0

System.Windows.Input.Tablet.TabletDevices.Count  > 0 ;
(new Windows.Devices.Input.TouchCapabilities()).TouchPresent > 0 ; //效果不佳 

2 系统的支持触摸点数及系统支持

[DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);
public static bool DetectSystemTablet() => GetSystemMetrics(94) > 0 || GetSystemMetrics(95) > 0 || GetSystemMetrics(86) > 0;

3 检测是否检测到触摸数字化器,输入的触摸设备(需要引用WinRT,nuget包引用:Microsoft.Windows.SDK.Contract)

private static bool IsTouchEnabledSystem()
 {
     var touchCapabilities = new TouchCapabilities();
     var isTouch = touchCapabilities.TouchPresent > 0;
     return isTouch;
 }

4 是否存在触摸设备尺寸

public static bool ExistTabletSize()
  {
    bool flag = false;
    foreach (TabletDevice tabletDevice in (IEnumerable) Tablet.TabletDevices)
    {
      PropertyInfo property = tabletDevice.GetType().GetProperty("TabletDeviceImpl", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty);
      object obj = (object) property == null ? (object) tabletDevice : property.GetValue((object) tabletDevice);
      if (obj.GetType().GetProperty("TabletSize", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty)?.GetValue(obj, (object[]) null) is Size size && tabletDevice.StylusDevices.Count > 0 && size.Width > 0)
        flag = true;
      if (flag)
        break;
    }
    return flag;
  }

5 FirstChanceException 监听触笔 异常

AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

private void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
 {
     AppDomain.CurrentDomain.FirstChanceException -= CurrentDomain_FirstChanceException;
     try
     {
         if (e.Exception.StackTrace.Contains("Penimc.IPimcManager2.GetTablet"))
         {
             //todo
         }
         else if (e.Exception.StackTrace.Contains("Windows.Input.StylusWisp.WispLogic.RegisterStylusDeviceCore"))
         {
             //todo 
         }
     }
     finally
     {
         AppDomain.CurrentDomain.FirstChanceException -= CurrentDomain_FirstChanceException;
         AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
     }
 }

使用 1 2 3 4 四个总结一起来判断
发现异常 重启应用,但是看到林德熙大老说可以直接热重启触摸
示例代码 https://github.com/lindexi/lindexi_gd/tree/bdf0e7a0/LeekailawnahelNarjailearyaydi

posted @ 2024-01-10 15:48  stweily  阅读(20)  评论(0)    收藏  举报