public static MessageDrawing CreateFromXml(string xml)
        {
            string me = "MessageDrawing.CreateFromXml()";

            //Debug.WriteLine( "MessageDrawing.CreateFromXml()" );
            Debug.WriteLine(me + " : " + xml);

            MessageDrawing msg = null;

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml);
            XmlNode root = xmlDoc.DocumentElement;

            bool  filled = false;
            Color fillColor = Color.Black, penColor = Color.Black;

            XmlNodeList nodes;
            XmlNode     node;

            node = root.SelectSingleNode("fill");
            if (node != null)
            {
                try
                {
                    filled = true;
                    int rgb = Convert.ToInt32(node.Attributes["color"].Value);
                    fillColor = Color.FromArgb(rgb);
                }
                catch (Exception ex)
                {
                    throw new Exception("Malformed MessageDrawing : fill element has error.");
                }
            }
            node = root.SelectSingleNode("pen");
            if (node != null)
            {
                try
                {
                    int rgb = Convert.ToInt32(node.Attributes["color"].Value);
                    penColor = Color.FromArgb(rgb);
                }
                catch (Exception ex)
                {
                    throw new Exception("Malformed MessageDrawing : fill element has error.");
                }
            }

            XmlNode data_node;
            string  type = null;

            try
            {
                //node = root.SelectSingleNode("descendant::book[author/last-name='Austen']");
                data_node = root.SelectSingleNode("data");
                type      = data_node.Attributes["type"].Value;
            }
            catch (Exception ex)
            {
                throw new Exception("Malformed MessageDrawing : missing data element or type attribute.");
            }

            switch (type)
            {
            case "line":
                Debug.WriteLine(me + " : create a MessageDrawingLine");

                try
                {
                    nodes = data_node.SelectNodes("point");
                    Point p1, p2;
                    p1 = new Point(Convert.ToInt32(nodes[0].Attributes["x"].Value), Convert.ToInt32(nodes[0].Attributes["y"].Value));
                    p2 = new Point(Convert.ToInt32(nodes[1].Attributes["x"].Value), Convert.ToInt32(nodes[1].Attributes["y"].Value));

                    msg = new MessageDrawingLine(penColor, p1, p2);
                }
                catch (Exception ex)
                {
                    throw new Exception("Malformed MessageDrawingLine : " + ex.Message + EOL + ex.StackTrace);
                }
                break;

            case "rectangle":
                try
                {
                    node = data_node.SelectSingleNode("point");
                    Point point = new Point(Convert.ToInt32(node.Attributes["x"].Value), Convert.ToInt32(node.Attributes["y"].Value));
                    node = data_node.SelectSingleNode("size");
                    Size size = new Size(Convert.ToInt32(node.Attributes["w"].Value), Convert.ToInt32(node.Attributes["h"].Value));

                    if (filled)
                    {
                        msg = new MessageDrawingRectangle(filled, fillColor, point, size);
                    }
                    else
                    {
                        msg = new MessageDrawingRectangle(filled, penColor, point, size);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Malformed MessageDrawingRectangle : " + ex.Message + EOL + ex.StackTrace);
                }
                break;

            case "ellipse":
                try
                {
                    node = data_node.SelectSingleNode("point");
                    Point point = new Point(Convert.ToInt32(node.Attributes["x"].Value), Convert.ToInt32(node.Attributes["y"].Value));
                    node = data_node.SelectSingleNode("size");
                    Size size = new Size(Convert.ToInt32(node.Attributes["w"].Value), Convert.ToInt32(node.Attributes["h"].Value));

                    if (filled)
                    {
                        msg = new MessageDrawingEllipse(filled, fillColor, point, size);
                    }
                    else
                    {
                        msg = new MessageDrawingEllipse(filled, penColor, point, size);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Malformed MessageDrawingEllipse : " + ex.Message + EOL + ex.StackTrace);
                }
                break;

            case "triangle":
                try
                {
                    nodes = data_node.SelectNodes("point");
                    Point p1, p2, p3;
                    p1 = new Point(Convert.ToInt32(nodes[0].Attributes["x"].Value), Convert.ToInt32(nodes[0].Attributes["y"].Value));
                    p2 = new Point(Convert.ToInt32(nodes[1].Attributes["x"].Value), Convert.ToInt32(nodes[1].Attributes["y"].Value));
                    p3 = new Point(Convert.ToInt32(nodes[2].Attributes["x"].Value), Convert.ToInt32(nodes[2].Attributes["y"].Value));

                    if (filled)
                    {
                        msg = new MessageDrawingTriangle(filled, fillColor, p1, p2, p3);
                    }
                    else
                    {
                        msg = new MessageDrawingTriangle(filled, penColor, p1, p2, p3);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Malformed MessageDrawingTriangle : " + ex.Message + EOL + ex.StackTrace);
                }
                break;
            }

            if (msg == null)
            {
                throw new Exception("Malformed MessageDrawing : Unknow drawing type");
            }

            return(msg);
        }
示例#2
0
        private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (numclicks == 0)
                {
                    // Set numclicks and first point to line
                    if (toolLine.Checked)
                    {
                        pts            = new Point[2];
                        numclicks      = 1;
                        pts[numclicks] = new Point(e.X, e.Y);
                    }
                    // Set numclicks and first point to rectangle
                    if (toolRectangle.Checked)
                    {
                        pts            = new Point[2];
                        numclicks      = 1;
                        pts[numclicks] = new Point(e.X, e.Y);
                    }
                    // Set numclicks and first point to triangle
                    if (toolTriangle.Checked)
                    {
                        pts            = new Point[3];
                        numclicks      = 2;
                        pts[numclicks] = new Point(e.X, e.Y);
                    }
                    // Set numclicks and first point to ellipse
                    if (toolEllipse.Checked)
                    {
                        pts            = new Point[2];
                        numclicks      = 1;
                        pts[numclicks] = new Point(e.X, e.Y);
                    }
                }
                else
                {
                    MessageDrawing messageDrawing = null;

                    numclicks--;
                    pts[numclicks] = new Point(e.X, e.Y);

                    if (numclicks == 0)
                    {
                        // draw line
                        if (toolLine.Checked)
                        {
                            graphics.DrawLine(new Pen(color), pts[1], pts[0]);

                            messageDrawing = new MessageDrawingLine(color, pts[1], pts[0]);
                        }
                        // draw rectangle
                        else if (toolRectangle.Checked)
                        {
                            int x = pts[1].X;
                            int y = pts[1].Y;
                            int w = mpx - x;
                            int h = mpy - y;
                            if (w < 0)
                            {
                                x = mpx;
                                w = -w;
                            }
                            if (h < 0)
                            {
                                y = mpy;
                                h = -h;
                            }
                            if (toolFill.Checked)
                            {
                                graphics.FillRectangle(new SolidBrush(color), x, y, w, h);
                            }
                            else
                            {
                                graphics.DrawRectangle(new Pen(color), x, y, w, h);
                            }

                            messageDrawing = new MessageDrawingRectangle(toolFill.Checked, color, new Point(x, y), new Size(w, h));
                        }
                        else if (toolTriangle.Checked)
                        // draw triangle
                        {
                            if (toolFill.Checked)
                            {
                                graphics.FillPolygon(new SolidBrush(color), new Point[] { pts[2], pts[1], pts[0] });
                            }
                            else
                            {
                                graphics.DrawPolygon(new Pen(color), new Point[] { pts[2], pts[1], pts[0] });
                            }

                            messageDrawing = new MessageDrawingTriangle(toolFill.Checked, color, pts[2], pts[1], pts[0]);
                        }
                        else if (toolEllipse.Checked)
                        // draw ellipse
                        {
                            int x = pts[1].X;
                            int y = pts[1].Y;
                            int w = mpx - x;
                            int h = mpy - y;
                            if (w < 0)
                            {
                                x = mpx;
                                w = -w;
                            }
                            if (h < 0)
                            {
                                y = mpy;
                                h = -h;
                            }
                            if (toolFill.Checked)
                            {
                                graphics.FillEllipse(new SolidBrush(color), x, y, w, h);
                            }
                            else
                            {
                                graphics.DrawEllipse(new Pen(color), x, y, w, h);
                            }

                            messageDrawing = new MessageDrawingEllipse(toolFill.Checked, color, new Point(x, y), new Size(w, h));
                        }
                    }

                    if (messageDrawing != null)
                    {
                        messageDrawing.Send(this, this.xmlBlaster);
                    }
                }
            }
        }