让 InkCanvas 支持 Multitouch

最近在做Multitouch的demo应用,其中有个白板的demo,尝试了使用WPF的InkCanvas。
(以前真没有做过.Net开发,可能我写的东西很幼稚)
下面的代码片段就是接收来自MultiTouch的消息,转化为InkCanvas的Stroke。
由于Touch的接收是在另外一个线程,它不能直接访问UI线程里面的托管对象,所以需要通过特殊的方式来更新InkCanvas。
    public struct NewCursor
    {
        public StylusPointCollection spc;
        public Stroke stroke;
    }
 public partial class InkPadWindow : TuioListener
 {
        ....... 一些其他的处理函数
        private Dictionary<long, NewCursor> spcList = new Dictionary<long, NewCursor>(32);
        private delegate void updateStrokeDelegate(TuioCursor tcur, int action);
        public void addStroke(TuioCursor tcur, int action)
        {
            if (action == 0)
            {
                NewCursor nc = new NewCursor();
                StylusPointCollection spc = new StylusPointCollection();
                StylusPoint sp = new StylusPoint(tcur.getX() * 1280, tcur.getY() * 800, (float)0.5);
                spc.Add(sp);
                Stroke newStroke = new Stroke(spc, inkCanv.DefaultDrawingAttributes.Clone());
                //绘画过程中,先不让Stroke自动平滑
                newStroke.DrawingAttributes.FitToCurve = false;
                inkCanv.Strokes.Add(newStroke);
                nc.spc = spc;
                nc.stroke = newStroke;
                spcList.Add(tcur.getSessionID(), nc);
            }
            else if (action == 1)
            {
                StylusPointCollection spc = spcList[tcur.getSessionID()].spc;
                StylusPoint sp = new StylusPoint(tcur.getX() * 1280, tcur.getY() * 800, (float)0.5);
                spc.Add(sp);
                Trace.Write("add stylus point:" + tcur.getX().ToString() + "," + tcur.getY().ToString() + "\n");
            }
            else if (action == 2)
            {
                StylusPointCollection spc = spcList[tcur.getSessionID()].spc;
                StylusPoint sp = new StylusPoint(tcur.getX() * 1280, tcur.getY() * 800, (float)0.5);
                spc.Add(sp);
 
                //画完以后,设置FitToCurve让这个Stroke平滑。
                spcList[tcur.getSessionID()].stroke.DrawingAttributes.FitToCurve = true;
                spcList.Remove(tcur.getSessionID());
                Trace.Write("add stylus point:" + tcur.getX().ToString() + "," + tcur.getY().ToString() + "\n");
            }
          
        }
 
       //接收Cursor Press 的事件接口函数
        public void addTuioCursor(TuioCursor tcur)
        {
            try
            {
                inkCanv.Dispatcher.Invoke(DispatcherPriority.Normal, new updateStrokeDelegate(addStroke), tcur, 0);
            }
            catch (Exception e)
            {
                Trace.Write("exception in addCursor\n");
                Trace.Write(e.Message);
            }
            Trace.Write("cursor add\n");
        }
 
        //接收Cursor move的事件接口
        public void updateTuioCursor(TuioCursor tcur)
        {
            try
            {
                inkCanv.Dispatcher.Invoke(DispatcherPriority.Normal, new updateStrokeDelegate(addStroke), tcur, 1);
            }
            catch (Exception e)
            {
                Trace.Write(e.Message);
            }
            Trace.Write("cursor update\n");
        }
 
        //接收Cursor Release的事件接口
        public void removeTuioCursor(TuioCursor tcur)
        {
            try
            {
                inkCanv.Dispatcher.Invoke(DispatcherPriority.Normal, new updateStrokeDelegate(addStroke), tcur, 2);
            }
            catch (Exception e)
            {
                Trace.Write(e.Message);
            }
            Trace.Write("cursor remove\n");
        }
}

你可能感兴趣的:(职场,休闲)