Example #1
0
        /// <summary>
        /// 绘制网格?
        /// </summary>
        /// <param name="canvas"></param>
        /// <param name="unitrect"></param>
        public void Draw(ICanvas canvas, RectangleF unitrect)
        {
            if (Enabled == false)
            {
                return;
            }
            float gridX           = Spacing.Width;
            float gridY           = Spacing.Height;
            float gridscreensizeX = canvas.ToScreen(gridX);
            float gridscreensizeY = canvas.ToScreen(gridY);

            if (gridscreensizeX < MinSize || gridscreensizeY < MinSize)            //超过最小尺寸则不绘制网格
            {
                return;
            }

            PointF leftpoint  = unitrect.Location;
            PointF rightpoint = ScreenUtils.RightPoint(canvas, unitrect);

            float left   = (float)Math.Round(leftpoint.X / gridX) * gridX;
            float top    = unitrect.Height + unitrect.Y;
            float right  = rightpoint.X;
            float bottom = (float)Math.Round(leftpoint.Y / gridY) * gridY;

            if (GridStyle == eStyle.Dots)            //如果网格类型是点??
            {
                GDI gdi = new GDI();
                gdi.BeginGDI(canvas.Graphics);
                for (float x = left; x <= right; x += gridX)
                {
                    for (float y = bottom; y <= top; y += gridY)
                    {
                        PointF p1 = canvas.ToScreen(new UnitPoint(x, y));
                        gdi.SetPixel((int)p1.X, (int)p1.Y, m_color.ToArgb());                        //设置像素
                    }
                }
                gdi.EndGDI();
            }
            if (GridStyle == eStyle.Lines)            //如果是线
            {
                Pen          pen  = new Pen(m_color);
                Pen          pen1 = new Pen(Color.DarkGray, 2); //每10根线条第10根线条得颜色粗细
                Pen          pen2 = new Pen(Color.Yellow, 2);   //边界线条
                GraphicsPath path = new GraphicsPath();
                // draw vertical lines画垂线
                while (left < right)
                {
                    PointF p1 = canvas.ToScreen(new UnitPoint(left, leftpoint.Y));
                    PointF p2 = canvas.ToScreen(new UnitPoint(left, rightpoint.Y));
                    if (left % 50 == 0 && left != 0)
                    {
                        canvas.Graphics.DrawLine(pen2, p1, p2);
                    }
                    else if (left % 10 == 0)
                    {
                        canvas.Graphics.DrawLine(pen1, p1, p2);
                    }
                    else
                    {
                        path.AddLine(p1, p2);
                        path.CloseFigure();
                    }
                    left += gridX;
                }

                // draw horizontal lines绘制水平线
                while (bottom < top)
                {
                    PointF p1 = canvas.ToScreen(new UnitPoint(leftpoint.X, bottom));
                    PointF p2 = canvas.ToScreen(new UnitPoint(rightpoint.X, bottom));
                    if (bottom % 50 == 0 && bottom != 0)
                    {
                        canvas.Graphics.DrawLine(pen2, p1, p2);
                    }
                    else if (bottom % 10 == 0)
                    {
                        canvas.Graphics.DrawLine(pen1, p1, p2);
                    }
                    else
                    {
                        path.AddLine(p1, p2);
                        path.CloseFigure();
                    }
                    bottom += gridY;
                }
                canvas.Graphics.DrawPath(pen, path);
            }
        }
Example #2
0
        /// <summary>
        /// 绘制网格
        /// </summary>
        /// <param name="canvas">画布</param>
        /// <param name="unitrect">单元矩形</param>
        public void Draw(ICanvas canvas, RectangleF unitrect)
        {
            if (Enabled == false)
            {
                return;
            }

            float gridX = Spacing.Width;
            float gridY = Spacing.Height;

            float gridscreensizeX = canvas.ToScreen(gridX);
            float gridscreensizeY = canvas.ToScreen(gridY);

            if (gridscreensizeX < MinSize || gridscreensizeY < MinSize)
            {
                return;
            }

            PointF leftpoint  = unitrect.Location;
            PointF rightpoint = ScreenUtils.RightPoint(canvas, unitrect);

            float left   = (float)Math.Round(leftpoint.X / gridX) * gridX;
            float top    = unitrect.Height + unitrect.Y;
            float right  = rightpoint.X;
            float bottom = (float)Math.Round(leftpoint.Y / gridY) * gridY;

            if (GridStyle == eStyle.Dots)
            {
                GDI gdi = new GDI();
                gdi.BeginGDI(canvas.Graphics);
                for (float x = left; x <= right; x += gridX)
                {
                    for (float y = bottom; y <= top; y += gridY)
                    {
                        PointF p1 = canvas.ToScreen(new UnitPoint(x, y));
                        gdi.SetPixel((int)p1.X, (int)p1.Y, color.ToArgb());
                    }
                }
                gdi.EndGDI();
            }

            if (GridStyle == eStyle.Lines)
            {
                Pen          pen  = new Pen(color);
                GraphicsPath path = new GraphicsPath();

                // 画垂直线条
                while (left < right)
                {
                    PointF p1 = canvas.ToScreen(new UnitPoint(left, leftpoint.Y));
                    PointF p2 = canvas.ToScreen(new UnitPoint(left, rightpoint.Y));
                    path.AddLine(p1, p2);
                    path.CloseFigure();
                    left += gridX;
                }

                // 画水平线条
                while (bottom < top)
                {
                    PointF p1 = canvas.ToScreen(new UnitPoint(leftpoint.X, bottom));
                    PointF p2 = canvas.ToScreen(new UnitPoint(rightpoint.X, bottom));
                    path.AddLine(p1, p2);
                    path.CloseFigure();
                    bottom += gridY;
                }
                canvas.Graphics.DrawPath(pen, path);
            }
        }
        public void DrawRuler(ICanvas canvas, RectangleF unitrect)
        {
            if (Enabled == false)
            {
                return;
            }
            float gridX           = Spacing.Width;
            float gridY           = Spacing.Height;
            float gridscreensizeX = canvas.ToScreen(gridX);
            float gridscreensizeY = canvas.ToScreen(gridY);

            if (gridscreensizeX < MinSize || gridscreensizeY < MinSize)
            {
                return;
            }

            PointF leftpoint  = unitrect.Location;
            PointF rightpoint = ScreenUtils.RightPoint(canvas, unitrect);

            float left   = 0;
            float top    = unitrect.Height + unitrect.Y;
            float right  = rightpoint.X;
            float bottom = 0;

            if (true)//绘制标尺
            {
                Pen          pen     = new Pen(Color.Red);
                GraphicsPath path    = new GraphicsPath();
                float        curSize = GetViewGrade(canvas);

                // draw vertical lines
                left = (float)Math.Round(leftpoint.X / gridX) * gridX - 1;
                double startPos = canvas.ToUnit(canvas.ToScreen(leftpoint.X) + 20f);
                int    count    = 0;
                while (left < right)
                {
                    if (left < startPos)
                    {
                        left += curSize /*0.1f*/;
                        count++;
                        continue;
                    }
                    PointF p1 = canvas.ToScreen(new UnitPoint(left, rightpoint.Y));
                    PointF p2 = canvas.ToScreen(new UnitPoint(left, rightpoint.Y));
                    p1.Y += 20;
                    p2.Y += 20;
                    if (count % 10 == 0)
                    {
                        // Set up all the string parameters.
                        string       stringText = left.ToString("0.###");
                        FontFamily   family     = new FontFamily("Arial");
                        int          emSize     = 10;
                        Brush        brush      = Brushes.Red;
                        StringFormat strF       = new StringFormat(StringFormatFlags.NoWrap);
                        canvas.Graphics.DrawString(stringText, new Font(family, emSize),
                                                   brush, new PointF(p1.X - 10, p1.Y - 20), strF);
                        p2.Y += 20;
                    }
                    else if (count % 5 == 0)
                    {
                        p2.Y += 15;
                    }
                    else
                    {
                        p2.Y += 10;
                    }
                    path.AddLine(p1, p2);
                    path.CloseFigure();
                    left += curSize /*0.1f*/;
                    count++;
                }

                // draw horizontal lines
                bottom   = (float)Math.Round(leftpoint.Y / gridY) * gridY - 1;
                startPos = canvas.ToUnit(canvas.ToScreen(rightpoint.Y) - 20f);
                count    = 0;
                while (bottom < top)
                {
                    if (bottom > startPos)
                    {
                        break;
                    }

                    PointF p1 = canvas.ToScreen(new UnitPoint(leftpoint.X, bottom));
                    PointF p2 = canvas.ToScreen(new UnitPoint(leftpoint.X, bottom));
                    p1.X += 20;
                    p2.X += 20;
                    if (count % 10 == 0)
                    {
                        // Set up all the string parameters.
                        string       stringText = bottom.ToString("0.###");
                        FontFamily   family     = new FontFamily("Arial");
                        int          emSize     = 10;
                        Brush        brush      = Brushes.Red;
                        StringFormat strF       = new StringFormat(StringFormatFlags.DirectionVertical);
                        canvas.Graphics.DrawString(stringText, new Font(family, emSize),
                                                   brush, new PointF(p1.X - 20, p1.Y - 10), strF);
                        p2.X += 20;
                    }
                    else if (count % 5 == 0)
                    {
                        p2.X += 15;
                    }
                    else
                    {
                        p2.X += 10;
                    }
                    path.AddLine(p1, p2);
                    path.CloseFigure();
                    bottom += curSize /*0.1f*/;
                    count++;
                }
                canvas.Graphics.DrawPath(pen, path);
            }
        }