Exemplo n.º 1
0
        public override void OnMouseUp(Palette palette, MouseEventArgs e)
        {
            if (isNewObjectAdded == false)
            {
                return;
            }
            base.OnMouseUp(palette, e);
            int         index = CC.myService.FindObjectIndex(CC.ID);
            DrawMyImage w     = (DrawMyImage)palette.graphics[index];

            if (CC.userState != UserState.SingleUser)
            {
                GraphicsList myGraphicsList = new GraphicsList();
                myGraphicsList.Add(w.Clone());
                //序列化
                try
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        IFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, myGraphicsList);
                        byte[] bytes = stream.GetBuffer();
                        //层号,序列化后的字节数
                        CC.me.SendToServer(string.Format("DrawMyImage,{0}", bytes.Length));
                        CC.me.SendToServer(bytes);
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "序列化失败");
                }
                palette.graphics.Remove(CC.ID);
            }
        }
Exemplo n.º 2
0
 /// <summary>将Graphics序列化到fileName中</summary>
 public void SerializeObject(GraphicsList serializedGraphics, string fileName)
 {
     try
     {
         using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
         {
             IFormatter formatter = new BinaryFormatter();
             formatter.Serialize(stream, serializedGraphics);
         }
     }
     catch (Exception err)
     {
         MessageBox.Show("保存文件失败,原因:" + err.Message);
     }
 }
Exemplo n.º 3
0
        private void ReceiveData()
        {
            while (normalExit == false)
            {
                string receiveString = null;
                try
                {
                    receiveString = sr.ReadLine();
                }
                catch
                {
                    Debug.Print("MyClient:接收receiveString数据失败");
                    break;
                }
                if (receiveString == null)
                {
                    if (normalExit == false)
                    {
                        MessageBox.Show("与主机失去联系,无法继续制作!");
                    }
                    break;
                }
                Debug.Print("附机收到:{0}", receiveString);
                string[] splitString = receiveString.Split(',');
                switch (splitString[0])
                {
                case "ID":
                    CC.ID          = int.Parse(splitString[1]);
                    finishGetNewID = true;
                    break;

                case "ServerExit":
                    if (CC.userState == UserState.Client)
                    {
                        MessageBox.Show("主机正常退出制作,由于本机为附机,无法继续!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    normalExit = true;
                    break;

                case "WelcomeLogin":
                {
                    //格式:WelcomeLogin,字节数
                    byte[] bytes = ReceiveBytesFromServer(int.Parse(splitString[1]));
                    try
                    {
                        MemoryStream stream    = new MemoryStream(bytes);
                        IFormatter   formatter = new BinaryFormatter();
                        CC.palette.graphics = (GraphicsList)formatter.Deserialize(stream);
                        stream.Close();
                        CC.myService.RefreshPalette();
                    }
                    catch (Exception err)
                    {
                        Debug.Print("WelcomeLogin反序列化失败,原因: {0}", err);
                        break;
                    }
                }
                break;

                case "Logout":
                {
                    if (splitString[1] == CC.me.client.Client.LocalEndPoint.ToString())
                    {
                        normalExit = true;
                        break;
                    }
                    if (splitString[1] == CC.me.client.Client.RemoteEndPoint.ToString())
                    {
                        MessageBox.Show("\n主机正常退出制作,由于本机为附机,无法继续!\n\n请保存文件,立即退出", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        Debug.Print(splitString[1] + " 退出制作(非主机),本机可以继续制作!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                break;

                case "DrawMyImage":
                {
                    //格式:DrawMyImage,序列化后的字节数
                    int    count = int.Parse(splitString[1]);
                    byte[] bytes = ReceiveBytesFromServer(count);
                    try
                    {
                        MemoryStream stream         = new MemoryStream(bytes);
                        IFormatter   formatter      = new BinaryFormatter();
                        GraphicsList myGraphicsList = (GraphicsList)formatter.Deserialize(stream);
                        stream.Close();
                        DrawMyImage w = (DrawMyImage)myGraphicsList[0];
                        w.Selected = false;
                        CC.palette.graphics.Add(w);
                        myGraphicsList.Clear();
                        CC.myService.RefreshPalette();
                    }
                    catch (Exception err)
                    {
                        Debug.Print("反序列化图片失败,原因: {0}", err);
                    }
                }
                break;

                case "DeleteObjects":
                {
                    string[] str = splitString[1].Split('@');
                    for (int i = 0; i < str.Length; i++)
                    {
                        CC.palette.graphics.Remove(int.Parse(str[i]));
                    }
                    CC.myService.RefreshPalette();
                }
                break;

                default:
                    CC.myService.DataProcessing(receiveString);
                    break;
                }
            }
        }