TabtipHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. namespace GDNXFD.Alert.Config.Helpers
  2. {
  3. using System;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Forms;
  11. using System.Windows.Input;
  12. /// <summary>
  13. /// This class manages the Tabtip process (touch keyboard).
  14. /// </summary>
  15. public class TabtipHelper
  16. {
  17. private const string PROCESS_NAME = "TabTip";
  18. private const string PROCESS_PATH = @"C:\Program Files\Common Files\microsoft shared\ink\tabtip.exe";
  19. private Process tabtipProcess;
  20. private static TabtipHelper instance;
  21. private CancellationTokenSource cts;
  22. private CancellationToken ct;
  23. private ProcessStartInfo psi;
  24. private TabtipHelper()
  25. {
  26. psi = new ProcessStartInfo
  27. {
  28. FileName = PROCESS_PATH,
  29. WindowStyle = ProcessWindowStyle.Maximized
  30. };
  31. }
  32. /// <summary>
  33. /// Creates a new instance of the class.
  34. /// </summary>
  35. public static TabtipHelper Instance
  36. {
  37. get { return instance ?? (instance = new TabtipHelper()); }
  38. }
  39. /// <summary>
  40. /// Is tabtip running.
  41. /// </summary>
  42. public bool IsTabtipRunning
  43. {
  44. get
  45. {
  46. this.tabtipProcess = Process.GetProcessesByName(PROCESS_NAME).FirstOrDefault();
  47. return (this.tabtipProcess != null);
  48. }
  49. }
  50. /// <summary>
  51. /// Try to kill Tabtip process.
  52. /// </summary>
  53. public void TryKillTabtipProcess()
  54. {
  55. if (IsTabtipRunning)
  56. {
  57. this.cts = new CancellationTokenSource();
  58. this.ct = cts.Token;
  59. // Wait for some miliseconds before hide the Taptip.
  60. TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
  61. Task.Factory.StartNew(async () =>
  62. {
  63. await Task.Delay(TimeSpan.FromMilliseconds(100));
  64. if (!ct.IsCancellationRequested)
  65. {
  66. this.cts = null;
  67. if (!IsFullScreen())
  68. {
  69. RestoreWindow();
  70. }
  71. this.tabtipProcess.Kill();
  72. }
  73. }, ct, TaskCreationOptions.None, uiScheduler);
  74. }
  75. }
  76. /// <summary>
  77. /// Try to create Tabtip process.
  78. /// </summary>
  79. public void TryCreateTabtipProcess()
  80. {
  81. if (IsTabtipRunning && (cts != null))
  82. {
  83. this.cts.Cancel();
  84. }
  85. // Check if current device has touch screen.
  86. if (HasTouchInput())
  87. {
  88. this.tabtipProcess = Process.Start(psi);
  89. }
  90. }
  91. private bool HasTouchInput()
  92. {
  93. foreach (TabletDevice tabletDevice in Tablet.TabletDevices)
  94. {
  95. //Only detect if it is a touch Screen not how many touches (i.e. Single touch or Multi-touch)
  96. if (tabletDevice.Type == TabletDeviceType.Touch)
  97. return true;
  98. }
  99. return false;
  100. }
  101. private void RestoreWindow()
  102. {
  103. if (App.Current.MainWindow.WindowState == WindowState.Maximized)
  104. {
  105. App.Current.MainWindow.UseLayoutRounding = true;
  106. App.Current.MainWindow.Width = Screen.PrimaryScreen.Bounds.Width;
  107. App.Current.MainWindow.Height = Screen.PrimaryScreen.Bounds.Height;
  108. App.Current.MainWindow.WindowState = WindowState.Normal;
  109. App.Current.MainWindow.WindowState = WindowState.Maximized;
  110. }
  111. }
  112. private bool IsFullScreen()
  113. {
  114. return ((App.Current.MainWindow.Width >= Screen.PrimaryScreen.Bounds.Width)
  115. && App.Current.MainWindow.Height >= Screen.PrimaryScreen.Bounds.Height);
  116. }
  117. }
  118. }