Windows Phone 7 触摸编程-单点触摸利用Touch.FrameReported事件

简介:

在WP7上Silverlight还支持多点触摸,有两种不同的编程模式:

1、低级别使用Touch.FrameReported事件

2、高级别的使用UIElement类中定义三个事件:ManipulationStarted,ManipulationDelta和ManipulationCompleted。

一、

第一种低级别的触摸编程是使用类TouchPoint,一个TouchPoint的实例代表一个特定的手指触摸屏幕。

TouchPoint的四个属性: 
• 动作的类型-枚举TouchAction,有Down, Move和Up四个值表示手指的按下、移动和离开。 
• 位置的类型-Point的位置,以左上角为参考点。 
• 大小的类型-Size,支持接触面积(手指的压力大小),但Windows 7不会返回电话有用的值。 
• 接触设备的类型TouchDevice。

该TouchDevice对象有两个得到只读属性: 
•ID int类型,用来区分手指,一个特定的手指有一个唯一测ID来触发所有的上下移动的事件。

• DirectlyOver UIElement的类型,你手指的最顶层元素。

 

使用Touch.FrameReported事件处理程序: 
Touch.FrameReported + = OnTouchFrameReported;


OnTouchFrameReported 方法格式如下:
void OnTouchFrameReported(object sender, TouchFrameEventArgs args)

{

}

TouchFrameEventArgs args事件有3个方法:

• GetTouchPoints(refElement)返回一个TouchPointCollection 获取多个接触点的集合
• GetPrimaryTouchPoint(refElement)返回一个TouchPoint 获取第一个手指接触点
• SuspendMousePromotionUntilTouchUp()


返回值是相对于传递的参数元素接触点的相对值。

当传递null的时候,GetTouchPoints得到Position属性相对于应用程序的左上角。

实例单点触摸改变字体的颜色

 

 

 

 
  1. 代码  
  2.  
  3. <phone:PhoneApplicationPage   
  4.     x:Class="SilverlightTouchHello.MainPage" 
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  6.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  7.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  9.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  12.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  13.     Foreground="{StaticResource PhoneForegroundBrush}" 
  14.     SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" 
  15.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  16.     shell:SystemTray.IsVisible="True"> 
  17.  
  18.     <!--LayoutRoot contains the root grid where all other page content is placed--> 
  19.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  20.         <Grid.RowDefinitions> 
  21.             <RowDefinition Height="Auto"/> 
  22.             <RowDefinition Height="*"/> 
  23.         </Grid.RowDefinitions> 
  24.  
  25.         <!--TitlePanel contains the name of the application and page title--> 
  26.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  27.             <TextBlock x:Name="ApplicationTitle" Text="SILVERLIGHT TOUCH HELLO" Style="{StaticResource PhoneTextNormalStyle}"/> 
  28.             <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  29.         </StackPanel> 
  30.  
  31.         <!--ContentPanel - place additional content here--> 
  32.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  33.             <TextBlock Name="txtblk" 
  34.                        Text="Hello, Windows Phone 7!" 
  35.                        Padding="0 22" 
  36.                        HorizontalAlignment="Center" 
  37.                        VerticalAlignment="Center" /> 
  38.         </Grid> 
  39.     </Grid> 
  40.       
  41.     <!-- Sample code showing usage of ApplicationBar  
  42.     <phone:PhoneApplicationPage.ApplicationBar> 
  43.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
  44.             <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="/Images/appbar_button1.png" Text="Button 1"></shell:ApplicationBarIconButton> 
  45.             <shell:ApplicationBarIconButton x:Name="appbar_button2" IconUri="/Images/appbar_button2.png" Text="Button 2"></shell:ApplicationBarIconButton> 
  46.             <shell:ApplicationBar.MenuItems> 
  47.                 <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="MenuItem 1"></shell:ApplicationBarMenuItem> 
  48.                 <shell:ApplicationBarMenuItem x:Name="menuItem2" Text="MenuItem 2"></shell:ApplicationBarMenuItem> 
  49.             </shell:ApplicationBar.MenuItems> 
  50.         </shell:ApplicationBar> 
  51.     </phone:PhoneApplicationPage.ApplicationBar> 
  52.     --> 
  53.  
  54.  
  55. </phone:PhoneApplicationPage> 

 

 

 
  1. 代码  
  2.  
  3. using System;  
  4. using System.Windows.Input;  
  5. using System.Windows.Media;  
  6. using Microsoft.Phone.Controls;  
  7.  
  8. namespace SilverlightTouchHello  
  9. {  
  10.     public partial class MainPage : PhoneApplicationPage  
  11.     {  
  12.         Random rand = new Random();  
  13.         Brush originalBrush;  
  14.           
  15.         public MainPage()  
  16.         {  
  17.             InitializeComponent();  
  18.             originalBrush = txtblk.Foreground;  
  19.             Touch.FrameReported += OnTouchFrameReported;  
  20.         }  
  21.  
  22.         void OnTouchFrameReported(object sender, TouchFrameEventArgs args)  
  23.         {  
  24.             TouchPoint primaryTouchPoint = args.GetPrimaryTouchPoint(null);  
  25.  
  26.             if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down)  
  27.             {  
  28.                 if (primaryTouchPoint.TouchDevice.DirectlyOver == txtblk)  
  29.                 {  
  30.                     txtblk.Foreground = new SolidColorBrush(  
  31.                                 Color.FromArgb(255, (byte)rand.Next(256),  
  32.                                                     (byte)rand.Next(256),  
  33.                                                     (byte)rand.Next(256)));  
  34.                 }  
  35.                 else  
  36.                 {  
  37.                     txtblk.Foreground = originalBrush;  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079241


相关文章
|
10月前
|
消息中间件 C++ Windows
02 MFC - Windows 编程模型
02 MFC - Windows 编程模型
39 0
|
27天前
|
编译器 开发工具 C语言
解锁QtCreator跨界神技!Windows下轻松驾驭OpenCV动态库,让你的跨平台开发如虎添翼,秒变视觉编程大师!
【8月更文挑战第4天】QtCreator是一款强大的跨平台IDE,便于创建多平台应用。本教程教你如何在Windows环境下集成OpenCV库至Qt项目。首先,下载匹配MinGW的OpenCV预编译版并解压。接着,在QtCreator中新建或打开项目,并在.pro文件中添加OpenCV的头文件和库文件路径。确保编译器设置正确。随后编写测试代码,例如加载和显示图片,并进行编译运行。完成这些步骤后,你就能在QtCreator中利用OpenCV进行图像处理开发了。
66 6
|
3月前
|
Java C++
jni编程(windows+JDK11+clion)
jni编程(windows+JDK11+clion)
39 1
|
4月前
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
44 0
|
4月前
|
Windows
火山中文编程 -- 第一个windows程序
火山中文编程 -- 第一个windows程序
27 0
|
4月前
|
编译器 API Windows
windows编程基础
windows编程基础
33 0
|
4月前
|
Windows
win32编程 -- windows绘图操作
win32编程 -- windows绘图操作
56 0
|
4月前
|
网络协议 Linux C语言
005.在Windows下编程让效率起飞
windows开发Linux方式: 先用编辑器编写源代码 然后进入Linux 系统,使用gcc编译器(后面会讲),对源代码进行编译运行。 熟练后推荐使用VS2019 开发Linux C++ 程序 将自己的Ip地址设为静态IP
59 1
|
4月前
|
人工智能 机器人 C#
Windows编程课设(C#)——基于WPF和.net的即时通讯系统(仿微信)
一款参考QQ、微信的即时通讯软件。采用CS结构,客户端基于.Net与WPF开发,服务端使用Java开发。
|
11月前
|
消息中间件 Ubuntu 编译器
Windows编程
Windows编程
44 0
下一篇
云函数