https://gist.github.com/vbfox/1339671
namespaceblackfox{usingSystem; usingSystem.ComponentModel; usingSystem.Runtime.InteropServices; usingSystem.Security; /// <summary> ///As long as this object exists all mouse events created from a touch event for legacy support would be disabled. /// </summary> classdisabletouchconversiontomouse:idisposable {Static ReadOnlyLowlevelmouseproc Hookcallback =Hookcallback; StaticIntPtr Hookid =IntPtr.Zero; PublicDisabletouchconversiontomouse () {Hookid=sethook (Hookcallback); } StaticIntPtr sethook (lowlevelmouseproc proc) {varModulehandle = Unsafenativemethods.getmodulehandle (NULL); varSethookresult = Unsafenativemethods.setwindowshookex (Wh_mouse_ll, Proc, Modulehandle,0); if(Sethookresult = =IntPtr.Zero) {Throw Newwin32exception (); } returnSethookresult; } DelegateIntPtr Lowlevelmouseproc (intNCode, IntPtr WParam, IntPtr lParam); StaticIntPtr Hookcallback (intNCode, IntPtr WParam, IntPtr LParam) { if(NCode >=0) { varinfo = (msllhookstruct) marshal.ptrtostructure (LParam,typeof(msllhookstruct)); varExtraInfo = (UINT) Info.dwExtraInfo.ToInt32 (); if((ExtraInfo & mouseeventf_fromtouch) = =Mouseeventf_fromtouch) { return NewINTPTR (1); } } returnUnsafenativemethods.callnexthookex (Hookid, NCode, WParam, LParam); } BOOLdisposed; Public voidDispose () {if(disposed)return; Unsafenativemethods.unhookwindowshookex (Hookid); Disposed=true; Gc. SuppressFinalize ( This); } ~Disabletouchconversiontomouse () {Dispose (); } #regionInterop//ReSharper Disable inconsistentnaming//ReSharper Disable membercanbeprivate.local//ReSharper Disable fieldcanbemadereadonly.local Const UINTMouseeventf_fromtouch =0xff515700; Const intWh_mouse_ll = -; [StructLayout (layoutkind.sequential)]structPoint { Public intx; Public inty; } [StructLayout (layoutkind.sequential)]structmsllhookstruct { PublicPoint pt; Public UINTMousedata; Public UINTflags; Public UINTTime ; PublicIntPtr dwExtraInfo; } [SuppressUnmanagedCodeSecurity]Static classUnsafeNativeMethods {[DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError =true)] Public Static externIntPtr SetWindowsHookEx (intIdhook, Lowlevelmouseproc lpfn, IntPtr hmod,UINTdwThreadID); [DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError =true)] [return: MarshalAs (unmanagedtype.bool)] Public Static extern BOOLUnhookWindowsHookEx (IntPtr hhk); [DllImport ("user32.dll", CharSet = CharSet.Auto, SetLastError =true)] Public Static externIntPtr CallNextHookEx (IntPtr hhk,intNCode, IntPtr WParam, IntPtr lParam); [DllImport ("Kernel32.dll", CharSet = CharSet.Auto, SetLastError =true)] Public Static externIntPtr GetModuleHandle (stringlpmodulename); } //ReSharper Restore Inconsistentnaming//ReSharper Restore Fieldcanbemadereadonly.local//ReSharper Restore Membercanbeprivate.local #endregion }}
C++hook Global Touch Events