//----------------------------------------------------------------------------- // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Interop; using Windows7.Multitouch.Interop; namespace Windows7.Multitouch.WPF { /// /// Represents a WPF Window /// class WindowWrapper : IHwndWrapper { private readonly System.Windows.Window _window; public WindowWrapper(System.Windows.Window window) { _window = window; HandleCreated += (s,e) => {}; _window.Loaded += (s,e) => { HandleCreated(s, EventArgs.Empty); }; } #region IHwndWrapper Members public IntPtr Handle { get { return System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; } } public object Source { get { return _window; } } public event EventHandler HandleCreated; public event EventHandler HandleDestroyed { add { _window.Closed += value; } remove { _window.Closed -= value; } } public bool IsHandleCreated { get { return Handle != IntPtr.Zero; } } public System.Drawing.Point PointToClient(System.Drawing.Point point) { System.Windows.Point sourcePoint = new System.Windows.Point(point.X, point.Y); System.Windows.Point destinationPoint = _window.PointFromScreen(sourcePoint); return new System.Drawing.Point((int)(0.5 + destinationPoint.X), (int)(0.5 + destinationPoint.Y)); } #endregion } /// /// The WPF GUI Timer for Inerta Events /// public class GUITimer : System.Windows.Threading.DispatcherTimer, IGUITimer { /// /// Do Nothing /// public void Dispose() { } /// /// Get/Set the timer interval /// int IGUITimer.Interval { get { return (int)base.Interval.Ticks; } set { base.Interval = TimeSpan.FromTicks(value); } } bool IGUITimer.Enabled { get { return base.IsEnabled; } set { base.IsEnabled = value; } } } /// /// Helper class for creating Gesture handler and Enabling touch for WPF application /// public class Factory { /// /// A factory that creates gesture handler for a WPF Window /// /// since WPF does not support Touch events, only Gesture handler can be created /// The window that need touch or gesture events /// The Gesture Handler public static GestureHandler CreateGestureHandler(System.Windows.Window window) { return Windows7.Multitouch.Handler.CreateHandler(new WindowWrapper(window)); } /// /// Enable Stylus events, that represent touch events. /// /// Each stylus device has an Id that is corelate to the touch Id /// The WPF window that needs stylus events public static void EnableStylusEvents(System.Windows.Window window) { WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window); // Set the window property to enable multitouch input on inking context. User32.SetProp(windowInteropHelper.Handle, "MicrosoftTabletPenServiceProperty", new IntPtr(0x01000000)); } /// /// Create a wrapper for a UI based timer /// /// This timer is called in the context of the UI thread /// A timer Wrapper public static IGUITimer CreateTimer() { return new GUITimer(); } } /// /// Helper class to convert drawing point tp WPF point and vice versa /// public static class PointUtil { /// /// Convert WPF point to drawing point /// /// WPF point /// Drawing Point public static System.Drawing.PointF ToDrawingPointF(this System.Windows.Point p) { return new System.Drawing.PointF((float)p.X, (float)p.Y); } /// /// Convert drawing point to WPF point /// /// Drawing Point /// WPF Point public static System.Windows.Point ToDrawingPointF(this System.Drawing.PointF p) { return new System.Windows.Point(p.X, p.Y); } } }