コード例 #1
0
        /// <summary>
        /// Hàm load file
        /// </summary>
        /// <param name="name">Đường dẫn tới file</param>
        public void LoadProject(string name)
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<PaintState>));
            FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
            List<PaintState> temp = new List<PaintState>();

            temp = (List<PaintState>)ser.Deserialize(fs);
            fs.Close();

            foreach (var item in temp)
            {
                item.CurrentColor = Color.FromArgb(item.R1, item.G1, item.B1);
                item.BrushColor = Color.FromArgb(item.RB1, item.GB1, item.BB1);
                MyShape a;
                switch (item.ShapeType1)
                {
                    case ShapeType.MyLine:
                        a = new MyLine(item);
                        break;
                    case ShapeType.MyRectangle:
                        a = new MyRectangle(item);
                        break;
                    case ShapeType.MyEllipse:
                        a = new MyEllipse(item);
                        break;
                    case ShapeType.MyTriangle:
                        a = new MyTriangle(item);
                        break;
                    case ShapeType.MyText:
                        a = new MyText(item);
                        break;
                    case ShapeType.MyFill:
                        {
                            a = new MyFill(item);
                            a.State.ColorFill = Color.FromArgb(a.State.RF1, a.State.GF1, a.State.BF1);
                            PnlPaint.floodFill(a.State.PointFill, a.State.ColorFill);
                            break;
                        }
                    case ShapeType.MyEraser:
                        {
                            a = new MyEraser(item);
                            break;
                        }
                    default:
                        a = new MyLine(item);
                        break;
                }

                byte[] buf = item.ToByteArray();
                for (int i = 0; i < PnlPaint._clientSocket.Count; i++)
                {
                    PnlPaint._clientSocket[i].Send(buf);
                }
                //Gởi nhanh quá người nhận không kịp
                Thread.Sleep(100);

                PnlPaint.MyData.Add(a);
                PnlPaint._isRemoveBorder = true;
                PnlPaint._isStillCanMove = PnlPaint._isStillCanReSize = false;
                PnlPaint.Invalidate();
            }
        }
コード例 #2
0
        /// <summary>
        /// Hàm nhận hình từ Server gởi đến 
        /// </summary>
        /// <param name="person"></param>
        private void AddListShape(PaintState person)
        {
            if (person.FirstPoint.X > 0 && person.FirstPoint.Y > 0)
            {
                person.CurrentColor = Color.FromArgb(person.R1, person.G1, person.B1);
                MyShape a;
                switch (person.ShapeType1)
                {
                    case ShapeType.MyLine:
                        a = new MyLine(person);
                        break;
                    case ShapeType.MyRectangle:
                        a = new MyRectangle(person);
                        break;
                    case ShapeType.MyEllipse:
                        a = new MyEllipse(person);
                        break;
                    case ShapeType.MyTriangle:
                        a = new MyTriangle(person);
                        break;
                    case ShapeType.MyText:
                        a = new MyText(person);
                        break;
                    case ShapeType.MyEraser:
                        a = new MyEraser(person);
                        break;
                    case ShapeType.MyFill:
                        {

                            if (_myData.Count() > 0 && !_isRemoveBorder)
                            {
                                _myData.RemoveAt(_myData.Count() - 1);
                            }
                            if (_myData.Count() > 0 && _isStillCanMove && _isStillCanReSize)
                            {
                                _myData.RemoveAt(_myData.Count() - 1);
                                _isStillCanReSize = _isStillCanMove = false;
                            }

                            a = new MyFill(person);
                            floodFill(a.State.PointFill, a.State.ColorFill);

                            break;
                        }
                    default:
                        a = new MyLine(person);
                        break;
                }
                if (person.ShapeType1 == ShapeType.MyFill)
                {
                    _myData.Add(a);
                    _isRemoveBorder = true;
                    _isStillCanMove = _isStillCanReSize = false;
                    
                }
                else
                {
                    if (_myData.Count() > 1 && _isStillCanMove == true && _isStillCanReSize == true)
                    {
                        _myData.Insert(_myData.Count() - 2, a);
                    }
                    else
                    {
                        _myData.Add(a);
                        _isRemoveBorder = true;
                        _isStillCanMove = _isStillCanReSize = false;
                     }
                }
                this.Invalidate();
            }

        }
コード例 #3
0
        private void PaintPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (_myData.Count() == 0)
            {
                undo.Clear();
            }
  
            if (!_isFill)
            {
                f = new Point();
                myPaint.FirstPoint = myPaint.SecondPoint = e.Location;
                
                /////

                //
                if (myPaint.ShapeType1 != ShapeType.MyEraser)
                {
                    myPaint.TextLocation = e.Location;
                    //Xác định bao khung để xem thử có đang ở chế độ Resize không

                    Point Start = new Point();
                    Point End = new Point();
                    if (_myData.Count() > 0)
                    {
                        MyShape temp = _myData[_myData.Count() - 1];
                        temp.findBourder(ref Start, ref End);
                    }

                    // Bỏ hình viền bao quanh hình vừa mới vẽ
                    int n = _myData.Count();
                    if (n > 0 && _myData[n - 1].State.ShapeType1 != ShapeType.MyText && !_isRemoveBorder)
                    {
                        _myData.RemoveAt(n - 1);
                        n--;
                        _isRemoveBorder = true;
                      
                    }

                    //Giữ lại điểm đầu của hình cho chức năng resize đường thẳng
                    if (n > 0)
                    {
                        f = new Point(_myData[n - 1].State.SecondPoint.X, _myData[n - 1].State.SecondPoint.Y);
                    }


                    bool flag = false;
                    if (n > 0 && _myData[_myData.Count() - 1].State.ShapeType1 != ShapeType.MyEraser && _isStillCanReSize)
                    {
                        flag = findReSizePoint(e.Location, Start, End, _myData[_myData.Count() - 1].State.FirstPoint, _myData[_myData.Count() - 1].State.SecondPoint);
                    }
                    // Nếu không ở chế độ resize
                    if (!flag)
                    {

                        _isReSizePoint = new bool[8] { false, false, false, false, false, false, false, false };
                        _isReSize = false;

                        if (n > 0 && _myData[_myData.Count() - 1].State.ShapeType1 == ShapeType.MyText
                            && textBox.Text != "")
                        {
                            _myData[_myData.Count() - 1].State.Text = textBox.Text;
                            _myData[_myData.Count() - 1].State.TextLocation = textBox.Location;
                            _myData[_myData.Count() - 1].State.TextSize = (int) textBox.Font.Size;
                            _myData[_myData.Count() - 1].State.TextFont = textBox.Font.Name.ToString();
                            _myData[_myData.Count() - 1].State.CurrentColor = textBox.ForeColor;
                            this.Invalidate();
                        }

                        // Xóa đi các textbox
                        this.Controls.Clear();
                        textBox = new TextBox();

                        if (_isStillCanMove)
                        {
                            //Nếu không phải là đường thẳng thì xét điểm click nằm trong bao khung
                            if (n > 0 && _myData[n - 1].State.ShapeType1 == ShapeType.MyLine)
                            {
                                if (_myData[n - 1].calcDistanceLine(e.Location) < 5)
                                {
                                    _isCanMove = true;
                                    
                                }
                                else
                                {
                                    _isCanMove = false;
                                }

                            }
                            //Nếu là đường thẳng thì xét khoảng cách từ điểm click đến đường thẳng đó nhỏ hơn < Epxelon ( 5 )
                            else
                            {
                                if (n > 0 && _myData[n - 1].State.ShapeType1 != ShapeType.MyEraser && _myData[n - 1].checkPointInside(e.Location))// Nếu đang nằm trong vùng của hình mới vẽ thì kích hoạt trạng thái di chuyển
                                {
                                    _isCanMove = true;
                                }
                            }

                            if (_isCanMove)
                            {
                                int dentaX = myPaint.SecondPoint.X - myPaint.FirstPoint.X;
                                int dentaY = myPaint.SecondPoint.Y - myPaint.FirstPoint.Y;

                                PaintState temp1 = new PaintState(myPaint);
                                MyShape a;
                                switch (myPaint.ShapeType1)
                                {
                                    case ShapeType.MyLine:
                                        a = new MyLine(temp1);
                                        break;
                                    case ShapeType.MyRectangle:
                                        a = new MyRectangle(temp1);
                                        break;
                                    case ShapeType.MyEllipse:
                                        a = new MyEllipse(temp1);
                                        break;
                                    case ShapeType.MyTriangle:
                                        a = new MyTriangle(temp1);
                                        break;
                                    default:
                                        a = new MyLine(temp1);
                                        break;
                                }

                                a.State.FirstPoint = new Point(_myData[n - 1].State.FirstPoint.X + dentaX, _myData[n - 1].State.FirstPoint.Y + dentaY);
                                a.State.SecondPoint = new Point(_myData[n - 1].State.SecondPoint.X + dentaX, _myData[n - 1].State.SecondPoint.Y + dentaY);
                                a.State.Shift1 = _myData[n - 1].State.Shift1;
                                _shapeCurrent = _myData[n - 1];
                                _myData[n - 1] = a;

                                this.Invalidate();
                            }


                           
                        }
                        // Nếu chưa có hình nào hoặc nằm ngoài hình vừa mới vẽ hoặc trạng thái có thể di chuyển bị tắt hoặc (hình trước đó là đường thẳng và không được bật trạng thái di chuyển ) thì tắt trạng thái di chuyển và bắt đầu thêm hình mới vào danh sách các hình
                        if (n == 0 || (n > 0 && !_myData[n - 1].checkPointInside(e.Location)) || (n > 0 && _myData[n - 1].checkPointInside(e.Location) && _myData[n - 1].State.ShapeType1 == ShapeType.MyLine && !_isCanMove) || !_isStillCanMove)
                        {
                            _isCanMove = false;

                            if (n > 0)
                            {
                                MyShape temp2 = _myData[_myData.Count() - 1];
                                byte[] buffer = temp2.State.ToByteArray();
                                if (_isClient && client != null && client.Connected)
                                {
                                    try
                                    {

                                        client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallbackClient), null);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                }

                                if (_isServer) //Nếu đang với vai trò là sever thì gởi hình trên sever về tất cả người kết nối
                                {

                                    foreach (var item in _clientSocket)
                                    {
                                        item.Send(buffer);
                                    }

                                }
                            }
                            //Hình đầu tiên của các client trước ( n - 1 ) bị gới lên server bị mất nên phải gởi lại hình đầu tiên
                            if (n == 1)
                            {
                                MyShape temp3 = _myData[_myData.Count() - 1];
                                byte[] buffer = temp3.State.ToByteArray();
                                if (_isClient && client != null && client.Connected)
                                {
                                    try
                                    {

                                        client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallbackClient), null);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                }

                                if (_isServer) //Nếu đang với vai trò là sever thì gởi hình trên sever về tất cả người kết nối
                                {

                                    foreach (var item in _clientSocket)
                                    {
                                        item.Send(buffer);
                                    }

                                }
                            }
                            PaintState t = new PaintState(myPaint);

                            MyShape a;
                            switch (myPaint.ShapeType1)
                            {
                                case ShapeType.MyLine:
                                    a = new MyLine(t);
                                    break;
                                case ShapeType.MyRectangle:
                                    a = new MyRectangle(t);
                                    break;
                                case ShapeType.MyEllipse:
                                    a = new MyEllipse(t);
                                    break;
                                case ShapeType.MyTriangle:
                                    a = new MyTriangle(t);
                                    break;
                                case ShapeType.MyText:                        
                                    a = new MyText(t);
                                    break;                                
                                default:
                                    a = new MyLine(t);
                                    break;
                            }

                            _myData.Add(a);

                            // Nếu đang ở trạng thái text thì thêm 1 khung hình 
                            if (myPaint.ShapeType1 == ShapeType.MyText)
                            {
                                MyRectangle x = new MyRectangle (myPaint);
                                x.State.CurrentColor = myPaint.CurrentColor;
                                x.State.LineWidth = 1;
                                x.State.NameBrush = "NoFill";
                                x.State.IsBrushFill = false;
                                _myData.Add(x);
                            }

                        }
                    }
                }
                else// Đang ở trạng thái xóa
                {
                    // Nếu trước đó đang ở trạng thái text thì lấy nội dung của text và xóa textbox đi
                    if (_myData.Count() > 0 && _myData[_myData.Count() - 1].State.ShapeType1 == ShapeType.MyText)
                    {
                        _myData[_myData.Count() - 1].State.Text = textBox.Text;
                        this.Invalidate();
                    }
                    // Xóa đi các textbox
                    this.Controls.Clear();
                    textBox = new TextBox();
                    //
                    // Nếu trước đó là một hình nào đó mà không phải là text hay border thì chắc chắn có bao khung --> xóa bao khung đi
                    if (_myData.Count() > 0 && _myData[_myData.Count() - 1].State.ShapeType1 != ShapeType.MyText && _myData[_myData.Count() - 1].State.ShapeType1 != ShapeType.MyEraser)
                    {
                        _myData.RemoveAt(_myData.Count() - 1);
                    }
                    // Gởi hình vừa vẽ trước đó
                    if (_myData.Count() > 0)
                    {
                        MyShape temp = _myData[_myData.Count() - 1];
                        byte[] buffer = temp.State.ToByteArray();
                        if (_isClient && client != null && client.Connected)
                        {
                            try
                            {

                                client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallbackClient), null);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }

                        if (_isServer) //Nếu đang với vai trò là sever thì gởi hình trên sever về tất cả người kết nối
                        {

                            foreach (var item in _clientSocket)
                            {
                                item.Send(buffer);
                            }

                        }
                    }
                    PaintState statetemp = new PaintState(myPaint);
                    MyShape a = new MyEraser(statetemp);
                    a.State.EraserPoint.Add(myPaint.FirstPoint);
                    _myData.Add(a);
                    this.Invalidate();
                }
              //
            }
            else //Trang thai tô
            {
                myPaint.FirstPoint = myPaint.SecondPoint = e.Location;

                // Nếu như đang network thì gởi hình vẽ trước đó đi
                if (_myData.Count() > 0)
                {
                    MyShape shapetemp = _myData[_myData.Count() - 1];
                    byte[] buffertemp = shapetemp.State.ToByteArray();
                    if (_isClient && client != null && client.Connected)
                    {
                        try
                        {
                            client.BeginSend(buffertemp, 0, buffertemp.Length, SocketFlags.None, new AsyncCallback(SendCallbackClient), null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    if (_isServer) //Nếu đang với vai trò là sever thì gởi hình trên sever về tất cả người kết nối
                    {

                        foreach (var item in _clientSocket)
                        {
                            item.Send(buffertemp);
                        }

                    }

                }           

                floodFill(e.Location,_fillColor);

                //Lưu dưới dạng đối tượng
                myPaint.PointFill = e.Location;
                myPaint.RF1 = _fillColor.R;
                myPaint.GF1 = _fillColor.G;
                myPaint.BF1 = _fillColor.B;
                PaintState t3 = new PaintState(myPaint);
                MyShape t1 = new MyFill(t3);
            
                _myData.Add(t1);
                ///

                //Gởi trạng thái tô

                byte[] buffer1 = t3.ToByteArray();
                if (_isClient && client != null && client.Connected)
                {
                    try
                    {
                        client.BeginSend(buffer1, 0, buffer1.Length, SocketFlags.None, new AsyncCallback(SendCallbackClient), null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                if (_isServer) //Nếu đang với vai trò là sever thì gởi hình trên sever về tất cả người kết nối
                {

                    foreach (var item in _clientSocket)
                    {
                        item.Send(buffer1);
                    }

                }

            }

        }
コード例 #4
0
        /// <summary>
        /// Hàm thêm hình của thành viên vào danh sách các hình của Server
        /// </summary>
        /// <param name="person"></param>
        /// <param name="index"></param>
        private void AddListShape(PaintState person, int index)
        {
            if (person.FirstPoint.X > 0 && person.FirstPoint.Y > 0)
            {
                person.CurrentColor = Color.FromArgb(person.R1, person.G1, person.B1);
                MyShape a;
                switch (person.ShapeType1)
                {
                    case ShapeType.MyLine:
                        a = new MyLine(person);
                        break;
                    case ShapeType.MyRectangle:
                        a = new MyRectangle(person);
                        break;
                    case ShapeType.MyEllipse:
                        a = new MyEllipse(person);
                        break;
                    case ShapeType.MyTriangle:
                        a = new MyTriangle(person);
                        break;
                    case ShapeType.MyText:
                        a = new MyText(person);
                        break;
                    case ShapeType.MyEraser:
                        a = new MyEraser(person);
                        break;
                    case ShapeType.MyFill:
                        {

                            if (_myData.Count() > 0 && !_isRemoveBorder)
                            {
                                _myData.RemoveAt(_myData.Count() - 1);
                            }
                            if (_myData.Count() > 0 && _isStillCanMove && _isStillCanReSize)
                            {
                                _myData.RemoveAt(_myData.Count() - 1);
                                _isStillCanReSize = _isStillCanMove = false;
                            }

                            a = new MyFill(person);
                            floodFill(a.State.PointFill, a.State.ColorFill);

                            break;
                        }
                    default:
                        a = new MyLine(person);
                        break;
                }
                if (person.ShapeType1 == ShapeType.MyFill)
                {
                    _myData.Add(a);
                    _isRemoveBorder = true;
                    _isStillCanMove = _isStillCanReSize = false;

                }
                else
                {
                    if (_myData.Count() > 1 && _isStillCanMove == true && _isStillCanReSize == true)
                    {
                        _myData.Insert(_myData.Count() - 2, a);
                    }
                    else
                    {
                        _myData.Add(a);
                        _isRemoveBorder = true;
                        _isStillCanMove = _isStillCanReSize = false;
                    }
                }

                for (int i = 0; i < _clientSocket.Count; i++)
                {
                    if (i == index)
                    {
                        continue;
                    }
                    _clientSocket[i].Send(_buffer);
                }
                
                //Gởi hình nhận đến tất cả mọi người.
                this.Invalidate();
            }
           
        }