WPF 触摸操作示例

<Window x:Class="WPFForStudy.Manipulation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Move, Size, and Rotate the Square"
        WindowState="Maximized"
        ManipulationStarting="Window_ManipulationStarting"
        ManipulationDelta="Window_ManipulationDelta"
        ManipulationInertiaStarting="Window_InertiaStarting">
    <Window.Resources>

        <!--The movement, rotation, and size of the Rectangle is specified by its RenderTransform.-->
        <MatrixTransform x:Key="InitialMatrixTransform">
            <MatrixTransform.Matrix>
                <Matrix OffsetX="200" OffsetY="200"/>
            </MatrixTransform.Matrix>
        </MatrixTransform>
    </Window.Resources>

    <Canvas>
        <Rectangle Fill="Red" Name="manRect"
                 Width="200" Height="200"
                 RenderTransform="{StaticResource InitialMatrixTransform}"
                 IsManipulationEnabled="true" />

        <Rectangle Fill="Green" Name="manRect2"
                 Width="100" Height="100"
                 RenderTransform="{StaticResource InitialMatrixTransform}"
                 IsManipulationEnabled="true" />
    </Canvas>
</Window>
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WPFForStudy
{
    /// <summary>
    /// Manipulation.xaml 的交互逻辑
    /// </summary>
    public partial class Manipulation : Window
    {
        public Manipulation()
        {
            InitializeComponent();
        }

        void Window_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            e.ManipulationContainer = this;
            e.Handled = true;
        }

        void Window_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {

            // Get the Rectangle and its RenderTransform matrix.
            Rectangle rectToMove = e.OriginalSource as Rectangle;
            Matrix rectsMatrix = ((MatrixTransform)rectToMove.RenderTransform).Matrix;

            // Rotate the Rectangle.
            rectsMatrix.RotateAt(e.DeltaManipulation.Rotation,
                                 e.ManipulationOrigin.X,
                                 e.ManipulationOrigin.Y);

            // Resize the Rectangle.  Keep it square
            // so use only the X value of Scale.
            rectsMatrix.ScaleAt(e.DeltaManipulation.Scale.X,
                                e.DeltaManipulation.Scale.X,
                                e.ManipulationOrigin.X,
                                e.ManipulationOrigin.Y);

            // Move the Rectangle.
            rectsMatrix.Translate(e.DeltaManipulation.Translation.X,
                                  e.DeltaManipulation.Translation.Y);

            // Apply the changes to the Rectangle.
            rectToMove.RenderTransform = new MatrixTransform(rectsMatrix);

            Rect containingRect =
                new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);

            Rect shapeBounds =
                rectToMove.RenderTransform.TransformBounds(
                    new Rect(rectToMove.RenderSize));

            // Check if the rectangle is completely in the window.
            // If it is not and intertia is occuring, stop the manipulation.
            if (e.IsInertial && !containingRect.Contains(shapeBounds))
            {
                e.Complete();
            }

            e.Handled = true;
        }

        void Window_InertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
        {

            // Decrease the velocity of the Rectangle‘s movement by
            // 10 inches per second every second.
            // (10 inches * 96 pixels per inch / 1000ms^2)
            e.TranslationBehavior.DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0);

            // Decrease the velocity of the Rectangle‘s resizing by
            // 0.1 inches per second every second.
            // (0.1 inches * 96 pixels per inch / (1000ms^2)
            e.ExpansionBehavior.DesiredDeceleration = 0.1 * 96 / (1000.0 * 1000.0);

            // Decrease the velocity of the Rectangle‘s rotation rate by
            // 2 rotations per second every second.
            // (2 * 360 degrees / (1000ms^2)
            e.RotationBehavior.DesiredDeceleration = 720 / (1000.0 * 1000.0);

            e.Handled = true;
        }
    }
}

  

时间: 2025-01-13 14:38:08

WPF 触摸操作示例的相关文章

WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序

原文:WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序 前面我讲了在win7下使用模拟器,进行调试模拟多点触摸,其实际开发中这样也比较麻烦.. 要拿几个鼠标. 所以更多的人会 买个触摸套 套在显示屏上. 这样可支持 2点触摸. 又没有正真触摸屏的昂贵,价格也相对优惠很多. 接下来进入正题 . WPF其实本身对触摸事件就支持. 其中有些高及触屏的操作的手势路由事件. 大大增强了用户体验性. 使得我们能做出类似iphone手机的的触控效果.并使之这些柔和的效果 移植到桌面触摸应用上. 首先

用gdb分析core文件及常见gdb命令操作示例

1.概述 在实际的软件开发项目中,程序出现问题是在所难免的.遥想本人参加工作之后首次遇到程序的情景,至今还历历在目.之前的经验告诉我,我们越是惊慌失措,问题就越是解决不了.我们要先让自己平静下来,然后再寻找解决程序问题的办法. 在Linux下做开发的朋友,想必都与core文件打过交道.当看到自己的程序运行之后出现core时,很多人都慌乱了,仿佛天快要塌下来一样.其实,我们大可不必如此,只要我们掌握了用gdb调试core文件的办法,依然可以很快定位程序问题,一举将bug消灭掉.有关Linux co

java 线程 原子类相关操作示例 thinking in java4 目录21.3.4

java 线程  原子类相关操作示例 package org.rui.thread.volatiles; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; /** * 原子类,

java目录相关操作 示例代码

package org.rui.io; import java.io.File; import java.io.FilenameFilter; import java.util.Arrays; import java.util.regex.Pattern; /** * 目录列表器 测试 * * @author lenovo * */ //Args:"C:/Users/lenovo/Pictures/screen/*\.jpg" public class DirList { public

AudioPlayer.js,一个响应式且支持触摸操作的jquery音频插件

AudioPlayer.js是一个响应式.支持触摸操作的HTML5 的音乐播放器.本文是对其官网的说用说明文档得翻译,博主第一次翻译外文.不到之处还请谅解.之处. JS文件地址:http://osvaldas.info/examples/audio-player-responsive-and-touch-friendly/audioplayer.js 你可以在右键点击上面的地址,然后选择另存为把JS文件保存到本地. 英文原文地址:http://osvaldas.info/audio-player

iOS中如何获取用户的触摸操作

iOS设备是一个多点触控的设备,在屏幕上可以进行多个手指的控制.那么如何在开发中获取用户的手势操作呢?iOS有四种手指的操作,分别是按下,抬起,移动和取消.四个方法如下: //按下屏幕,开始触摸: override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { println("touchesBegan") } //手指抬起,结束触摸: override func touchesE

字符串去空格以及反转操作示例

1.字符串去空格 package tan; public class CopyOfStringTest { public static void main(String[] args) { String s = " tan at guigu "; sop(s); String x = MyTrim(s); sop(x); } public static void sop(String str) { System.out.println(str); } // 1.去除字符串两端的空格 p

cocos2d-x 显示触摸操作(显示水波点击效果,用于视频演示)

昨天刚刚參加玩游戏设计大赛, 积累了一些东西. 接下去将会逐个分享出来. 首先是显示触摸操作. 由于要演示我们的作品.使用试玩过程中, 假设没办法显示我们的触摸操作(像录制视频一样, 点击了屏幕某点, 出现红点或者水波荡漾这种效果), 那样的话演示效果不好. 观众就无法直观的了解我们的游戏.所以考虑加入这个功能. 之后, 走了点弯路.一直在考虑手机本身有没有这个功能,后来找了非常久.非越狱iPhone是没有这个功能的. 于是乎, 自己写呗. 详细效果例如以下: 实现非常easy.主要用到了一个粒

验证码接收平台原理和网页版运行操作示例

现在市面上能够使用的验证码接收平台有:    Y码:www.yma0.com 牛码:www.niuma.org 接码:www.7vs.net 极码:www.yzm8.net 这些平台得以实现的运行原理: 思路:       A:获得验证码:      1.找到相关的表.      2.用什么发送,post,get ,ajax,当然ajax首选      3.post之前要js先判断是手机号码11位,并且全部都是数字,或者用正则也行.      4.用ajax发送数据data,要对数据进行检验,过