Class1.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using System.Runtime.InteropServices;
  8. using System.Runtime.CompilerServices;
  9. namespace EdgeGesture
  10. {
  11. public class EdgeGestureUtil
  12. {
  13. private static Guid DISABLE_TOUCH_SCREEN = new Guid("32CE38B2-2C9A-41B1-9BC5-B3784394AA44");
  14. private static Guid IID_PROPERTY_STORE = new Guid("886d8eeb-8cf2-4446-8d02-cdba1dbdcf99");
  15. private static short VT_BOOL = 11;
  16. #region "Structures"
  17. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  18. public struct PropertyKey
  19. {
  20. public PropertyKey(Guid guid, UInt32 pid)
  21. {
  22. fmtid = guid;
  23. this.pid = pid;
  24. }
  25. [MarshalAs(UnmanagedType.Struct)]
  26. public Guid fmtid;
  27. public uint pid;
  28. }
  29. [StructLayout(LayoutKind.Explicit)]
  30. public struct PropVariant
  31. {
  32. [FieldOffset(0)]
  33. public short vt;
  34. [FieldOffset(2)]
  35. private short wReserved1;
  36. [FieldOffset(4)]
  37. private short wReserved2;
  38. [FieldOffset(6)]
  39. private short wReserved3;
  40. [FieldOffset(8)]
  41. private sbyte cVal;
  42. [FieldOffset(8)]
  43. private byte bVal;
  44. [FieldOffset(8)]
  45. private short iVal;
  46. [FieldOffset(8)]
  47. public ushort uiVal;
  48. [FieldOffset(8)]
  49. private int lVal;
  50. [FieldOffset(8)]
  51. private uint ulVal;
  52. [FieldOffset(8)]
  53. private int intVal;
  54. [FieldOffset(8)]
  55. private uint uintVal;
  56. [FieldOffset(8)]
  57. private long hVal;
  58. [FieldOffset(8)]
  59. private long uhVal;
  60. [FieldOffset(8)]
  61. private float fltVal;
  62. [FieldOffset(8)]
  63. private double dblVal;
  64. [FieldOffset(8)]
  65. public bool boolVal;
  66. [FieldOffset(8)]
  67. private int scode;
  68. //CY cyVal;
  69. [FieldOffset(8)]
  70. private DateTime date;
  71. [FieldOffset(8)]
  72. private System.Runtime.InteropServices.ComTypes.FILETIME filetime;
  73. //CLSID* puuid;
  74. //CLIPDATA* pclipdata;
  75. //BSTR bstrVal;
  76. //BSTRBLOB bstrblobVal;
  77. [FieldOffset(8)]
  78. private Blob blobVal;
  79. //LPSTR pszVal;
  80. [FieldOffset(8)]
  81. private IntPtr pwszVal;
  82. //LPWSTR
  83. //IUnknown* punkVal;
  84. //IDispatch* pdispVal;
  85. // IStream* pStream;
  86. // IStorage* pStorage;
  87. // LPVERSIONEDSTREAM pVersionedStream;
  88. // LPSAFEARRAY parray;
  89. // CAC cac;
  90. // CAUB caub;
  91. // CAI cai;
  92. // CAUI caui;
  93. // CAL cal;
  94. // CAUL caul;
  95. // CAH cah;
  96. // CAUH cauh;
  97. // CAFLT caflt;
  98. // CADBL cadbl;
  99. // CABOOL cabool;
  100. // CASCODE cascode;
  101. // CACY cacy;
  102. // CADATE cadate;
  103. // CAFILETIME cafiletime;
  104. // CACLSID cauuid;
  105. // CACLIPDATA caclipdata;
  106. // CABSTR cabstr;
  107. // CABSTRBLOB cabstrblob;
  108. // CALPSTR calpstr;
  109. // CALPWSTR calpwstr;
  110. // CAPROPVARIANT capropvar;
  111. // CHAR* pcVal;
  112. // UCHAR* pbVal;
  113. // SHORT* piVal;
  114. // USHORT* puiVal;
  115. // LONG* plVal;
  116. // ULONG* pulVal;
  117. // INT* pintVal;
  118. // UINT* puintVal;
  119. // FLOAT* pfltVal;
  120. // DOUBLE* pdblVal;
  121. // VARIANT_BOOL* pboolVal;
  122. // DECIMAL* pdecVal;
  123. // SCODE* pscode;
  124. // CY* pcyVal;
  125. // DATE* pdate;
  126. // BSTR* pbstrVal;
  127. // IUnknown** ppunkVal;
  128. // IDispatch** ppdispVal;
  129. // LPSAFEARRAY* pparray;
  130. // PROPVARIANT* pvarVal;
  131. //
  132. /// <summary>
  133. /// Helper method to gets blob data
  134. /// </summary>
  135. private byte[] GetBlob()
  136. {
  137. byte[] Result = new byte[blobVal.Length];
  138. Marshal.Copy(blobVal.Data, Result, 0, Result.Length);
  139. return Result;
  140. }
  141. /// <summary>
  142. /// Property value
  143. /// </summary>
  144. public object Value
  145. {
  146. get
  147. {
  148. VarEnum ve = (VarEnum)vt;
  149. switch (ve)
  150. {
  151. case VarEnum.VT_I1:
  152. return bVal;
  153. case VarEnum.VT_I2:
  154. return iVal;
  155. case VarEnum.VT_I4:
  156. return lVal;
  157. case VarEnum.VT_I8:
  158. return hVal;
  159. case VarEnum.VT_INT:
  160. return iVal;
  161. case VarEnum.VT_UI4:
  162. return ulVal;
  163. case VarEnum.VT_LPWSTR:
  164. return Marshal.PtrToStringUni(pwszVal);
  165. case VarEnum.VT_BLOB:
  166. return GetBlob();
  167. }
  168. throw new NotImplementedException("PropVariant " + ve.ToString());
  169. }
  170. }
  171. }
  172. internal struct Blob
  173. {
  174. public int Length;
  175. public IntPtr Data;
  176. //Code Should Compile at warning level4 without any warnings,
  177. //However this struct will give us Warning CS0649: Field [Fieldname]
  178. //is never assigned to, and will always have its default value
  179. //You can disable CS0649 in the project options but that will disable
  180. //the warning for the whole project, it's a nice warning and we do want
  181. //it in other places so we make a nice dummy function to keep the compiler
  182. //happy.
  183. private void FixCS0649()
  184. {
  185. Length = 0;
  186. Data = IntPtr.Zero;
  187. }
  188. }
  189. #endregion
  190. #region "Interfaces"
  191. [ComImport(), Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  192. public interface IPropertyStore
  193. {
  194. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  195. void GetCount([Out(), In()] ref uint cProps);
  196. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  197. void GetAt([In()] uint iProp, ref PropertyKey pkey);
  198. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  199. void GetValue([In()] ref PropertyKey key, ref PropVariant pv);
  200. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  201. void SetValue([In()] ref PropertyKey key, [In()] ref PropVariant pv);
  202. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  203. void Commit();
  204. [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  205. void Release();
  206. }
  207. #endregion
  208. #region "Methods"
  209. [DllImport("shell32.dll", SetLastError = true)]
  210. private static extern int SHGetPropertyStoreForWindow(IntPtr handle, Guid riid, ref IPropertyStore propertyStore);
  211. public static void EnableEdgeGestures(IntPtr hwnd, bool enable)
  212. {
  213. IPropertyStore pPropStore = null;
  214. int hr = 0;
  215. hr = SHGetPropertyStoreForWindow(hwnd, IID_PROPERTY_STORE, ref pPropStore);
  216. if (hr == 0)
  217. {
  218. PropertyKey propKey = new PropertyKey();
  219. propKey.fmtid = DISABLE_TOUCH_SCREEN;
  220. propKey.pid = 2;
  221. PropVariant var = new PropVariant();
  222. var.vt = VT_BOOL;
  223. var.boolVal = enable;
  224. pPropStore.SetValue(ref propKey, ref var);
  225. Marshal.FinalReleaseComObject(pPropStore);
  226. }
  227. }
  228. #endregion
  229. }
  230. //=======================================================
  231. //Service provided by Telerik (www.telerik.com)
  232. //Conversion powered by NRefactory.
  233. //Twitter: @telerik
  234. //Facebook: facebook.com/telerik
  235. //=======================================================
  236. }