简体   繁体   English

在winforms应用程序中检测各种触摸事件

[英]Detecting various touch events in a winforms app

I have a touch screen monitor with 5 touch points. 我有一个带5个触摸点的触摸屏显示器。 I would like to do some touch work in a new windows forms app but I'm having trouble finding resouces for this. 我想在一个新的Windows窗体应用程序中做一些触摸工作,但我很难找到资源。

Is it possible to handle touch events in a winforms app? 是否可以在winforms应用程序中处理触摸事件? Im not just talking about Tapping, either. 我不只是在谈论攻丝。 I mean things like Pinching, Swiping (two finger swiping), grabbing and twisting/rotating and zooming? 我的意思是像Pinching,Swiping(两个手指滑动),抓取和扭曲/旋转和缩放?

How can we detect a pinch? 我们怎样才能发现捏?

Take a look at WM_GESTURE message , I think you can get some info here, this could also be good and may be this 看看WM_GESTURE消息 ,我想你可以在这里获得一些信息, 这也可能是好的 ,可能就是这样

Since I cant comment on your post, dont take this as a full answer. 由于我不能评论你的帖子,不要把它作为一个完整的答案。 I help how I can ! 我尽我所能!

There's a set of C# interop libraries, provided by Microsoft, that let you detect touches and gestures in WinForms. Microsoft提供了一组C#interop库,可以让您检测WinForms中的触摸和手势。 It was called "Windows 7 Multitouch .NET Interop Sample Library". 它被称为“Windows 7 Multitouch .NET Interop示例库”。 The old link was here , but seems to be dead. 旧链接在这里 ,但似乎已经死了。 For the moment, it's on Google Code . 目前,它位于Google Code上 The archive's original name was Win7RC_MT.zip . 存档的原始名称是Win7RC_MT.zip。 I've used it with great success in my WinForms apps. 我在WinForms应用程序中使用它非常成功。

Adding gesture support to winforms - solved here: 为winforms添加手势支持 - 在此解决:

thesis of LIEW HON CHIN: LIEW HON CHIN的论文:

http://portal.fke.utm.my/libraryfke/files/1387_LIEWHONCHIN2011.pdf http://portal.fke.utm.my/libraryfke/files/1387_LIEWHONCHIN2011.pdf

'Imports System.Security.Permissions
'Imports System.Runtime.InteropServices


  Private first_point As New Point()
  Private second_point As New Point()
  Private iArguments As Integer = 0
  Private Const ULL_ARGUMENTS_BIT_MASK As Int64 = &HFFFFFFFFL
  Private Const WM_GESTURENOTIFY As Integer = &H11A
  Private Const WM_GESTURE As Integer = &H119
  Private Const GC_ALLGESTURES As Integer = &H1
  Private Const GID_BEGIN As Integer = 1
  Private Const GID_END As Integer = 2
  Private Const GID_ZOOM As Integer = 3
  Private Const GID_PAN As Integer = 4
  Private Const GID_ROTATE As Integer = 5
  Private Const GID_TWOFINGERTAP As Integer = 6
  Private Const GID_PRESSANDTAP As Integer = 7
  Private Const GF_BEGIN As Integer = &H1
  Private Const GF_INERTIA As Integer = &H2
  Private Const GF_END As Integer = &H4
  Private Structure GESTURECONFIG
    Public dwID As Integer
    Public dwWant As Integer
    Public dwBlock As Integer
  End Structure
  Private Structure POINTS
    Public x As Short
    Public y As Short
  End Structure
  Private Structure GESTUREINFO
    Public cbSize As Integer
    Public dwFlags As Integer
    Public dwID As Integer
    Public hwndTarget As IntPtr
    <MarshalAs(UnmanagedType.Struct)>
    Friend ptsLocation As POINTS
    Public dwInstanceID As Integer
    Public dwSequenceID As Integer
    Public ullArguments As Int64
    Public cbExtraArgs As Integer
  End Structure
  <DllImport("user32")> _
  Private Shared Function SetGestureConfig(ByVal hWnd As IntPtr, ByVal dwReserved As Integer, ByVal cIDs As Integer, ByRef pGestureConfig As GESTURECONFIG, ByVal cbSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
  End Function
  <DllImport("user32")>
  Private Shared Function GetGestureInfo(ByVal hGestureInfo As IntPtr, ByRef pGestureInfo As GESTUREINFO) As <MarshalAs(UnmanagedType.Bool)> Boolean
  End Function
  Private _gestureConfigSize As Integer
  Private _gestureInfoSize As Integer
  <SecurityPermission(SecurityAction.Demand)>
  Private Sub SetupStructSizes()
    _gestureConfigSize = Marshal.SizeOf(New GESTURECONFIG())
    _gestureInfoSize = Marshal.SizeOf(New GESTUREINFO())
  End Sub
  <PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
  Protected Overrides Sub WndProc(ByRef m As Message)
    Dim handled As Boolean
    Select Case m.Msg
      Case WM_GESTURENOTIFY
        Dim gc As New GESTURECONFIG()
        gc.dwID = 0
        gc.dwWant = GC_ALLGESTURES
        gc.dwBlock = 0
        Dim bResult As Boolean = SetGestureConfig(Handle, 0, 1, gc, _gestureConfigSize)
        If Not bResult Then
          Throw New Exception("Error in execution of SetGestureConfig")
        End If
        handled = True
      Case WM_GESTURE
        handled = DecodeGesture(m)
      Case Else
        handled = False
    End Select
    MyBase.WndProc(m)
    If handled Then
      Try
        m.Result = New IntPtr(1)
      Catch excep As Exception
        Debug.Print("Could not allocate result ptr")
        Debug.Print(excep.ToString())
      End Try
    End If
  End Sub
  Private Function DecodeGesture(ByRef m As Message) As Boolean
    Dim gi As GESTUREINFO
    Try
      gi = New GESTUREINFO()
    Catch excep As Exception
      Debug.Print("Could not allocate resources to decode gesture")
      Debug.Print(excep.ToString())
      Return False
    End Try
    gi.cbSize = _gestureInfoSize
    If Not GetGestureInfo(m.LParam, gi) Then
      Return False
    End If
    Select Case gi.dwID
      Case GID_BEGIN, GID_END
      Case GID_TWOFINGERTAP
        'Receipt.Show()
        'Invalidate()
      Case GID_ZOOM
        Select Case gi.dwFlags
          Case GF_BEGIN
            iArguments = CInt(Fix(gi.ullArguments And
            ULL_ARGUMENTS_BIT_MASK))
            first_point.X = gi.ptsLocation.x
            first_point.Y = gi.ptsLocation.y
            first_point = PointToClient(first_point)
          Case Else
            second_point.X = gi.ptsLocation.x
            second_point.Y = gi.ptsLocation.y
            second_point = PointToClient(second_point)
            RaiseEvent GestureHappened(Me, New GestureEventArgs With {.Operation = Gestures.Pan, .FirstPoint = first_point, .SecondPoint = second_point})
            'Invalidate()
            'MsgBox("zoom")
        End Select
      Case GID_PAN
        Select Case gi.dwFlags
          Case GF_BEGIN
            first_point.X = gi.ptsLocation.x
            first_point.Y = gi.ptsLocation.y
            first_point = PointToClient(first_point)
          Case Else
            second_point.X = gi.ptsLocation.x
            second_point.Y = gi.ptsLocation.y
            second_point = PointToClient(second_point)
            RaiseEvent GestureHappened(Me, New GestureEventArgs With {.Operation = Gestures.Pan, .FirstPoint = first_point, .SecondPoint = second_point})
            'Invalidate()
            'MsgBox("pan")
        End Select
      Case GID_PRESSANDTAP
        'If gi.dwFlags = GF_BEGIN Then
        '  Invalidate()
        'End If
      Case GID_ROTATE
        'Select Case gi.dwFlags
        '  Case GF_BEGIN
        '    iArguments = 0
        '  Case Else
        '    first_point.X = gi.ptsLocation.x
        '    first_point.Y = gi.ptsLocation.y
        '    first_point = PointToClient(first_point)
        '    Invalidate()
        'End Select
    End Select
    Return True
  End Function


  Public Enum Gestures
    Pan
    Zoom
  End Enum

  Public Class GestureEventArgs
    Inherits EventArgs
    Public Property Operation As Gestures
    Public Property FirstPoint As Point
    Public Property SecondPoint As Point
  End Class

  Public Event GestureHappened(sender As Object, e As GestureEventArgs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM