Imports System.Runtime.InteropServices Imports System.Windows.Interop Imports devDept.Eyeshot Imports devDept.Eyeshot.Translators Class MainWindow Public Const WM_TOUCH As Integer = &H0240 Public Enum TouchWindowFlag FineTouch = &H1 WantPalm = &H2 End Enum Public Shared Function RegisterTouchWindow(ByVal hWnd As System.IntPtr, ByVal flags As TouchWindowFlag) As Boolean End Function Protected Overrides Sub OnSourceInitialized(ByVal e As EventArgs) MyBase.OnSourceInitialized(e) Dim source As HwndSource = TryCast(System.Windows.PresentationSource.FromVisual(Me), HwndSource) source.AddHook(AddressOf WndProc) Dim presentationSource = CType(System.Windows.PresentationSource.FromDependencyObject(Me), HwndSource) If presentationSource Is Nothing Then Throw New Exception("Unable to find the parent element host.") End If RegisterTouchWindow(presentationSource.Handle, TouchWindowFlag.WantPalm) End Sub Public Shared Function GetTouchInputInfo(ByVal hTouchInput As System.IntPtr, ByVal cInputs As Integer, <[In], Out> ByVal pInputs As devDept.Eyeshot.MultiTouch.Interop.TOUCHINPUT(), ByVal cbSize As Integer) As Boolean End Function Public Shared Sub CloseTouchInputHandle(ByVal lParam As System.IntPtr) End Sub Class TouchDeviceEmulator Inherits TouchDevice Public Position As System.Windows.Point Public Sub New(ByVal deviceId As Integer) MyBase.New(deviceId) End Sub Public Overrides Function GetTouchPoint(ByVal relativeTo As IInputElement) As TouchPoint Dim pt As System.Windows.Point = Position If relativeTo IsNot Nothing Then pt = ActiveSource.RootVisual.TransformToDescendant(CType(relativeTo, Visual)).Transform(Position) Dim rect = New Rect(pt, New Size(1.0, 1.0)) Return New TouchPoint(Me, pt, rect, TouchAction.Move) End Function Public Overrides Function GetIntermediateTouchPoints(ByVal relativeTo As IInputElement) As TouchPointCollection Throw New NotImplementedException() End Function Public Sub SetActiveSource(ByVal activeSource As PresentationSource) MyBase.SetActiveSource(activeSource) End Sub Public Sub Activate() MyBase.Activate() End Sub Public Sub ReportUp() MyBase.ReportUp() End Sub Public Sub ReportDown() MyBase.ReportDown() End Sub Public Sub ReportMove() MyBase.ReportMove() End Sub Public Sub Deactivate() MyBase.Deactivate() End Sub End Class Private _devices As Dictionary(Of Integer, TouchDeviceEmulator) = New Dictionary(Of Integer, TouchDeviceEmulator)() Public Const TOUCHEVENTF_MOVE As Integer = &H0001 Public Const TOUCHEVENTF_DOWN As Integer = &H0002 Public Const TOUCHEVENTF_UP As Integer = &H0004 Public Const TOUCHEVENTF_INRANGE As Integer = &H0008 Public Const TOUCHEVENTF_PRIMARY As Integer = &H0010 Public Const TOUCHEVENTF_NOCOALESCE As Integer = &H0020 Public Const TOUCHEVENTF_PEN As Integer = &H0040 Public Const TOUCHEVENTF_PALM As Integer = &H0080 Private Function WndProc(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr, ByRef handled As Boolean) As IntPtr If msg = WM_TOUCH Then handled = HandleTouch(wParam, lParam) Return New IntPtr(1) End If Return IntPtr.Zero End Function Private Function HandleTouch(ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean Dim handled As Boolean = False Dim inputCount = wParam.ToInt32() And &HFFFF Dim inputs = New devDept.Eyeshot.MultiTouch.Interop.TOUCHINPUT(inputCount - 1) {} If GetTouchInputInfo(lParam, inputCount, inputs, Marshal.SizeOf(inputs(0))) Then For i As Integer = 0 To inputCount - 1 Dim input = inputs(i) Dim position = PointFromScreen(New System.Windows.Point((input.x * 0.01), (input.y * 0.01))) Dim device As TouchDeviceEmulator If Not _devices.TryGetValue(input.dwID, device) Then device = New TouchDeviceEmulator(input.dwID) _devices.Add(input.dwID, device) End If device.Position = position If (input.dwFlags And TOUCHEVENTF_DOWN) > 0 Then device.SetActiveSource(PresentationSource.FromVisual(Me)) device.Activate() device.ReportDown() ElseIf device.IsActive AndAlso (input.dwFlags And TOUCHEVENTF_UP) > 0 Then device.ReportUp() device.Deactivate() _devices.Remove(input.dwID) ElseIf device.IsActive AndAlso (input.dwFlags And TOUCHEVENTF_MOVE) > 0 Then device.ReportMove() End If Next CloseTouchInputHandle(lParam) handled = True End If Return handled End Function End Class