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

本文关键字:触摸 事件 winforms 应用程序 检测 | 更新日期: 2023-09-27 18:18:49

我有一个触摸屏显示器,有5个触摸点。我想做一些触摸工作在一个新的windows窗体应用程序,但我有麻烦找到这个资源。

是否有可能在winforms应用程序中处理触摸事件?我说的也不仅仅是"敲击"。我的意思是像捏捏,滑动(两个手指滑动),抓取,旋转和缩放?

我们如何检测捏?

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

看一下WM_GESTURE消息,我想你可以在这里得到一些信息,这也可能是好的,可能是这个

因为我不能评论你的帖子,所以不要把这当作一个完整的答案。

有一组c#互操作库,由微软提供,让你在WinForms中检测触摸和手势。它被称为"Windows 7 Multitouch . net互操作样本库"。原来的联系在这里,但似乎已经消失了。目前,它在Google Code上。归档文件的原始名称是Win7RC_MT.zip。我在我的WinForms应用中使用了它,并取得了巨大的成功。

为winforms添加手势支持-这里解决:

刘汉琴论文:

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)