PageRenderTime 32ms CodeModel.GetById 8ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSSL3LocalMessage/MainPage.xaml.cs

#
C# | 159 lines | 111 code | 17 blank | 31 comment | 5 complexity | 3dbbf4582bdaee69ea84b812686db02c MD5 | raw file
  1/****************************** Module Header ******************************\
  2* Module Name:                 MainPage.xaml.cs
  3* Project:                     CSSL3LocalMessage
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* Local message whiteboard's code behind file.
  7* 
  8* This source is subject to the Microsoft Public License.
  9* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 10* All other rights reserved.
 11* 
 12* History:
 13* * 9/22/2009 6:34 PM Mog Liang Created
 14\***************************************************************************/
 15
 16using System;
 17using System.Collections.Generic;
 18using System.Linq;
 19using System.Net;
 20using System.Windows;
 21using System.Windows.Controls;
 22using System.Windows.Documents;
 23using System.Windows.Input;
 24using System.Windows.Media;
 25using System.Windows.Media.Animation;
 26using System.Windows.Shapes;
 27using System.Windows.Messaging;
 28using System.Runtime.Serialization.Json;
 29using System.IO;
 30using System.Windows.Ink;
 31
 32namespace CSSL3LocalMessage
 33{
 34    public partial class MainPage : UserControl
 35    {
 36        // Preassign two names for LocalMessageReceiver.
 37        readonly string[] clientnames = { "{36BF6178-53A3-4d0b-8E0D-1E7BD9FBBE62}", "{E62BF131-B5EA-4f1d-B80D-DC083C7F0851}" };
 38        // Preassign two colors for distinguishing two applications.
 39        readonly Color[] colors = { Colors.Red, Colors.Green };
 40        int clientid;
 41
 42        LocalMessageReceiver _localreceiver;
 43        LocalMessageSender _localsender;
 44        DataContractJsonSerializer _jsonserializer;
 45
 46        public MainPage()
 47        {
 48            InitializeComponent();
 49
 50            _jsonserializer =new DataContractJsonSerializer(inkP.Strokes.GetType());
 51
 52            // First try.
 53            // Create receiver with one preset name.
 54            _localreceiver = new LocalMessageReceiver(clientnames[clientid]);
 55            _localreceiver.MessageReceived += localreceiver_MessageReceived;
 56            try
 57            {
 58                _localreceiver.Listen();
 59            }
 60            catch(Exception e)
 61            {
 62                // Second try.
 63                // Create receiver with another preset name.
 64                _localreceiver.MessageReceived -= localreceiver_MessageReceived;
 65                clientid = 1;
 66                _localreceiver = new LocalMessageReceiver(clientnames[clientid]);
 67                _localreceiver.MessageReceived += localreceiver_MessageReceived;
 68                try
 69                {
 70                    _localreceiver.Listen();
 71                }
 72                catch (Exception e1)
 73                {
 74                    // Already has two apllication instance.
 75                    // Pop messagebox, disable drawing area.
 76                    MessageBox.Show("Only allow 2 clients.");
 77                    disablegrid.Visibility = Visibility.Visible;
 78                    return;
 79                }
 80            }
 81
 82            // Create sender targeting to another application's receiver
 83            _localsender = new LocalMessageSender(clientnames[(clientid + 1) % 2]);
 84            _localsender.SendCompleted += localsender_SendCompleted;
 85
 86            drawbr.Background = new SolidColorBrush(colors[clientid]);
 87        }
 88
 89        void localsender_SendCompleted(object sender, SendCompletedEventArgs e)
 90        {
 91            // If send message got error, pop messagebox.
 92            if(e.Error!=null)
 93                MessageBox.Show("Cannot connect to another client.");
 94        }
 95
 96        void localreceiver_MessageReceived(object sender, MessageReceivedEventArgs e)
 97        {
 98            // Deserialize json string to stroke object.
 99            var stream = new MemoryStream();
100            var streamwriter = new StreamWriter(stream);
101            streamwriter.Write(e.Message);
102            streamwriter.Flush();
103            var receivedstroke = _jsonserializer.ReadObject(stream) as Stroke;
104            stream.Close();
105
106            // Add received stroke to inkPresenter.
107            inkP.Strokes.Add(receivedstroke);
108        }
109
110        // Attaching MouseLeftButtonDown/MouseMove/MouseLeftButtonUp event
111        // to implement drawing functionality.
112        Stroke _newStroke;
113        private void InkPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
114        {
115            inkP.CaptureMouse();
116            _newStroke = new System.Windows.Ink.Stroke();
117            _newStroke.DrawingAttributes.Color = colors[clientid];
118            _newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkP));
119            inkP.Strokes.Add(_newStroke);
120        }
121
122        // Sending message when finishing one stroke.
123        private void InkPresenter_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
124        {
125            inkP.ReleaseMouseCapture();
126
127            // Serialize stroke object to string.
128            var stream = new MemoryStream();
129            _jsonserializer.WriteObject(stream, _newStroke);
130            stream.Flush();
131            stream.Position = 0;
132            var obstring = new StreamReader(stream).ReadToEnd();
133            stream.Close();
134
135            // Send Serialized stroke.
136            if (_localsender != null)
137            {
138                try
139                {
140                    _localsender.SendAsync(obstring);
141                }
142                catch (Exception ex)
143                {
144                    MessageBox.Show(ex.Message);
145                }
146            }
147
148            _newStroke = null;
149        }
150
151        private void InkPresenter_MouseMove(object sender, MouseEventArgs e)
152        {
153            if (_newStroke != null)
154            {
155                _newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkP));
156            }
157        }
158    }
159}