Beispiel #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.White, this.ClientRectangle);
            Rectangle clientRectangle = this.ClientRectangle;

            --clientRectangle.Width;
            --clientRectangle.Height;
            e.Graphics.DrawRectangle(SystemPens.ControlDark, clientRectangle);
            Matrix matrix = new Matrix(1f, 0.0f, 0.0f, 1f, (float)(this.xOverflowOffset - this.hScrollBar1.Value), (float)(this.yOverflowOffset - this.vScrollBar1.Value));

            matrix.Scale(this.zoomFactor, this.zoomFactor);
            e.Graphics.Transform = matrix;
            e.Graphics.FillRectangle(Brushes.LightGray, this.GetRealFromVirtualRect(this.Dimension));
            this.DrawGridLines(e.Graphics, new Rectangle(0, 0, (int)Math.Round((double)this.ClientRectangle.Width * 1.5), (int)Math.Round((double)this.ClientRectangle.Height * 1.5)), Color.Black);
            Rectangle dimension = this.dimension;
            int       num1      = dimension.Left + dimension.Width / 2;
            int       num2      = dimension.Top + dimension.Height / 2;

            e.Graphics.DrawRectangle(Pens.Black, this.GetRealFromVirtualRect(dimension));
            e.Graphics.FillRectangle(Brushes.Black, this.GetRealFromVirtualRect(new Rectangle(num1 - 4, dimension.Y - 4, 8, 4)));
            e.Graphics.FillRectangle(Brushes.Black, this.GetRealFromVirtualRect(new Rectangle(num1 - 4, dimension.Bottom, 8, 4)));
            e.Graphics.FillRectangle(Brushes.Black, this.GetRealFromVirtualRect(new Rectangle(dimension.X - 4, num2 - 4, 4, 8)));
            e.Graphics.FillRectangle(Brushes.Black, this.GetRealFromVirtualRect(new Rectangle(dimension.Right, num2 - 4, 4, 8)));
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            this.minWidth            = this.minHeight = 0;
            this.maxWidth            = this.ClientRectangle.Width;
            this.maxHeight           = this.ClientRectangle.Height;
            using (Brush brush1 = (Brush) new SolidBrush(Color.Red))
            {
                using (Pen pen1 = new Pen(Color.Black, 2f))
                {
                    using (Pen pen2 = new Pen(Color.Blue, 2f))
                    {
                        using (Brush brush2 = (Brush) new SolidBrush(Color.Blue))
                        {
                            using (Pen pen3 = new Pen(Color.Green))
                            {
                                using (Brush brush3 = (Brush) new SolidBrush(Color.Green))
                                {
                                    for (int index = 0; index < this.points.Count; ++index)
                                    {
                                        ShapePoint point      = this.points[index];
                                        ShapePoint shapePoint = index < this.points.Count - 1 ? this.points[index + 1] : this.points[0];
                                        this.UpdateMaxSize((ShapePointBase)point);
                                        this.UpdateMaxSize((ShapePointBase)shapePoint);
                                        Pen pen4 = this.point == point ? pen2 : pen1;
                                        if (point.Bezier)
                                        {
                                            e.Graphics.DrawBezier(pen4, this.GetRealFromVirtualPoint((ShapePointBase)point), this.GetRealFromVirtualPoint(point.ControlPoint1), this.GetRealFromVirtualPoint(point.ControlPoint2), this.GetRealFromVirtualPoint((ShapePointBase)shapePoint));
                                            e.Graphics.DrawLine(pen3, this.GetRealFromVirtualPoint((ShapePointBase)point), this.GetRealFromVirtualPoint(point.ControlPoint1));
                                            e.Graphics.DrawLine(pen3, this.GetRealFromVirtualPoint((ShapePointBase)shapePoint), this.GetRealFromVirtualPoint(point.ControlPoint2));
                                            e.Graphics.FillEllipse(point.ControlPoint1 == this.point || point.ControlPoint1.Selected ? brush2 : brush3, this.GetRealFromVirtualRect(point.ControlPoint1.GetBounds(8)));
                                            e.Graphics.FillEllipse(point.ControlPoint2 == this.point || point.ControlPoint2.Selected ? brush2 : brush3, this.GetRealFromVirtualRect(point.ControlPoint2.GetBounds(8)));
                                            this.UpdateMaxSize(point.ControlPoint1);
                                            this.UpdateMaxSize(point.ControlPoint2);
                                        }
                                        else
                                        {
                                            e.Graphics.DrawLine(point == this.point ? pen2 : pen1, this.GetRealFromVirtualPoint((ShapePointBase)point), this.GetRealFromVirtualPoint((ShapePointBase)shapePoint));
                                        }
                                        e.Graphics.FillEllipse(point == this.point || point.Selected ? brush2 : brush1, this.GetRealFromVirtualRect(point.GetBounds(8)));
                                        e.Graphics.FillEllipse(shapePoint == this.point || shapePoint.Selected ? brush2 : brush1, this.GetRealFromVirtualRect(shapePoint.GetBounds(8)));
                                    }
                                    if (this.mouseDown)
                                    {
                                        Rectangle rect = new Rectangle(this.downPoint.X < this.curPoint.X ? this.downPoint.X : this.curPoint.X, this.downPoint.Y < this.curPoint.Y ? this.downPoint.Y : this.curPoint.Y, Math.Abs(this.downPoint.X - this.curPoint.X), Math.Abs(this.downPoint.Y - this.curPoint.Y));
                                        rect.X      = (int)Math.Round((double)rect.X / Math.Pow((double)this.zoomFactor, 1.0));
                                        rect.Y      = (int)Math.Round((double)rect.Y / Math.Pow((double)this.zoomFactor, 1.0));
                                        rect.Width  = (int)Math.Round((double)rect.Width / Math.Pow((double)this.zoomFactor, 1.0));
                                        rect.Height = (int)Math.Round((double)rect.Height / Math.Pow((double)this.zoomFactor, 1.0));
                                        pen2.Width  = 1f;
                                        e.Graphics.DrawRectangle(pen2, rect);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.vScrollBar1.Minimum = this.minHeight;
            this.hScrollBar1.Minimum = this.minWidth;
            this.vScrollBar1.Maximum = this.maxHeight;
            this.hScrollBar1.Maximum = this.maxWidth;
            this.SizeScrollBars();
            if (!this.snapToGrid.IsLastSnapped)
            {
                return;
            }
            e.Graphics.FillEllipse((Brush) new SolidBrush(Color.Red), this.GetRealFromVirtualRect(new Rectangle((int)this.snapToGrid.SnappedPoint.X - 2, (int)this.snapToGrid.SnappedPoint.Y - 2, 4, 4)));
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            //fill the entire area with white
            e.Graphics.FillRectangle(Brushes.White, ClientRectangle);

            Rectangle rect = ClientRectangle; rect.Width--; rect.Height--;

            //draw a non-movable rectangle encompassing the drawable area
            e.Graphics.DrawRectangle(SystemPens.ControlDark, rect);

            //initialize the matrix to be used when transforming the coordinate system
            //does scaling the translation in accordance with the positions of the scrollbars
            Matrix transformationMatrix = new Matrix(1, 0, 0, 1, xOverflowOffset - hScrollBar1.Value, yOverflowOffset - vScrollBar1.Value);

            transformationMatrix.Scale(zoomFactor, zoomFactor);

            e.Graphics.Transform = transformationMatrix;

            //fill the control area
            e.Graphics.FillRectangle(Brushes.LightGray, GetRealFromVirtualRect(Dimension));

            //draw the guides in the entire area
            DrawGridLines(e.Graphics,
                          new Rectangle(0, 0,
                                        (int)Math.Round((ClientRectangle.Width * 1.5)),        //+ ClientRectangle.Width
                                        (int)Math.Round((ClientRectangle.Height * 1.5)))       //+ ClientRectangle.Height
                          , Color.Black);

            Rectangle r    = dimension;
            int       midx = r.Left + r.Width / 2;
            int       midy = r.Top + r.Height / 2;

            //draw the black rectangle denoting the clipping rectangle of the control area
            e.Graphics.DrawRectangle(Pens.Black, GetRealFromVirtualRect(r));
            e.Graphics.FillRectangle(Brushes.Black, GetRealFromVirtualRect(new Rectangle(midx - 4, r.Y - 4, 8, 4)));
            e.Graphics.FillRectangle(Brushes.Black, GetRealFromVirtualRect(new Rectangle(midx - 4, r.Bottom, 8, 4)));
            e.Graphics.FillRectangle(Brushes.Black, GetRealFromVirtualRect(new Rectangle(r.X - 4, midy - 4, 4, 8)));
            e.Graphics.FillRectangle(Brushes.Black, GetRealFromVirtualRect(new Rectangle(r.Right, midy - 4, 4, 8)));

            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            minWidth  = minHeight = 0;
            maxWidth  = this.ClientRectangle.Width;
            maxHeight = this.ClientRectangle.Height;

            using (Brush dot = new SolidBrush(Color.Red))
                using (Pen line = new Pen(Color.Black, 2))
                    using (Pen activeLine = new Pen(Color.Blue, 2))
                        using (Brush activeDot = new SolidBrush(Color.Blue))
                            using (Pen controlLine = new Pen(Color.Green))
                                using (Brush controlDot = new SolidBrush(Color.Green))
                                {
                                    //draw the points and the lines connecting them
                                    for (int i = 0; i < points.Count; i++)
                                    {
                                        ShapePoint pos1 = points[i];
                                        ShapePoint pos2 = i < points.Count - 1 ? points[i + 1] : points[0];

                                        UpdateMaxSize(pos1);
                                        UpdateMaxSize(pos2);

                                        Pen pen = point == pos1 ? activeLine : line;
                                        //if the line at the point is a bezier curve, draw it together with the tangents and the control points
                                        if (pos1.Bezier)
                                        {
                                            //draw the bezier curve
                                            e.Graphics.DrawBezier(pen,
                                                                  GetRealFromVirtualPoint(pos1),
                                                                  GetRealFromVirtualPoint(pos1.ControlPoint1),
                                                                  GetRealFromVirtualPoint(pos1.ControlPoint2),
                                                                  GetRealFromVirtualPoint(pos2));

                                            //draw the tangents to the control points
                                            e.Graphics.DrawLine(controlLine, GetRealFromVirtualPoint(pos1), GetRealFromVirtualPoint(pos1.ControlPoint1));
                                            e.Graphics.DrawLine(controlLine, GetRealFromVirtualPoint(pos2), GetRealFromVirtualPoint(pos1.ControlPoint2));

                                            //draw the control points
                                            e.Graphics.FillEllipse((pos1.ControlPoint1 == point || pos1.ControlPoint1.Selected) ? activeDot : controlDot,
                                                                   GetRealFromVirtualRect(pos1.ControlPoint1.GetBounds(8)));
                                            e.Graphics.FillEllipse((pos1.ControlPoint2 == point || pos1.ControlPoint2.Selected) ? activeDot : controlDot,
                                                                   GetRealFromVirtualRect(pos1.ControlPoint2.GetBounds(8)));

                                            //update the maximum size in response to points outside of the drawable area
                                            UpdateMaxSize(pos1.ControlPoint1);
                                            UpdateMaxSize(pos1.ControlPoint2);
                                        }
                                        //draws a straight line
                                        else
                                        {
                                            e.Graphics.DrawLine(pos1 == point ? activeLine : line, GetRealFromVirtualPoint(pos1), GetRealFromVirtualPoint(pos2));
                                        }

                                        //draws the two points at the ends of the line
                                        e.Graphics.FillEllipse((pos1 == point || pos1.Selected) ? activeDot : dot, GetRealFromVirtualRect(pos1.GetBounds(8)));
                                        e.Graphics.FillEllipse((pos2 == point || pos2.Selected) ? activeDot : dot, GetRealFromVirtualRect(pos2.GetBounds(8)));
                                    }
                                    //draw multiple point selection rectangle
                                    if (mouseDown)
                                    {
                                        r = new Rectangle(
                                            downPoint.X < curPoint.X ? downPoint.X : curPoint.X,
                                            downPoint.Y < curPoint.Y ? downPoint.Y : curPoint.Y,
                                            Math.Abs(downPoint.X - curPoint.X),
                                            Math.Abs(downPoint.Y - curPoint.Y));
                                        //convert the rectangle location and size from virtual to real
                                        r.X = (int)Math.Round((r.X) / Math.Pow(zoomFactor, 1));
                                        r.Y = (int)Math.Round((r.Y) / Math.Pow(zoomFactor, 1));

                                        r.Width  = (int)Math.Round(r.Width / Math.Pow(zoomFactor, 1));
                                        r.Height = (int)Math.Round(r.Height / Math.Pow(zoomFactor, 1));

                                        activeLine.Width = 1;
                                        e.Graphics.DrawRectangle(activeLine, r);
                                    }
                                }
            //set the bounds of the scrollbars to contain all points
            vScrollBar1.Minimum = minHeight;
            hScrollBar1.Minimum = minWidth;

            vScrollBar1.Maximum = maxHeight;
            hScrollBar1.Maximum = maxWidth;
            SizeScrollBars();

            //draw little red point indicating the line of snap at a given point
            if (snapToGrid.IsLastSnapped)
            {
                e.Graphics.FillEllipse(new SolidBrush(Color.Red), GetRealFromVirtualRect(new Rectangle((int)snapToGrid.SnappedPoint.X - 2, (int)snapToGrid.SnappedPoint.Y - 2, 4, 4)));
            }
        }