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


相关文章
|
7月前
|
存储 安全 UED
Cyber Triage 3.13 for Windows - 数字取证和事件响应
Cyber Triage 3.13 for Windows - 数字取证和事件响应
199 71
Cyber Triage 3.13 for Windows - 数字取证和事件响应
|
9月前
|
缓存 安全 网络协议
使用事件日志识别常见 Windows 错误
事件查看器是Windows操作系统中的标准诊断工具,用于记录系统事件,包括硬件问题、软件中断和系统行为等详细信息。通过分析这些日志,管理员能够追踪和解决系统错误。访问方法包括使用快捷键Win + R输入eventvwr.msc,或通过控制面板进入。事件查看器中的每条记录包含事件ID、来源和描述,帮助识别和解决问题。常见错误如蓝屏死机、DLL错误、驱动程序错误等,可通过更新驱动程序、运行系统诊断、使用恢复功能等方式解决。
585 4
|
12月前
|
网络协议 API Windows
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
|
12月前
|
Windows
[原创]用MASM32编程获取windows类型
[原创]用MASM32编程获取windows类型
|
12月前
|
JavaScript 前端开发 API
MASM32编程通过WMI获取Windows计划任务
MASM32编程通过WMI获取Windows计划任务
|
12月前
|
API Windows
MASM32编程获取Windows当前桌面主题名
MASM32编程获取Windows当前桌面主题名
|
编译器 开发工具 C语言
解锁QtCreator跨界神技!Windows下轻松驾驭OpenCV动态库,让你的跨平台开发如虎添翼,秒变视觉编程大师!
【8月更文挑战第4天】QtCreator是一款强大的跨平台IDE,便于创建多平台应用。本教程教你如何在Windows环境下集成OpenCV库至Qt项目。首先,下载匹配MinGW的OpenCV预编译版并解压。接着,在QtCreator中新建或打开项目,并在.pro文件中添加OpenCV的头文件和库文件路径。确保编译器设置正确。随后编写测试代码,例如加载和显示图片,并进行编译运行。完成这些步骤后,你就能在QtCreator中利用OpenCV进行图像处理开发了。
614 6
|
数据库 Windows
超详细步骤解析:从零开始,手把手教你使用 Visual Studio 打造你的第一个 Windows Forms 应用程序,菜鸟也能轻松上手的编程入门指南来了!
【8月更文挑战第31天】创建你的第一个Windows Forms (WinForms) 应用程序是一个激动人心的过程,尤其适合编程新手。本指南将带你逐步完成一个简单WinForms 应用的开发。首先,在Visual Studio 中创建一个“Windows Forms App (.NET)”项目,命名为“我的第一个WinForms 应用”。接着,在空白窗体中添加一个按钮和一个标签控件,并设置按钮文本为“点击我”。然后,为按钮添加点击事件处理程序`button1_Click`,实现点击按钮后更新标签文本为“你好,你刚刚点击了按钮!”。
1217 0
|
Java C++
jni编程(windows+JDK11+clion)
jni编程(windows+JDK11+clion)
220 1
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
122 0