/// <summary> /// 使选中方块无效并重绘(移动选中的方块) /// </summary> /// <param name="dostatic"></param> /// <param name="rect"></param> public void DoInvalidate(bool dostatic, RectangleF rect) { if (dostatic) { m_staticDirty = true; } Invalidate(ScreenUtils.ConvertRect(rect)); }
public RectangleF Selection(ICanvas canvas) { Rectangle screenRect = ScreenRect(); if (screenRect.IsEmpty) { return(RectangleF.Empty); } return(ScreenUtils.ToUnitNormalized(canvas, screenRect)); }
public void DrawString(ICanvas canvas, string str, Font font, Brush brush, RectangleF rect, StringFormat format) { if (str == string.Empty) { return; } RectangleF screenRect = ScreenUtils.ToScreenNormalized(canvas, rect); canvas.Graphics.DrawString(str, font, brush, screenRect, format); }
public static RectangleF LineBoundingRect(UnitPoint linepoint1, UnitPoint linepoint2, float halflinewidth) { double x = Math.Min(linepoint1.X, linepoint2.X); double y = Math.Min(linepoint1.Y, linepoint2.Y); double w = Math.Abs(linepoint1.X - linepoint2.X); double h = Math.Abs(linepoint1.Y - linepoint2.Y); RectangleF boundingrect = ScreenUtils.GetRect(x, y, w, h); boundingrect.Inflate(halflinewidth, halflinewidth); return(boundingrect); }
public static RectangleF GetRect(UnitPoint p1, UnitPoint p2, double width) { double x = Math.Min(p1.X, p2.X); double y = Math.Min(p1.Y, p2.Y); double w = Math.Abs(p1.X - p2.X); double h = Math.Abs(p1.Y - p2.Y); RectangleF rect = ScreenUtils.GetRect(x, y, w, h); rect.Inflate((float)width, (float)width); return(rect); }
} /// <summary> /// 在画布按照IDrawObject重画 /// </summary> /// <param name="obj"></param> void RepaintObject(IDrawObject obj) { if (obj == null) { return; } CanvasWrapper dc = new CanvasWrapper(this, Graphics.FromHwnd(Handle), ClientRectangle); RectangleF invalidaterect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(dc, obj.GetBoundingRect(dc))); obj.Draw(dc, invalidaterect); dc.Graphics.Dispose(); dc.Dispose(); }
public void Draw(ICanvas canvas, RectangleF unitrect) { RectangleF r = ScreenUtils.ToScreenNormalized(canvas, unitrect); canvas.Graphics.FillRectangle(m_backgroundBrush, r); //StringFormat f = new StringFormat(); //f.Alignment = StringAlignment.Center; //PointF centerpoint = new PointF(r.Width / 2, r.Height / 2); //canvas.Graphics.TranslateTransform(centerpoint.X, centerpoint.Y); //canvas.Graphics.RotateTransform(-15); //canvas.Graphics.DrawString("Jesper Kristiansen (2007)", m_font, m_brush, 0, 0, f); //canvas.Graphics.ResetTransform(); }
protected void DrawPoint(ICanvas canvas, Pen pen, Brush fillBrush) { Rectangle screenrect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(canvas, m_boundingRect)); canvas.Graphics.DrawRectangle(pen, screenrect); screenrect.X++; screenrect.Y++; screenrect.Width--; screenrect.Height--; if (fillBrush != null) { canvas.Graphics.FillRectangle(fillBrush, screenrect); } }
protected override void OnMouseDoubleClick(MouseEventArgs e) { if (m_commandType != eCommandType.select || m_model.SelectedCount < 1) { return; } UnitPoint mousePt = ToUnit(new PointF(e.X, e.Y)); DrawTools.RectBase rectBase = null; foreach (var curObj in m_model.SelectedObjects) { rectBase = curObj as DrawTools.RectBase; if (rectBase != null && rectBase.PointInObject(m_canvaswrapper, mousePt)) { break; } } if (rectBase == null) { return; } //lay down a textbox on the rect shape RectangleF rect = ScreenUtils.ToScreenNormalized(m_canvaswrapper, rectBase.GetExactBoundingRect(m_canvaswrapper)); rect.Inflate(1, 1); m_rectBaseTextBox = new TextBox(); m_rectBaseTextBox.Tag = rectBase; m_rectBaseTextBox.Multiline = true; m_rectBaseTextBox.Font = new Font("Times New Roman", 15.0f); m_rectBaseTextBox.Location = new Point((int)rect.Left, (int)rect.Top); m_rectBaseTextBox.Size = new Size((int)rect.Width, (int)rect.Height); m_rectBaseTextBox.Text = rectBase.Text; Controls.Add(m_rectBaseTextBox); m_rectBaseTextBox.Focus(); m_rectBaseTextBox.SelectAll(); base.OnMouseDoubleClick(e); }
/// <summary> /// 鼠标移动事件 /// </summary> /// <param name="e"></param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (m_selection != null) { Graphics dc = Graphics.FromHwnd(Handle); m_selection.SetMousePoint(dc, new PointF(e.X, e.Y)); dc.Dispose(); return; } if (m_commandType == eCommandType.pan && e.Button == MouseButtons.Left) //如果选择pan(移动画布),切按住鼠标左键 { m_dragOffset.X = -(m_mousedownPoint.X - e.X); m_dragOffset.Y = -(m_mousedownPoint.Y - e.Y); m_lastCenterPoint = CenterPointUnit(); DoInvalidate(true); } UnitPoint mousepoint; UnitPoint unitpoint = ToUnit(new PointF(e.X, e.Y)); if (m_commandType == eCommandType.draw || m_commandType == eCommandType.move || m_nodeMoveHelper.IsEmpty == false) //如果使绘制或者移动或节点移动或节点移动为空 { Rectangle invalidaterect = Rectangle.Empty; ISnapPoint newsnap = null; mousepoint = GetMousePoint(); if (RunningSnapsEnabled) { newsnap = m_model.SnapPoint(m_canvaswrapper, mousepoint, m_runningSnapTypes, null); } if (newsnap == null) { newsnap = m_model.GridLayer.SnapPoint(m_canvaswrapper, mousepoint, null); } if ((m_snappoint != null) && ((newsnap == null) || (newsnap.SnapPoint != m_snappoint.SnapPoint) || m_snappoint.GetType() != newsnap.GetType())) //绘图时点击左键生成节点 { Console.WriteLine("canvas 688"); invalidaterect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(m_canvaswrapper, m_snappoint.BoundingRect)); invalidaterect.Inflate(2, 2); RepaintStatic(invalidaterect); // remove old snappoint m_snappoint = newsnap; } if (m_commandType == eCommandType.move)//使用移动工具时,重画图像 { Invalidate(invalidaterect); } if (m_snappoint == null) { m_snappoint = newsnap; } } m_owner.SetPositionInfo(unitpoint); //给出绘制信息 m_owner.SetSnapInfo(m_snappoint); //给出绘制信息 //UnitPoint mousepoint; if (m_snappoint != null) { mousepoint = m_snappoint.SnapPoint; } else { mousepoint = GetMousePoint(); } if (m_newObject != null) //如果不为空则重画* { //MessageBox.Show(m_newObject.ToString()); //Console.WriteLine(m_newObject.ToString()); Rectangle invalidaterect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(m_canvaswrapper, m_newObject.GetBoundingRect(m_canvaswrapper))); invalidaterect.Inflate(2, 2); RepaintStatic(invalidaterect); m_newObject.OnMouseMove(m_canvaswrapper, mousepoint); RepaintObject(m_newObject); } if (m_snappoint != null) //如果不为空则重画 { RepaintSnappoint(m_snappoint); } if (m_moveHelper.HandleMouseMoveForMove(mousepoint)) //重绘 { Refresh(); //Invalidate(); } RectangleF rNoderect = m_nodeMoveHelper.HandleMouseMoveForNode(mousepoint); //坐标获取 if (rNoderect != RectangleF.Empty) { Rectangle invalidaterect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(m_canvaswrapper, rNoderect)); //坐标转换 RepaintStatic(invalidaterect); //重绘 CanvasWrapper dc = new CanvasWrapper(this, Graphics.FromHwnd(Handle), ClientRectangle); dc.Graphics.Clip = new Region(ClientRectangle); //m_nodeMoveHelper.DrawOriginalObjects(dc, rNoderect); m_nodeMoveHelper.DrawObjects(dc, rNoderect); if (m_snappoint != null) { RepaintSnappoint(m_snappoint); //重绘 } dc.Graphics.Dispose(); dc.Dispose(); } }
/// <summary> /// 响应消息 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { CommonTools.Tracing.StartTrack(Program.TracePaint); //TracePaint=1 ClearPens(); //清空画笔 e.Graphics.SmoothingMode = m_smoothingMode; CanvasWrapper dc = new CanvasWrapper(this, e.Graphics, ClientRectangle); Rectangle cliprectangle = e.ClipRectangle; if (m_staticImage == null) { cliprectangle = ClientRectangle; m_staticImage = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); m_staticDirty = true; } RectangleF r = ScreenUtils.ToUnitNormalized(dc, cliprectangle); if (float.IsNaN(r.Width) || float.IsInfinity(r.Width)) { r = ScreenUtils.ToUnitNormalized(dc, cliprectangle); } if (m_staticDirty) { m_staticDirty = false; CanvasWrapper dcStatic = new CanvasWrapper(this, Graphics.FromImage(m_staticImage), ClientRectangle); dcStatic.Graphics.SmoothingMode = m_smoothingMode; m_model.BackgroundLayer.Draw(dcStatic, r); if (m_model.GridLayer.Enabled) { m_model.GridLayer.Draw(dcStatic, r); } PointF nullPoint = ToScreen(new UnitPoint(0, 0)); dcStatic.Graphics.DrawLine(Pens.Blue, nullPoint.X - 10, nullPoint.Y, nullPoint.X + 10, nullPoint.Y); dcStatic.Graphics.DrawLine(Pens.Blue, nullPoint.X, nullPoint.Y - 10, nullPoint.X, nullPoint.Y + 10); ICanvasLayer[] layers = m_model.Layers; //线条粗细颜色 for (int layerindex = layers.Length - 1; layerindex >= 0; layerindex--) { if (layers[layerindex] != m_model.ActiveLayer && layers[layerindex].Visible) { layers[layerindex].Draw(dcStatic, r); } } if (m_model.ActiveLayer != null) { m_model.ActiveLayer.Draw(dcStatic, r); } dcStatic.Dispose(); } e.Graphics.DrawImage(m_staticImage, cliprectangle, cliprectangle, GraphicsUnit.Pixel); foreach (IDrawObject drawobject in m_model.SelectedObjects) { drawobject.Draw(dc, r); } if (m_newObject != null) { m_newObject.Draw(dc, r); } if (m_snappoint != null) { m_snappoint.Draw(dc); } if (m_selection != null) { m_selection.Reset(); m_selection.SetMousePoint(e.Graphics, this.PointToClient(Control.MousePosition)); } if (m_moveHelper.IsEmpty == false) { m_moveHelper.DrawObjects(dc, r); } if (m_nodeMoveHelper.IsEmpty == false) { m_nodeMoveHelper.DrawObjects(dc, r); } dc.Dispose(); ClearPens(); CommonTools.Tracing.EndTrack(Program.TracePaint, "OnPaint complete"); }
/// <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); } }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (m_selection != null) { Graphics dc = Graphics.FromHwnd(Handle); m_selection.SetMousePoint(dc, new PointF(e.X, e.Y)); dc.Dispose(); return; } if (m_commandType == eCommandType.pan && (e.Button == MouseButtons.Left || e.Button == MouseButtons.Middle)) { m_dragOffset.X = -(m_mousedownPoint.X - e.X); m_dragOffset.Y = -(m_mousedownPoint.Y - e.Y); m_lastCenterPoint = CenterPointUnit(); DoInvalidate(true); } UnitPoint mousepoint; UnitPoint unitpoint = ToUnit(new PointF(e.X, e.Y)); if (m_commandType == eCommandType.draw || m_commandType == eCommandType.move || m_nodeMoveHelper.IsEmpty == false) { Rectangle invalidaterect = Rectangle.Empty; ISnapPoint newsnap = null; mousepoint = GetMousePoint(); if (RunningSnapsEnabled) { newsnap = m_model.SnapPoint(m_canvaswrapper, mousepoint, m_runningSnapTypes, null); } if (newsnap == null) { newsnap = m_model.GridLayer.SnapPoint(m_canvaswrapper, mousepoint, null); } if ((m_snappoint != null) && ((newsnap == null) || (newsnap.SnapPoint != m_snappoint.SnapPoint) || m_snappoint.GetType() != newsnap.GetType())) { invalidaterect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(m_canvaswrapper, m_snappoint.BoundingRect)); invalidaterect.Inflate(2, 2); RepaintStatic(invalidaterect); // remove old snappoint m_snappoint = newsnap; } if (m_commandType == eCommandType.move) { Invalidate(invalidaterect); } if (m_snappoint == null) { m_snappoint = newsnap; } } m_owner.SetPositionInfo(unitpoint); m_owner.SetSnapInfo(m_snappoint); //UnitPoint mousepoint; if (m_snappoint != null) { mousepoint = m_snappoint.SnapPoint; } else { mousepoint = GetMousePoint(); } if (m_newObject != null) { Rectangle invalidaterect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(m_canvaswrapper, m_newObject.GetBoundingRect(m_canvaswrapper))); invalidaterect.Inflate(2, 2); RepaintStatic(invalidaterect); m_newObject.OnMouseMove(m_canvaswrapper, mousepoint); RepaintObject(m_newObject); } if (m_snappoint != null) { RepaintSnappoint(m_snappoint); } if (m_moveHelper.HandleMouseMoveForMove(mousepoint)) { Refresh(); //Invalidate(); } RectangleF rNoderect = m_nodeMoveHelper.HandleMouseMoveForNode(mousepoint); if (rNoderect != RectangleF.Empty) { Rectangle invalidaterect = ScreenUtils.ConvertRect(ScreenUtils.ToScreenNormalized(m_canvaswrapper, rNoderect)); RepaintStatic(invalidaterect); CanvasWrapper dc = new CanvasWrapper(this, Graphics.FromHwnd(Handle), ClientRectangle); dc.Graphics.Clip = new Region(ClientRectangle); //m_nodeMoveHelper.DrawOriginalObjects(dc, rNoderect); m_nodeMoveHelper.DrawObjects(dc, rNoderect); if (m_snappoint != null) { RepaintSnappoint(m_snappoint); } dc.Graphics.Dispose(); dc.Dispose(); } }
/// <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); } }
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); } }