Windows Phone开发(21):做一个简单的绘图板

移动开发
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验整合至手机中。

其实我们今天要说的就是一个控件——InkPresenter,这个控件并不是十分强大,没办法和WPF中的InkCanvas相比,估计在实际开发中也很少可能会用到它,不过,我们还是来了解一下吧,毕竟用起来也不难。

使用该控件没有什么技术含量,注意一下以下几点就是了:

1、必须明确指定InkPresenter的宽度和高度,也就是不能使用自动值和Margin,不然不能收集墨迹,除非里面有子元素;

2、要收集墨迹,要设置Clip属性;

3、可以使用DrawingAttributes类设置墨迹的大小和颜色。

该控件不能像WPF那样自动实现收集墨迹的功能,也就是说只能是我们自己写代码了。

  1. <Grid> 
  2.     <InkPresenter x:Name="MyPresenter"  
  3.                   HorizontalAlignment="Left" 
  4.                   VerticalAlignment="Top"  
  5.                   MouseLeftButtonDown="MyPresenter_MouseLeftButtonDown" 
  6.                   LostMouseCapture="MyPresenter_LostMouseCapture" 
  7.                   MouseMove="MyPresenter_MouseMove" 
  8.                   Background="Transparent" 
  9.                   Opacity="1" Width="480" Height="750" /> 
  10. </Grid>
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Microsoft.Phone.Controls; 
  13. // 引入以下命名空间。 
  14. using System.Windows.Ink; 
  15. namespace InkPresentSample 
  16.     public partial class MainPage : PhoneApplicationPage 
  17.     { 
  18.         Stroke CurrentStroke = null
  19.         // 构造函数 
  20.         public MainPage() 
  21.         { 
  22.             InitializeComponent(); 
  23.             // 设置剪辑,以便收集墨迹 
  24.             RectangleGeometry rg = new RectangleGeometry(); 
  25.             // 为了使范围准确,应使用控件的最终呈现高度。 
  26.             rg.Rect = new Rect(0, 0, MyPresenter.ActualWidth, MyPresenter.ActualHeight); 
  27.             MyPresenter.Clip = rg; 
  28.         } 
  29.         private void MyPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
  30.         { 
  31.             // 当我们点击时获捉鼠标光标 
  32.             MyPresenter.CaptureMouse(); 
  33.             // 收集当前的光标所在的位置的点 
  34.             StylusPointCollection sc = new StylusPointCollection(); 
  35.             sc.Add(e.StylusDevice.GetStylusPoints(MyPresenter)); 
  36.             CurrentStroke = new Stroke(sc); 
  37.             // 设置笔触的颜色,大小 
  38.             CurrentStroke.DrawingAttributes.Color = Colors.Yellow; 
  39.             CurrentStroke.DrawingAttributes.Width = 8; 
  40.             CurrentStroke.DrawingAttributes.Height = 8; 
  41.             // 把新的笔触添加到集合中 
  42.             MyPresenter.Strokes.Add(CurrentStroke); 
  43.         } 
  44.         private void MyPresenter_LostMouseCapture(object sender, MouseEventArgs e) 
  45.         { 
  46.             // 当释放鼠标时,也同时释放笔触变量的引用 
  47.             CurrentStroke = null
  48.         } 
  49.         private void MyPresenter_MouseMove(object sender, MouseEventArgs e) 
  50.         { 
  51.             if (CurrentStroke != null
  52.             { 
  53.                 // 每移动一次鼠标,都收集对应的点。 
  54. CurrentStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(MyPresenter)); 
  55.             } 
  56.         } 
  57.     } 

责任编辑:闫佳明 来源: oschina
相关推荐

2013-04-23 17:11:47

Windows Pho用Express Bl

2020-07-20 10:00:52

Python翻译工具命令行

2011-02-28 09:22:47

SQLite记账簿

2015-07-03 11:27:30

程序员自己神器

2011-09-08 13:41:53

Widget

2010-07-05 15:18:01

Windows Pho

2023-05-26 16:42:28

2023-05-27 21:13:34

FlaskURL装饰器

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho

2009-08-25 01:46:00

C# WINDOWS服

2018-01-04 16:04:35

圆环放大动画

2011-06-08 10:24:38

Windows Pho 应用程序

2010-11-05 10:40:55

Windows Pho

2011-02-22 17:33:36

Windows Pho

2010-08-06 11:14:35

SilverlightWindows PhoWindows Pho

2011-06-08 10:01:36

Windows Pho 应用程序

2023-04-11 09:12:31

北向应用开发鸿蒙

2012-12-17 12:58:18

WebjQuery重构

2017-06-30 15:18:24

对账系统互联网
点赞
收藏

51CTO技术栈公众号