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); 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)); 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)); path.AddLine(p1, p2); path.CloseFigure(); bottom += gridY; } canvas.Graphics.DrawPath(pen, path); } }
/// <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); } }
/// <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); } }