public MainWindow()
        {
            this.Loaded += MainWindow_Loaded;

            InitializeComponent();

            mainCanvas.Width = Screen.PrimaryScreen.Bounds.Width;
            mainCanvas.Height = Screen.PrimaryScreen.Bounds.Height;

            BitmapImage bi0 = new BitmapImage();
            bi0.BeginInit();
            bi0.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor0.png");
            bi0.EndInit();

            BitmapImage bi1 = new BitmapImage();
            bi1.BeginInit();
            bi1.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor1.png");
            bi1.EndInit();

            cursorImageIndex.Source = bi0;
            cursorImageIndex.Width = cursorImageIndex.Height = 100;

            cursorImageThumb.Source = bi0;
            cursorImageThumb.Width = cursorImageThumb.Height = 100;

            cursorImageIndex.Opacity = 0;
            cursorImageThumb.Opacity = 0;

            IPC ipc = new IPC("win_cursor_plus");

            ipc.MapFunction("exit", delegate(string messageBody)
            {
                Environment.Exit(0);
                return 1;
            });

            ipc.SetUDPCallback(delegate(string message)
            {
                if (message != "hide_cursor_index" || message != "hide_cursor_thumb")
                {
                    string[] xyStr = message.Split('!');

                    if (xyStr.Length == 5)
                    {
                        cursorIndexDown = false;
                        cursorThumbDown = false;

                        float x;
                        float y;
                        float z;
                        int down;

                        bool b0 = float.TryParse(xyStr[0], out x);
                        bool b1 = float.TryParse(xyStr[1], out y);
                        bool b2 = float.TryParse(xyStr[2], out z);
                        bool b3 = int.TryParse(xyStr[3], out down);

                        if (b0 && b1 && b2)
                            if (xyStr[4] == "index")
                            {
                                xCursorIndex = x * screenWidth / 1000;
                                yCursorIndex = y * screenHeight / 1000;
                                zCursorIndex = z;
                                showCursorIndex = true;
                                cursorIndexDown = down == 1;
                            }
                            else if (xyStr[4] == "thumb")
                            {
                                xCursorThumb = x * screenWidth / 1000;
                                yCursorThumb = y * screenHeight / 1000;
                                zCursorThumb = z;
                                showCursorThumb = true;
                                cursorIndexDown = true;
                                cursorThumbDown = true;
                            }
                    }
                    else if (message == "hide_cursor_index")
                        showCursorIndex = false;
                    else if (message == "hide_cursor_thumb")
                        showCursorThumb = false;
                    else if (xyStr.Length == 2 && xyStr[0] == "update")
                        updateNumNew = int.Parse(xyStr[1]);
                }

                return 1;
            });

            Timer timer = new Timer();
            timer.Interval = 20;
            timer.Tick += delegate(object o, EventArgs e)
            {
                ipc.Update();

                if (updateNumNew == updateNumOld)
                    return;

                updateNumOld = updateNumNew;

                if (!useTUIO && showCursorIndex)
                {
                    System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCursorIndex, (int)yCursorIndex);

                    if (cursorIndexDown)
                        mouseDown();
                    else
                        mouseUp();
                }
                else if (showCursorIndex)
                {
                    Object[] tCur0 = new Object[7];
                    Object[] tCur1 = new Object[7];
                    OSCBundle bundle = new OSCBundle();
                    bundle.Append(TUIO.TUIOFseq(tuioFSeq));
                    ArrayList TUIOSessions = new ArrayList();

                    if (cursorIndexDown)
                    {
                        bundle.Append(TUIO.TUIO2DcurExt(0, xCursorIndex / screenWidth, yCursorIndex / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(0);
                    }
                    if (cursorThumbDown) {
                        bundle.Append(TUIO.TUIO2DcurExt(1, xCursorThumb / screenWidth, yCursorThumb / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(1);
                    }
                    bundle.Append(TUIO.TUIOAlive(TUIOSessions));
                    transmitter.Send(bundle);
                    tuioFSeq++;
                }

                if (showCursorIndex)
                {
                    if (cursorIndexDown)
                        cursorImageIndex.Source = bi1;
                    else
                        cursorImageIndex.Source = bi0;

                    if (cursorImageIndex.Opacity < 1)
                        cursorImageIndex.Opacity += 0.2;

                    int cursorSize = (int)(zCursorIndex * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 200)
                        cursorSize = 200;
                    else if (cursorSize < 50)
                        cursorSize = 50;

                    cursorImageIndex.Width = cursorImageIndex.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorIndex, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorIndex, 0, screenHeight, 0, windowHeight);

                    Canvas.SetLeft(cursorImageIndex, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageIndex, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageIndex.Opacity > 0)
                    cursorImageIndex.Opacity -= 0.2;

                if (showCursorThumb)
                {
                    if (cursorThumbDown)
                        cursorImageThumb.Source = bi1;
                    else
                        cursorImageThumb.Source = bi0;

                    if (cursorImageThumb.Opacity < 1)
                        cursorImageThumb.Opacity += 0.2;

                    int cursorSize = (int)(zCursorThumb * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 200)
                        cursorSize = 200;
                    else if (cursorSize < 50)
                        cursorSize = 50;

                    cursorImageThumb.Width = cursorImageThumb.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorThumb, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorThumb, 0, screenHeight, 0, windowHeight);

                    Canvas.SetLeft(cursorImageThumb, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageThumb, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageThumb.Opacity > 0)
                    cursorImageThumb.Opacity -= 0.2;

                cursorIndexDownOld = cursorIndexDown;
                cursorThumbDownOld = cursorThumbDown;

                xCursorThumbOld = xCursorThumb;
                yCursorThumbOld = yCursorThumb;
                zCursorThumbOld = zCursorThumb;

                xCursorIndexOld = xCursorIndex;
                yCursorIndexOld = yCursorIndex;
                zCursorIndexOld = zCursorIndex;
            };

            timer.Start();
        }
        public MainWindow()
        {
            this.Loaded += MainWindow_Loaded;

            InitializeComponent();

            mainCanvas.Width  = Screen.PrimaryScreen.Bounds.Width;
            mainCanvas.Height = Screen.PrimaryScreen.Bounds.Height;

            BitmapImage bi0 = new BitmapImage();

            bi0.BeginInit();
            bi0.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor0.png");
            bi0.EndInit();

            BitmapImage bi1 = new BitmapImage();

            bi1.BeginInit();
            bi1.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor1.png");
            bi1.EndInit();

            cursorImageIndex.Source = bi0;
            cursorImageIndex.Width  = cursorImageIndex.Height = 100;

            cursorImageThumb.Source = bi0;
            cursorImageThumb.Width  = cursorImageThumb.Height = 100;

            cursorImageIndex.Opacity = 0;
            cursorImageThumb.Opacity = 0;

            IPC ipc = new IPC("win_cursor_plus");

            ipc.MapFunction("exit", delegate(string messageBody)
            {
                Environment.Exit(0);
                return(1);
            });

            ipc.SetUDPCallback(delegate(string message)
            {
                if (message != "hide_cursor_index" || message != "hide_cursor_thumb")
                {
                    string[] xyStr = message.Split('!');

                    if (xyStr.Length == 5)
                    {
                        cursorIndexDown = false;
                        cursorThumbDown = false;

                        float x;
                        float y;
                        float z;
                        int down;

                        bool b0 = float.TryParse(xyStr[0], out x);
                        bool b1 = float.TryParse(xyStr[1], out y);
                        bool b2 = float.TryParse(xyStr[2], out z);
                        bool b3 = int.TryParse(xyStr[3], out down);

                        if (b0 && b1 && b2)
                        {
                            if (xyStr[4] == "index")
                            {
                                xCursorIndex    = x * screenWidth / 1000;
                                yCursorIndex    = y * screenHeight / 1000;
                                zCursorIndex    = z;
                                showCursorIndex = true;
                                cursorIndexDown = down == 1;
                            }
                            else if (xyStr[4] == "thumb")
                            {
                                xCursorThumb    = x * screenWidth / 1000;
                                yCursorThumb    = y * screenHeight / 1000;
                                zCursorThumb    = z;
                                showCursorThumb = true;
                                cursorIndexDown = true;
                                cursorThumbDown = true;
                            }
                        }
                    }
                    else if (message == "hide_cursor_index")
                    {
                        showCursorIndex = false;
                    }
                    else if (message == "hide_cursor_thumb")
                    {
                        showCursorThumb = false;
                    }
                    else if (xyStr.Length == 2 && xyStr[0] == "update")
                    {
                        updateNumNew = int.Parse(xyStr[1]);
                    }
                }

                return(1);
            });

            Timer timer = new Timer();

            timer.Interval = 20;
            timer.Tick    += delegate(object o, EventArgs e)
            {
                ipc.Update();

                if (updateNumNew == updateNumOld)
                {
                    return;
                }

                updateNumOld = updateNumNew;

                if (!useTUIO && showCursorIndex)
                {
                    System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCursorIndex, (int)yCursorIndex);

                    if (cursorIndexDown)
                    {
                        mouseDown();
                    }
                    else
                    {
                        mouseUp();
                    }
                }
                else if (showCursorIndex)
                {
                    Object[]  tCur0  = new Object[7];
                    Object[]  tCur1  = new Object[7];
                    OSCBundle bundle = new OSCBundle();
                    bundle.Append(TUIO.TUIOFseq(tuioFSeq));
                    ArrayList TUIOSessions = new ArrayList();

                    if (cursorIndexDown)
                    {
                        bundle.Append(TUIO.TUIO2DcurExt(0, xCursorIndex / screenWidth, yCursorIndex / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(0);
                    }
                    if (cursorThumbDown)
                    {
                        bundle.Append(TUIO.TUIO2DcurExt(1, xCursorThumb / screenWidth, yCursorThumb / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(1);
                    }
                    bundle.Append(TUIO.TUIOAlive(TUIOSessions));
                    transmitter.Send(bundle);
                    tuioFSeq++;
                }

                if (showCursorIndex)
                {
                    if (cursorIndexDown)
                    {
                        cursorImageIndex.Source = bi1;
                    }
                    else
                    {
                        cursorImageIndex.Source = bi0;
                    }

                    if (cursorImageIndex.Opacity < 1)
                    {
                        cursorImageIndex.Opacity += 0.2;
                    }

                    int cursorSize = (int)(zCursorIndex * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 200)
                    {
                        cursorSize = 200;
                    }
                    else if (cursorSize < 50)
                    {
                        cursorSize = 50;
                    }

                    cursorImageIndex.Width = cursorImageIndex.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorIndex, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorIndex, 0, screenHeight, 0, windowHeight);

                    Canvas.SetLeft(cursorImageIndex, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageIndex, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageIndex.Opacity > 0)
                {
                    cursorImageIndex.Opacity -= 0.2;
                }

                if (showCursorThumb)
                {
                    if (cursorThumbDown)
                    {
                        cursorImageThumb.Source = bi1;
                    }
                    else
                    {
                        cursorImageThumb.Source = bi0;
                    }

                    if (cursorImageThumb.Opacity < 1)
                    {
                        cursorImageThumb.Opacity += 0.2;
                    }

                    int cursorSize = (int)(zCursorThumb * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 200)
                    {
                        cursorSize = 200;
                    }
                    else if (cursorSize < 50)
                    {
                        cursorSize = 50;
                    }

                    cursorImageThumb.Width = cursorImageThumb.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorThumb, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorThumb, 0, screenHeight, 0, windowHeight);

                    Canvas.SetLeft(cursorImageThumb, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageThumb, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageThumb.Opacity > 0)
                {
                    cursorImageThumb.Opacity -= 0.2;
                }

                cursorIndexDownOld = cursorIndexDown;
                cursorThumbDownOld = cursorThumbDown;

                xCursorThumbOld = xCursorThumb;
                yCursorThumbOld = yCursorThumb;
                zCursorThumbOld = zCursorThumb;

                xCursorIndexOld = xCursorIndex;
                yCursorIndexOld = yCursorIndex;
                zCursorIndexOld = zCursorIndex;
            };

            timer.Start();
        }