WPF开发多点程序

体会:使用wpf开发多点程序让我感到非常简单,这也无不证明了微软的强大。

第一步:打开vs2010,新建一个WpfApplication1程序

第二步:添加一个Canvas,并给Canvas添加 TouchDown,TouchMove,TouchUp事件

如:        <Canvas Background="Gray"  Name="canvas1" TouchDown="canvas1_TouchDown"
                 TouchMove="canvas1_TouchMove" TouchUp="canvas1_TouchUp"/>

第三步:定义一个数字字典存放对象:

          private Dictionary<int, Ellipse> ellipses = new Dictionary<int, Ellipse>();

        然后先三个事件中添加如下代码:

   

        private void canvas1_TouchDown(object sender, TouchEventArgs e)
        {
            Ellipse ellipse = new Ellipse();
            ellipse.Width = 50;
            ellipse.Height = 50;
            ellipse.Stroke = Brushes.YellowGreen;
            TouchPoint touchpoint = e.GetTouchPoint(this.canvas1);
            Canvas.SetTop(ellipse,touchpoint.Bounds.Top);
            Canvas.SetLeft(ellipse,touchpoint.Bounds.Left);
            ellipses[e.TouchDevice.Id] = ellipse;
            this.canvas1.Children.Add(ellipse);
        }

        private void canvas1_TouchMove(object sender, TouchEventArgs e)
        {
            Ellipse ellipse = ellipses[e.TouchDevice.Id];
            TouchPoint touchpoint = e.GetTouchPoint(this.canvas1);
            Canvas.SetTop(ellipse, touchpoint.Bounds.Top);
            Canvas.SetLeft(ellipse, touchpoint.Bounds.Left);

        }

        private void canvas1_TouchUp(object sender, TouchEventArgs e)
        {
            Ellipse ellipse = ellipses[e.TouchDevice.Id];
            ellipses.Remove(e.TouchDevice.Id);
            this.canvas1.Children.Remove(ellipse);
        }

编译运行OK,可以下载一个多点模拟器,有多点触摸屏硬件的更好。

 

效果如下图:

posted @ 2011-08-26 16:58  Xingsoft  阅读(2410)  评论(3编辑  收藏  举报