Beispiel #1
0
        /// <summary>
        /// http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5c8ff964-c087-43c0-a88d-0ce7719e033c
        /// c:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\Touch\MTScratchpadWMTouch\CS\
        /// </summary>
        private bool DecodeTouch(IntPtr wParam, IntPtr lParam)
        {
            // More than one touch input may be associated with a touch message,
            // so an array is needed to get all event information.
            int inputCount = LoWord(wParam.ToInt32()); // Number of touch inputs, actual per-contact messages
            var inputs = new TOUCHINPUT[inputCount]; // Allocate the storage for the parameters of the per-contact messages
                                                     // Unpack message parameters into the array of TOUCHINPUT structures, each
                                                     // representing a message for one single contact.
            var touchInputSize = Marshal.SizeOf(new TOUCHINPUT());
            if (!GetTouchInputInfo(lParam, inputCount, inputs, touchInputSize))
            {
                // Get touch info failed.
                return false;
            }
            // For each contact, dispatch the message to the appropriate message
            // handler.
            bool handled = false; // Boolean, is message handled
            for (int i = 0; i < inputCount; i++)
            {
                TOUCHINPUT ti = inputs[i];

                // Assign a handler to this message.
                EventHandler<MouseTouchEventArgs> handler = null;     // Touch event handler
                if ((ti.dwFlags & TOUCHEVENTF_DOWN) != 0)
                {
                    handler = OnTouchDown;
                }
                else if ((ti.dwFlags & TOUCHEVENTF_UP) != 0)
                {
                    handler = OnTouchUp;
                }
                else if ((ti.dwFlags & TOUCHEVENTF_MOVE) != 0)
                {
                    handler = OnTouchMove;
                }
                // Convert message parameters into touch event arguments and handle the event.
                if (handler != null)
                {
                    // Convert the raw touch input message into a touch event.
                    var te = new MouseTouchEventArgs(); // Touch event arguments
                                                        // TOUCHINFO point coordinates and contact size is in 1/100 of a pixel; convert it to pixels.
                                                        // Also convert screen to client coordinates.
                    te.ContactY = ti.cyContact / 100;
                    te.ContactX = ti.cxContact / 100;
                    te.Id = ti.dwID;
                    {
                        te.LocationX = ti.x / 100;
                        te.LocationY = ti.y / 100;
                    }
                    te.Time = ti.dwTime;
                    te.Mask = ti.dwMask;
                    te.Flags = ti.dwFlags;
                    // Invoke the event handler.
                    handler(this, te);
                    // Mark this event as handled.
                    handled = true;
                }
            }
            CloseTouchInputHandle(lParam);
            return handled;
        }
Beispiel #2
0
        /// <summary>
        /// http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5c8ff964-c087-43c0-a88d-0ce7719e033c
        /// c:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\Touch\MTScratchpadWMTouch\CS\
        /// </summary>
        private bool DecodeTouch(IntPtr wParam, IntPtr lParam)
        {
            // More than one touch input may be associated with a touch message,
            // so an array is needed to get all event information.
            int inputCount = LoWord(wParam.ToInt32());           // Number of touch inputs, actual per-contact messages
            var inputs     = new TOUCHINPUT[inputCount];         // Allocate the storage for the parameters of the per-contact messages
            // Unpack message parameters into the array of TOUCHINPUT structures, each
            // representing a message for one single contact.
            var touchInputSize = Marshal.SizeOf(new TOUCHINPUT());

            if (!NativeMethods.GetTouchInputInfo(lParam, inputCount, inputs, touchInputSize))
            {
                // Get touch info failed.
                return(false);
            }
            // For each contact, dispatch the message to the appropriate message
            // handler.
            bool handled = false;             // Boolean, is message handled

            for (int i = 0; i < inputCount; i++)
            {
                TOUCHINPUT ti = inputs[i];

                // Assign a handler to this message.
                EventHandler <MouseTouchEventArgs> handler = null;                    // Touch event handler
                if ((ti.dwFlags & TOUCHEVENTF_DOWN) != 0)
                {
                    handler = OnTouchDown;
                }
                else if ((ti.dwFlags & TOUCHEVENTF_UP) != 0)
                {
                    handler = OnTouchUp;
                }
                else if ((ti.dwFlags & TOUCHEVENTF_MOVE) != 0)
                {
                    handler = OnTouchMove;
                }
                // Convert message parameters into touch event arguments and handle the event.
                if (handler != null)
                {
                    // Convert the raw touch input message into a touch event.
                    var te = new MouseTouchEventArgs();                     // Touch event arguments
                    // TOUCHINFO point coordinates and contact size is in 1/100 of a pixel; convert it to pixels.
                    // Also convert screen to client coordinates.
                    te.ContactY = ti.cyContact / 100;
                    te.ContactX = ti.cxContact / 100;
                    te.Id       = ti.dwID;
                    {
                        te.LocationX = ti.x / 100;
                        te.LocationY = ti.y / 100;
                    }
                    te.Time  = ti.dwTime;
                    te.Mask  = ti.dwMask;
                    te.Flags = ti.dwFlags;
                    // Invoke the event handler.
                    handler(this, te);
                    // Mark this event as handled.
                    handled = true;
                }
            }
            NativeMethods.CloseTouchInputHandle(lParam);
            return(handled);
        }