需求:现,在窗口下有一个StackPanel控件.

  1.可以拖动.

  2.可以展开及收缩(不仅仅可以拖动还可以点击)

  3.窗口向坐标轴一样分四个象限,在不同的区域停靠在不同的地方(四个角).

第一阶段:

  我遇到的问题:

  1.起初完成的时候发现StackPanel拖动的时候一直发疯一样的抖,

   解决方法:ManipulationStarting事件中,e.ManipulationContainer = this.myGrid,容器要父控件,我原先写成自己本身了.

  2.为啥写了之后触控点不动?

   解决发发:查看构造函数中myStackPanel.RenderTransform = new MatrixTransform(),但是这样做会给后面留下新的问题,花了我一天时间找出这处.后面详述.

 1 Xaml's code:
 2
 3 <Window x:Class="LearnWpf.ManipulationDemo"
 4         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 5         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 6         Title="ManipulationDemo" Height="300" Width="300">
 7     <Grid x:Name="myGrid">
 8         <StackPanel x:Name="myStackPanel"
 9                     Background="Red" Height="50" Width="50"
10                     ManipulationStarting="StackPanel_ManipulationStarting"
11                     ManipulationDelta="StackPanel_ManipulationDelta"
12                     ManipulationCompleted="StackPanel_ManipulationCompleted"
13                     />
14     </Grid>
15 </Window>

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Shapes;
13
14 namespace LearnWpf
15 {
16     /// <summary>
17     /// ManipulationDemo.xaml 的交互逻辑
18     /// </summary>
19     public partial class ManipulationDemo : Window
20     {
21         public ManipulationDemo()
22         {
23             InitializeComponent();
24             myStackPanel.RenderTransform = new MatrixTransform();
25             myStackPanel.IsManipulationEnabled = true;
26         }
27
28         private void StackPanel_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
29         {
30             StackPanel element = (StackPanel)e.OriginalSource;
31             e.ManipulationContainer = this.myGrid;
32             e.Mode = ManipulationModes.All;
33         }
34
35         private void StackPanel_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
36         {
37             FrameworkElement element = (FrameworkElement)e.Source;
38             Matrix matrix = ((MatrixTransform)element.RenderTransform).Matrix;
39             ManipulationDelta deltaManipulation = e.DeltaManipulation;
40             Point center = new Point(element.ActualWidth / 2, element.ActualHeight / 2);
41             center = matrix.Transform(center);
42             matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, center.X, center.Y);
43             matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
44             matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
45             ((MatrixTransform)element.RenderTransform).Matrix = matrix;
46         }
47
48         private void StackPanel_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
49         {
50
51         }
52     }
53 }

第二阶段:

<这里我想把它推上首页了,因为很多地方肯定没有做好,希望各位提提宝贵意见,还有一些问题想请教大家.还有很多地方注释没有完善

,因为我是现写的,请大家多多包容>

  上一章,我们完成了触控条件下的操作,但是现在有一个问题:鼠标肿么办?

  解决方案:利用MouseLeftButtonDown,MouseMove,MouseLeftButtonUp写.

  还有需求中的要点击怎么办?

  解决方案:这个我是要肿么办呢?决定用Touch事件做了.但是感觉又跟Manipulation重复了.详情请看代码.Touch事件和Manipulation事件重复了,没有注释掉,可以注释掉Manipulation事件,各位亲自理.

  遇到问题:当鼠标和触摸两个事件一起发生时,会发生一起奇怪的现象,我做了处理,但是不能够解决,各位大大有什么看法?  

 1 <Window x:Class="LearnWpf.ManipulationDemo"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="ManipulationDemo" WindowState="Maximized" Height="300" Width="300" MouseLeftButtonDown="Window_MouseLeftButtonDown" MouseMove="Window_MouseMove" MouseLeftButtonUp="Window_MouseLeftButtonUp">
 5     <Grid x:Name="myGrid">
 6         <StackPanel x:Name="myStackPanel"
 7                     Background="Red" Height="200" Width="200"
 8                     ManipulationStarting="StackPanel_ManipulationStarting" ManipulationDelta="StackPanel_ManipulationDelta" ManipulationCompleted="StackPanel_ManipulationCompleted"
 9                     TouchDown="myStackPanel_TouchDown" TouchMove="myStackPanel_TouchMove" TouchUp="myStackPanel_TouchUp"/>
10     </Grid>
11 </Window>

  1 CSharp Code
  2  using System;
  3  using System.Collections.Generic;
  4  using System.Linq;
  5  using System.Text;
  6  using System.Windows;
  7  using System.Windows.Controls;
  8  using System.Windows.Data;
  9  using System.Windows.Documents;
 10  using System.Windows.Input;
 11  using System.Windows.Media;
 12  using System.Windows.Media.Imaging;
 13  using System.Windows.Shapes;
 14
 15  namespace LearnWpf
 16  {
 17      /// <summary>
 18      /// ManipulationDemo.xaml 的交互逻辑
 19      /// </summary>
 20      public partial class ManipulationDemo : Window
 21      {
 22          FrameworkElement frameworkElement; //想要操纵的元素
 23          bool isFrameworkElementSelect; //想要用鼠标移动的元素是否被选中
 24          bool isMouse = false; //鼠标操作中
 25          bool isTouch = false; //触摸操作中
 26          public ManipulationDemo()
 27          {
 28              InitializeComponent();
 29              frameworkElement = this.myStackPanel;
 30              myStackPanel.RenderTransform = new MatrixTransform();
 31              myStackPanel.IsManipulationEnabled = true;
 32          }
 33
 34          private void StackPanel_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
 35          {
 36              frameworkElement = (FrameworkElement)e.OriginalSource;
 37              e.ManipulationContainer = this.myGrid;
 38              e.Mode = ManipulationModes.All;
 39              frameworkElement.Opacity = 0.5;
 40          }
 41
 42          private void StackPanel_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
 43          {
 44              FrameworkElement element = (FrameworkElement)e.Source;
 45              Matrix matrix = ((MatrixTransform)element.RenderTransform).Matrix;
 46              ManipulationDelta deltaManipulation = e.DeltaManipulation;
 47              Point center = new Point(element.ActualWidth / 2, element.ActualHeight / 2);
 48              center = matrix.Transform(center);
 49              matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, center.X, center.Y);
 50              matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
 51              //matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);
 52              ((MatrixTransform)element.RenderTransform).Matrix = matrix;
 53          }
 54
 55          private void StackPanel_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
 56          {
 57              frameworkElement.Opacity = 1;
 58          }
 59
 60          #region 坐标的相关变量定义
 61          double dx; //x轴方向的每次移动的距离
 62          double dy; //y轴方向每次移动的距离
 63
 64          double tdx; //x轴方向的每次移动的总距离
 65          double tdy; //y轴方向的每次移动的总距离
 66
 67          double opx; //旧的x轴的值
 68          double opy; //旧的y轴的值
 69
 70          double npx; //新的x轴的值
 71          double npy; //新的y轴的值
 72          #endregion
 73
 74          #region Touch事件
 75          private void myStackPanel_TouchDown(object sender, TouchEventArgs e)
 76          {
 77              if (isMouse) return;
 78              isTouch = true;
 79
 80              tdx = 0;
 81              tdy = 0;
 82
 83              Point p = e.GetTouchPoint(this).Position;
 84              opx = p.X;
 85              opy = p.Y;
 86          }
 87          private void myStackPanel_TouchMove(object sender, TouchEventArgs e)
 88          {
 89              if (isMouse) return;
 90
 91              Point p = e.GetTouchPoint(this).Position;
 92              npx = p.X;
 93              npy = p.Y;
 94              dx = npx - opx;
 95              dy = npy - opy;
 96              opx = npx;
 97              opy = npy;
 98
 99              tdx += Math.Abs(dx);
100              tdy += Math.Abs(dy);
101              Move(dx, dy);
102          }
103          private void myStackPanel_TouchUp(object sender, TouchEventArgs e)
104          {
105              if (isMouse) return;
106
107              if (tdx < 5 || tdy < 5)
108              {
109                  Click();
110              }
111
112              isTouch = false;
113          }
114          #endregion
115
116          /// <summary>
117          /// 移动frameElement方法
118          /// </summary>
119          /// <param name="dx"></param>
120          /// <param name="dy"></param>
121          private void Move(double dx,double dy)
122          {
123              Matrix matrix = ((MatrixTransform)frameworkElement.RenderTransform).Matrix;
124              matrix.Translate(dx, dy);
125              this.frameworkElement.RenderTransform = new MatrixTransform(matrix);
126          }
127
128          /// <summary>
129          /// Click触发的方法
130          /// </summary>
131          private void Click()
132          {
133              MessageBox.Show("U hurt me");
134          }
135
136          private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
137          {
138              if (isTouch) return;
139              isMouse = true;
140
141              tdx = 0;
142              tdy = 0;
143
144              frameworkElement.Opacity = 0.5;
145              frameworkElement = (FrameworkElement)e.OriginalSource;
146              isFrameworkElementSelect = true;
147              Point p = e.GetPosition(this);
148              opx = p.X;
149              opy = p.Y;
150
151          }
152
153          private void Window_MouseMove(object sender, MouseEventArgs e)
154          {
155              if (isTouch) return;
156
157              if (isFrameworkElementSelect)
158              {
159                  Point p = e.GetPosition(this);
160                  npx = p.X;
161                  npy = p.Y;
162                  dx = npx - opx;
163                  dy = npy - opy;
164                  opx = npx;
165                  opy = npy;
166
167                  tdx += Math.Abs(dx);
168                  tdy += Math.Abs(dy);
169                  Move(dx, dy);
170              }
171          }
172
173          private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
174          {
175              if (isTouch) return;
176
177              frameworkElement.Opacity = 1;
178              isFrameworkElementSelect = false;
179              if (tdx < 5 || tdy < 5)
180              {
181                  Click();
182              }
183
184              isMouse = false;
185          }
186      }
187  }

11/15/2012 初次整理

转载于:https://www.cnblogs.com/XzcBlog/archive/2012/09/05/2671668.html

WPF之Manipulation相关推荐

  1. WPF Multi-Touch 开发:高级触屏操作(Manipulation)

    在上一篇中我们对基础触控操作有了初步了解,本篇将继续介绍触碰控制的高级操作(Manipulation),在高级操作中包含了一些特殊的触屏手势:平移.缩放.旋转,当然在WPF 中无需自行开发这些手势,只 ...

  2. WPF Multi-Touch 开发:惯性效果(Inertia)

    从上一篇实例可以发现在图片移动过程中如果将手指移开屏幕则图片会立刻停止,根据这种情况WPF 提供另外一种惯性效果(Inertia).通过它可以使UI 单元移动的更加符合物理特性.更为实际和流畅. 在前 ...

  3. 分享Silverlight/WPF/Windows Phone一周学习导读(12月20日-12月26日)

    上周圣诞节,Silverlight社区有些冷清,没有太多新鲜的事情.总结上周的Silverlight,WPF和Windows Phone的学习文章. 本周Silverlight学习资源更新: 解决Si ...

  4. 分享Silverlight/Windows8/WPF/WP7/HTML5一周学习导读(5月14日-5月20日)

    分享Silverlight/Windows8/WPF/WP7/HTML5一周学习导读(5月14日-5月20日) 本周Silverlight学习资源更新 MVVM设计模式相关--Silverlight商 ...

  5. wpf时间显示代码_如何在ASP.NET和WPF中显示QR代码

    wpf时间显示代码 I've half-jokingly said that there's never a good reason to use a QR Code. However, I'm wo ...

  6. 基于 WPF + Modern UI 的 公司OA小助手 开发总结

    前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个阶段的一个反思.人只有在总结的过程中才会发现自己的不足. 公司每天都要在OA系统上上班点击签到,下班点击签退 ...

  7. 学习Modern UI for WPF

    这两天断断续续的学了学Modern UI for WPF 没啥学习笔记呵呵,来自大牛王春明的博客园 http://www.cnblogs.com/wangchunming/category/34288 ...

  8. [转][小结][三种方法]实现WPF不规则窗体

    实现WPF不规则窗体的三种常用的方法如下: 1.使用Blend等工具绘制一个不规则xaml,然后作为窗体的背景.这个可以参考xiaowei0705的这篇博文:WPF制作不规则的窗体 . 2.给wind ...

  9. WPF:跨应用程序会话保持和还原应用程序范围的属性

    所谓的wpf夸应用程序员会话保持和还原.其实就是将多个应用程序都用的资源保存到一个独立的文件存储系统中.这个应用程序退出的时候将数据写入文件中,其他应用程序使用的时候可以去读取这个文件 这个地方用到了 ...

最新文章

  1. Net设计模式实例之模板方法模式(Template Mothed Pattern)(1)
  2. MYSQL启用日志,查看日志,利用mysqlbinlog工具恢复MySQL数据库
  3. requestWindowFeature()的应用
  4. plt title设置在下方_Python数据分析:可视化图表注释设置
  5. 文件上传的注意细节(PHP)
  6. 如何利用DeepFM算法设计推荐系统
  7. Java集合之EnumSet
  8. server.transfer 无法跳转页面_H5 腾讯地图无法导航
  9. java同名函数_浅谈Java 继承接口同名函数问题
  10. 阿里云服务器(windows2012)
  11. php 中c函数重载,php函数重载的替代方法--伪重载详解
  12. 【Java从0到架构师】SpringCloud - Hystrix、Zuul
  13. 数据库修改服务器ip地址吗,服务器数据库与改ip地址吗
  14. HP Networking/Comware NETCONF interface quick tutorial (using python’s ncclient and pyhpecw7)
  15. Mugeda(木疙瘩)H5案例课—世界名画抖抖抖起来了-岑远科-专题视频课程
  16. 移动接入的身份认证技术
  17. System mem和AGP mem和video mem
  18. 多值逻辑与计算机科学,多值逻辑
  19. Python原生服务端签名生成请求订单信息「orderString」
  20. 截流式合流制设计流量计算_截流式合流制管道系统的设计说明

热门文章

  1. yii mysql条件查询_mysql – 在Yii2 find()/ QueryBuilder中使用WHERE条件的SELECT子查询
  2. html5 视口,html5 – 在媒体查询中更改视口
  3. juypter 不省略_常用pandas清洗数据命令
  4. synchronized 解决死锁的问题 轉貼
  5. 【BZOJ2437】【codevs1949】兔兔与蛋蛋游戏,博弈+二分图匹配
  6. 【BZOJ1412】【codevs2351】狼和羊的故事,最小割
  7. 【codevs2460】【BZOJ1036】树的统计count,第一次的树链剖分
  8. 2017.9.13 序列维护 思考记录
  9. inchat库下载 python_Linux 环境下安装 Python3 的操作方法
  10. 【英语学习】【Daily English】U11 Work L03 He is a had guy to work for