Click here to Skip to main content
15,895,557 members
Articles / Desktop Programming / WPF

Image Manipulation in Multitouch Development

Rate me:
Please Sign up or sign in to vote.
4.81/5 (8 votes)
3 Apr 2010CPOL3 min read 61.4K   2.5K   19  
In this article, I will describe Image manipulation in Windows 7 multitouch Environment
//-----------------------------------------------------------------------------
// 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
{
    /// <summary>
    /// Represents a WPF Window
    /// </summary>
    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
    }

    /// <summary>
    /// The WPF GUI Timer for Inerta Events
    /// </summary>
    public class GUITimer : System.Windows.Threading.DispatcherTimer, IGUITimer
    {
        /// <summary>
        /// Do Nothing
        /// </summary>
        public void Dispose()
        {
            
        }

        /// <summary>
        /// Get/Set the timer interval
        /// </summary>
        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;
            }
        }
    }

    /// <summary>
    /// Helper class for creating Gesture handler and Enabling touch for WPF application
    /// </summary>
    public class Factory
    {
        /// <summary>
        /// A factory that creates gesture handler for a WPF Window
        /// </summary>
        /// <remarks>since WPF does not support Touch events, only Gesture handler can be created</remarks>
        /// <param name="window">The window that need touch or gesture events</param>
        /// <returns>The Gesture Handler</returns>
        public static GestureHandler CreateGestureHandler(System.Windows.Window window) 
        {
            return Windows7.Multitouch.Handler.CreateHandler<GestureHandler>(new WindowWrapper(window));
        }

        /// <summary>
        /// Enable Stylus events, that represent touch events. 
        /// </summary>
        /// <remarks>Each stylus device has an Id that is corelate to the touch Id</remarks>
        /// <param name="window">The WPF window that needs stylus events</param>
        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));
        }


        /// <summary>
        /// Create a wrapper for a UI based timer 
        /// </summary>
        /// <remarks>This timer is called in the context of the UI thread</remarks>
        /// <returns>A timer Wrapper</returns>
        public static IGUITimer CreateTimer()
        {
            return new GUITimer();
        }
    }

    /// <summary>
    /// Helper class to convert drawing point tp WPF point and vice versa
    /// </summary>
    public static class PointUtil
    {
        /// <summary>
        /// Convert WPF point to drawing point
        /// </summary>
        /// <param name="p">WPF point</param>
        /// <returns>Drawing Point</returns>
        public static System.Drawing.PointF ToDrawingPointF(this System.Windows.Point p)
        {
            return new System.Drawing.PointF((float)p.X, (float)p.Y);
        }

        /// <summary>
        /// Convert drawing point to WPF point
        /// </summary>
        /// <param name="p">Drawing Point</param>
        /// <returns>WPF Point</returns>
        public static System.Windows.Point ToDrawingPointF(this System.Drawing.PointF p)
        {
            return new System.Windows.Point(p.X, p.Y);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions