2012-04-14 155 views
4

我剛剛開始使用InjectTouchInput for Windows 8 Consumer Preview。我一直在繞圈轉圈試圖讓這件事情起作用,但似乎無法讓它實際上互動。我正在使用C#,目前我只是在兩個文本框和按鈕中創建一個帶有x和y座標的地鐵界面,它們調用下面的函數在這些座標處觸摸屏幕。這是做這件事的正確方法嗎?InjectTouchInput Windows 8 C#不工作(返回false)

protected unsafe class TouchDriver 
    { 


     public struct POINTER_TOUCH_INFO { 
      public POINTER_INFO pointerInfo; // An embedded POINTER_INFO header structure. 

      public TOUCH_FLAGS touchFlags;   // Currently none. 

      public Rect rcContact;    // Pointer contact area in pixel screen coordinates. By default, if the device does not report a contact area, this field defaults to a 0-by-0 rectangle centered around the pointer location. 

      public UInt32 orientation;   // A pointer orientation, with a value between 0 and 359, where 0 indicates a touch pointer aligned with the x-axis and pointing from left to right; increasing values indicate degrees of rotation in the clockwise direction. 
             // This field defaults to 0 if the device does not report orientation. 

      public UInt32 pressure;   // Pointer pressure normalized in a range of 0 to 256. 
             // This field defaults to 128 if the device does not report pressure. 
             // Question: Can this go from 0 to 1024 to match pen pressure? 

     } 
     public enum TOUCH_FLAGS 
     { 
      TOUCH_FLAGS_NONE = 0x00000000 
     } 

     public POINTER_TOUCH_INFO create_pointer_touch_info(POINTER_INFO pointerInfo, TOUCH_FLAGS touchFlags, RECT rcContact, UInt32 orientation, UInt32 pressure) 
     { 
      POINTER_TOUCH_INFO mi = new POINTER_TOUCH_INFO(); 
      mi.pointerInfo = pointerInfo; 
      mi.touchFlags = touchFlags; 
      mi.rcContact = rcContact; 
      mi.orientation = orientation; 
      mi.pressure = pressure; 
      return mi; 
     } 
     public enum POINTER_INPUT_TYPE 
     { 
      PT_POINTER = 0x00000001, 
      PT_TOUCH  = 0x00000002, 
      PT_PEN  = 0x00000003, 
      PT_MOUSE  = 0x00000004 
     } 
     public struct POINTER_INFO 
     { 
      public POINTER_INPUT_TYPE pointerType; 
      public UInt32 pointerId; 
      public UInt32 frameId; 
      public HANDLE sourceDevice; 
      public HWND hwndTarget; 
      public Point ptPixelLocation; 
      public Point ptHimetricLocation; 
      public Point ptPixelLocationPredicted; 
      public Point ptHimetricLocationPredicted; 
      public POINTER_FLAGS pointerFlags; 
      public DWORD dwTime; 
      public UInt32 historyCount; 
      // public UInt32 inputData; 
      public DWORD dwKeyStates; 
      public ULONGLONG Reserved; 
     } 
     public POINTER_INFO create_pointer_info(
      POINTER_INPUT_TYPE pointerType, 
      UInt32 pointerId, 
      UInt32 frameId, 
      HANDLE sourceDevice, 
      HWND hwndTarget, 
      Point ptPixelLocation, 
      Point ptHimetricLocation, 
      Point ptPixelLocationPredicted, 
      Point ptHimetricLocationPredicted, 
      POINTER_FLAGS pointerFlags, 
      DWORD dwTime, 
      UInt32 historyCount, 
      // UInt32 inputData, 
      DWORD dwKeyStates, 
      ULONGLONG Reserved) 
     { 
      POINTER_INFO mi = new POINTER_INFO(); 
      mi.pointerType = pointerType; 
      mi.pointerId = pointerId; 
      mi.frameId = frameId; 
      mi.sourceDevice = sourceDevice; 
      mi.hwndTarget = hwndTarget; 
      mi.ptPixelLocation = ptPixelLocation; 
      mi.ptHimetricLocation = ptHimetricLocation; 
      mi.ptPixelLocationPredicted = ptPixelLocationPredicted; 
      mi.ptHimetricLocationPredicted = ptHimetricLocationPredicted; 
      mi.pointerFlags = pointerFlags; 
      mi.dwTime = dwTime; 
      mi.historyCount = historyCount; 
      // mi.inputData = inputData; 
      mi.dwKeyStates = dwKeyStates; 
      mi.Reserved = Reserved; 
      return mi; 
     } 
     public enum POINTER_FLAGS 
     { 
      POINTER_FLAG_NONE   = 0x00000000, 
      POINTER_FLAG_NEW   = 0x00000001, 
      POINTER_FLAG_INRANGE  = 0x00000002, 
      POINTER_FLAG_INCONTACT  = 0x00000004, 
      POINTER_FLAG_FIRSTBUTTON = 0x00000010, 
      POINTER_FLAG_SECONDBUTTON = 0x00000020, 
      POINTER_FLAG_THIRDBUTTON = 0x00000040, 
      POINTER_FLAG_OTHERBUTTON = 0x00000080, 
      POINTER_FLAG_PRIMARY  = 0x00000100, 
      POINTER_FLAG_CONFIDENCE  = 0x00000200, 
      POINTER_FLAG_CANCELLED  = 0x00000400, 
      POINTER_FLAG_DOWN   = 0x00010000, 
      POINTER_FLAG_UPDATE   = 0x00020000, 
      POINTER_FLAG_UP    = 0x00040000, 
      POINTER_FLAG_WHEEL   = 0x00080000, 
      POINTER_FLAG_HWHEEL   = 0x00100000 
     } 

     [System.Runtime.InteropServices.DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)] 
     private static extern Boolean InjectTouchInput(UInt32 count, POINTER_TOUCH_INFO* pntTchInfo); 

     [System.Runtime.InteropServices.DllImport("user32.dll")] 
     private static extern Boolean InitializeTouchInjection(UInt32 maxCount, DWORD dwMode); 


     private const UInt32 MAX_TOUCH_COUNT = 256; // Can be as high as 256 
     private const UInt32 TOUCH_FEEDBACK_DEFAULT = 0x1; 
     private const UInt32 TOUCH_FEEDBACK_INDIRECT = 0x2; 
     private const UInt32 TOUCH_FEEDBACK_NONE = 0x3; 

     public unsafe static void MouseTouch(int x, int y) 
     { 
      bool ret = false; 
      ret = InitializeTouchInjection(1, TOUCH_FEEDBACK_DEFAULT); 
      if (!ret) 
      { 
       throw new NotSupportedException(); 
      } 
      Point point = new Point(x,y); 

      POINTER_INFO ptrInfo = new POINTER_INFO(); 
      POINTER_TOUCH_INFO* ptrTchInfo; 
      ptrInfo.pointerType = POINTER_INPUT_TYPE.PT_TOUCH; 
      ptrInfo.pointerId = 1; 
      ptrInfo.ptPixelLocation = point; 
      ptrInfo.pointerFlags = POINTER_FLAGS.POINTER_FLAG_PRIMARY; 
      POINTER_TOUCH_INFO ptrTchInfobase = new POINTER_TOUCH_INFO(); 
      ptrTchInfo = &ptrTchInfobase; 
      ptrTchInfo->pointerInfo = ptrInfo; 
      ptrTchInfo->touchFlags = TOUCH_FLAGS.TOUCH_FLAGS_NONE; 
      ptrTchInfo->rcContact.X = x - 2; 
      ptrTchInfo->rcContact.Y = y - 2; 
      ptrTchInfo->rcContact.Width = 4; 
      ptrTchInfo->rcContact.Height = 4; 
      ptrTchInfo->pressure = 128; 
      ptrTchInfo->orientation = 0; 



      ret = InjectTouchInput(1, ptrTchInfo); 
      if (!ret) 
      { 
       throw new NotImplementedException(); 
      } 

     } 


    } 

幾乎所有我試圖從我在網上找到的InjectTouchInput API中解除。我可以InitializeTouchInject罰款,其注入位返回false,我不知道爲什麼。

+0

調用InjectTouchInput後GetLastError返回什麼? InjectTouchInput對於它的輸入參數很挑剔,可能有些東西並不完美。 – 2012-04-14 22:26:59

回答

3

我繼續在C++中基於Microsoft提供的示例創建一些自定義函數,然後使用與之前相同的定義將其導入到C#中,但沒有與所有類型檢查,指針和參數放在一起之前引起頭痛。

,我已經使用的DLL文件是TouchInjectionDriver.dll並且可以在這裏找到:

http://www.mediafire.com/file/do2h6m04omjweb3/TouchInjectionDriver.zip

下面是我用來實現它的C#代碼。

 public enum TOUCH_MASK : uint 
     { 
      TOUCH_MASK_NONE = 0x00000000, 
      TOUCH_MASK_CONTACTAREA = 0x00000001, 
      TOUCH_MASK_ORIENTATION = 0x00000002, 
      TOUCH_MASK_PRESSURE = 0x00000004 
     } 
     public enum POINTER_INPUT_TYPE : uint 
     { 
      PT_POINTER = 0x00000001, 
      PT_TOUCH = 0x00000002, 
      PT_PEN = 0x00000003, 
      PT_MOUSE = 0x00000004 
     } 

     public enum POINTER_FLAGS : uint 
     { 
      POINTER_FLAG_NONE = 0x00000000, 
      POINTER_FLAG_NEW = 0x00000001, 
      POINTER_FLAG_INRANGE = 0x00000002, 
      POINTER_FLAG_INCONTACT = 0x00000004, 
      POINTER_FLAG_FIRSTBUTTON = 0x00000010, 
      POINTER_FLAG_SECONDBUTTON = 0x00000020, 
      POINTER_FLAG_THIRDBUTTON = 0x00000040, 
      POINTER_FLAG_OTHERBUTTON = 0x00000080, 
      POINTER_FLAG_PRIMARY = 0x00000100, 
      POINTER_FLAG_CONFIDENCE = 0x00000200, 
      POINTER_FLAG_CANCELLED = 0x00000400, 
      POINTER_FLAG_DOWN = 0x00010000, 
      POINTER_FLAG_UPDATE = 0x00020000, 
      POINTER_FLAG_UP = 0x00040000, 
      POINTER_FLAG_WHEEL = 0x00080000, 
      POINTER_FLAG_HWHEEL = 0x00100000 
     } 
     public enum TOUCH_FEEDBACK : uint 
     { 
      TOUCH_FEEDBACK_DEFAULT = 0x1, 
      TOUCH_FEEDBACK_INDIRECT = 0x2, 
      TOUCH_FEEDBACK_NONE = 0x3 
     } 

     [DllImport("TouchInjectionDriver.dll", CallingConvention = CallingConvention.Cdecl)] 
     private static extern bool InjectTouch(int x, int y, POINTER_INPUT_TYPE pt_input, int pressure, int orientation, int id, int rcContactTop, int rcContactBottom, int rcContactLeft, int rcContactRight, POINTER_FLAGS pointerFlags, TOUCH_MASK touchMask); 
     [DllImport("TouchInjectionDriver.dll", CallingConvention = CallingConvention.Cdecl)] 
     private static extern void setTouchFeedback(TOUCH_FEEDBACK fb); 
     [DllImport("TouchInjectionDriver.dll", CallingConvention = CallingConvention.Cdecl)] 
     private static extern void setDefaultRectSize(int size); 
     [DllImport("TouchInjectionDriver.dll", CallingConvention = CallingConvention.Cdecl)] 
     private static extern void setDefaultPressure(int pres); 
     [DllImport("TouchInjectionDriver.dll", CallingConvention = CallingConvention.Cdecl)] 
     private static extern void setDefaultOrientation(int or); 

     [DllImport("User32.dll")] 
     static extern Boolean MessageBeep(UInt32 beepType); 

     public static void mouseclick(int x, int y) 
     { 
      bool ret; 
      setTouchFeedback(TOUCH_FEEDBACK.TOUCH_FEEDBACK_INDIRECT); 
      ret = InjectTouch(x, y, POINTER_INPUT_TYPE.PT_TOUCH, 3200, 0, 0, x - 4, x + 4, y - 4, y + 4,POINTER_FLAGS.POINTER_FLAG_DOWN|POINTER_FLAGS.POINTER_FLAG_INCONTACT|POINTER_FLAGS.POINTER_FLAG_INRANGE,TOUCH_MASK.TOUCH_MASK_CONTACTAREA|TOUCH_MASK.TOUCH_MASK_ORIENTATION|TOUCH_MASK.TOUCH_MASK_PRESSURE); 
      if (ret) 
      { 
       ret = InjectTouch(x, y, POINTER_INPUT_TYPE.PT_TOUCH, 3200, 0, 0, x - 4, x + 4, y - 4, y + 4, POINTER_FLAGS.POINTER_FLAG_UP, TOUCH_MASK.TOUCH_MASK_CONTACTAREA | TOUCH_MASK.TOUCH_MASK_ORIENTATION | TOUCH_MASK.TOUCH_MASK_PRESSURE); 
      } 
      else 
      { 
       MessageBeep(0); 
      } 
     } 
+1

太棒了!你是如何將TouchInjectionDriver.dll引用到你的C#項目中的?對於我來說,當我嘗試在Visual Studio中添加引用時,我得到「無法添加引用...請確保該文件是可訪問的,並且它是有效的程序集或COM組件」。任何想法? 我也在命令提示符下嘗試過「regsvr32 TouchInjectionDriver.dll」,但得到了類似的錯誤! 謝謝! – ala 2012-04-16 12:10:15

+0

您不需要在項目中包含.dll作爲參考。 (事實上,除非你正在構建一個C++項目,否則你不能) 儘管如此,你可以將它包含在資源中,所以你知道它將與你的信息一起打包和部署。 只要你的可執行文件可以找到dll,包裝代碼應該可以運行 – 2013-04-11 21:48:44

+0

我得到了這個運行,但是託管的InjectTouch也爲我返回false,兩者都連接了真實的觸摸屏。 – 2013-12-21 00:14:26