Created
October 3, 2017 22:16
-
-
Save axodox/ed2bb7802641b70853018a50aa1a730c to your computer and use it in GitHub Desktop.
WPF Touch Simulation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Data; | |
using System.Windows.Documents; | |
using System.Windows.Input; | |
using System.Windows.Media; | |
using System.Windows.Media.Imaging; | |
using System.Windows.Navigation; | |
using System.Windows.Shapes; | |
using System.Windows.Threading; | |
namespace TouchTest | |
{ | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
TouchDown += MainWindow_TouchDown; | |
MouseDoubleClick += MainWindow_MouseDoubleClick; | |
} | |
private void MainWindow_MouseDoubleClick(object sender, MouseButtonEventArgs e) | |
{ | |
Tap(); | |
} | |
private void Tap() | |
{ | |
VirtualTouchDevice.Apply(this, p => | |
{ | |
p.Position = PointToScreen(new Point(ActualWidth / 2d, ActualHeight / 2d)); | |
p.IsActive = true; | |
WaifFotUiUpdate(); | |
p.Position = p.Position; | |
WaitForSeconds(0.2); | |
WaifFotUiUpdate(); | |
p.Position = p.Position; | |
p.IsActive = false; | |
}); | |
} | |
private void WaifFotUiUpdate() | |
{ | |
Dispatcher.Invoke(() => { }); | |
} | |
private static void WaitForSeconds(double seconds) | |
{ | |
var wFrame = new DispatcherFrame(); | |
new Thread(() => | |
{ | |
Thread.Sleep(TimeSpan.FromSeconds(seconds)); | |
wFrame.Continue = false; | |
}).Start(); | |
Dispatcher.PushFrame(wFrame); | |
} | |
private void MainWindow_TouchDown(object sender, TouchEventArgs e) | |
{ | |
MessageBox.Show("It works!"); | |
} | |
class VirtualTouchDevice : TouchDevice | |
{ | |
public const double TouchRadius = 2; | |
private TouchAction mLastAction; | |
private Point mPosition; | |
public Point Position | |
{ | |
get | |
{ | |
return mPosition; | |
} | |
set | |
{ | |
if(mPosition != value) | |
{ | |
mPosition = value; | |
mLastAction = TouchAction.Move; | |
ReportMove(); | |
Synchronize(); | |
} | |
} | |
} | |
private bool mIsActive; | |
public new bool IsActive | |
{ | |
get | |
{ | |
return mIsActive; | |
} | |
set | |
{ | |
var wLastValue = mIsActive; | |
mIsActive = value; | |
if (value && !wLastValue) | |
{ | |
mLastAction = TouchAction.Down; | |
ReportDown(); | |
} | |
if(!value && wLastValue) | |
{ | |
mLastAction = TouchAction.Up; | |
ReportUp(); | |
} | |
Synchronize(); | |
} | |
} | |
public VirtualTouchDevice() | |
: base(1) | |
{ | |
} | |
public static void Apply(Window window, Action<VirtualTouchDevice> action) | |
{ | |
var wDevice = new VirtualTouchDevice(); | |
wDevice.SetActiveSource(PresentationSource.FromDependencyObject(window)); | |
try | |
{ | |
wDevice.Activate(); | |
action(wDevice); | |
} | |
finally | |
{ | |
wDevice.Deactivate(); | |
} | |
} | |
public override TouchPointCollection GetIntermediateTouchPoints(IInputElement relativeTo) | |
{ | |
return new TouchPointCollection(); | |
} | |
public override TouchPoint GetTouchPoint(IInputElement relativeTo) | |
{ | |
var wPosition = ActiveSource.RootVisual.PointFromScreen(Position); | |
if (relativeTo != null) | |
{ | |
wPosition = ActiveSource.RootVisual. | |
TransformToDescendant((Visual)relativeTo). | |
Transform(Position); | |
} | |
var wBounds = new Rect(wPosition.X - TouchRadius, wPosition.Y - TouchRadius, 2 * TouchRadius, 2 * TouchRadius); | |
return new TouchPoint(this, wPosition, wBounds, mLastAction); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment