/// <summary> /// 在画布按照snappoint重画 /// </summary> /// <param name="snappoint"></param> void RepaintSnappoint(ISnapPoint snappoint) { if (snappoint == null) { return; } CanvasWrapper dc = new CanvasWrapper(this, Graphics.FromHwnd(Handle), ClientRectangle); snappoint.Draw(dc); dc.Graphics.Dispose(); dc.Dispose(); } /// <summary>
} /// <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 CanvasCtrl(ICanvasOwner owner, IModel datamodel) { m_canvaswrapper = new CanvasWrapper(this); m_owner = owner; m_model = datamodel; InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); //忽略窗口信息减少闪烁,由控件来绘制自身 this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); //控件将首先绘制到缓冲区而不是绘制到屏幕。 m_commandType = eCommandType.select; m_cursors.AddCursor(eCommandType.select, Cursors.Arrow); //设置选中光标类型 m_cursors.AddCursor(eCommandType.draw, Cursors.Cross); //设置画图光标类型 m_cursors.AddCursor(eCommandType.pan, "hmove.cur"); //设置移动画面光标类型 m_cursors.AddCursor(eCommandType.move, Cursors.SizeAll); //设置移动光标类型 m_cursors.AddCursor(eCommandType.edit, Cursors.Cross); //设置编辑光标类型 UpdateCursor(); //更新光标 m_moveHelper = new MoveHelper(this); m_nodeMoveHelper = new NodeMoveHelper(m_canvaswrapper); }
public CanvasCtrl(ICanvasOwner owner, IModel datamodel) { m_canvaswrapper = new CanvasWrapper(this); m_owner = owner; m_model = datamodel; InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); m_commandType = eCommandType.select; m_cursors.AddCursor(eCommandType.select, Cursors.Arrow); m_cursors.AddCursor(eCommandType.draw, Cursors.Cross); m_cursors.AddCursor(eCommandType.pan, "hmove.cur"); m_cursors.AddCursor(eCommandType.move, Cursors.SizeAll); m_cursors.AddCursor(eCommandType.edit, Cursors.Cross); UpdateCursor(); m_moveHelper = new MoveHelper(this); m_nodeMoveHelper = new NodeMoveHelper(m_canvaswrapper); }
protected override void OnPaint(PaintEventArgs e) { CommonTools.Tracing.StartTrack(Program.TracePaint); 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"); }
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) { 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(); } }
void RepaintSnappoint(ISnapPoint snappoint) { if (snappoint == null) return; CanvasWrapper dc = new CanvasWrapper(this, Graphics.FromHwnd(Handle), ClientRectangle); snappoint.Draw(dc); dc.Graphics.Dispose(); dc.Dispose(); }
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(); }
/// <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"); }
public NodeMoveHelper(CanvasWrapper canvas) { m_canvas = canvas; }
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(); } }
private void Trace(ModuleItems.Module m, List<ModuleItems.Module> l) { CanvasWrapper c = new CanvasWrapper(m_canvas); List<ModuleItems.Module> toremove = new List<ModuleItems.Module>(); foreach (ModuleItems.Property p in m.Properties) if (p.name == "name") foreach (ModuleItems.Module n in l) foreach (ModuleItems.Property q in n.Properties) if ((q.name == "from" && q.value.ToString() == p.value.ToString()) || (q.name == "parent" && q.value.ToString() == p.value.ToString())) toremove.Add(n); foreach (ModuleItems.Property p in m.Properties) if (p.name == "to") foreach (ModuleItems.Module n in l) foreach (ModuleItems.Property q in n.Properties) if ((q.name == "name" && q.value.ToString() == p.value.ToString())) toremove.Add(n); for(int i = 0;i <toremove.Count;i++) { int spacerX = 0; int spacerY = 0; while (m_canvas.Model.GetHitObjects(c, new UnitPoint(m.ToPoint.X + spacerX * 2, m.ToPoint.Y - 1 - spacerY*2)).Count > 0) if (spacerX++ > 5) { spacerY++; spacerX = 0; } toremove[i].FromPoint = toremove[i].StartPoint = toremove[i].ToPoint = toremove[i].EndPoint = m.ToPoint; if (toremove[i].tofrom) { toremove[i].StartPoint = new UnitPoint(m.ToPoint.X + spacerX * 2, m.ToPoint.Y - 1 - spacerY * 2); toremove[i].EndPoint = new UnitPoint(m.ToPoint.X + 1 + spacerX * 2, m.ToPoint.Y - 1 - spacerY * 2); toremove[i].ToPoint = new UnitPoint(m.ToPoint.X + spacerX * 2, m.ToPoint.Y - 2 - spacerY * 2); } if (toremove[i].child) { toremove[i].StartPoint = new UnitPoint(m.StartPoint.X + spacerX * 2, m.StartPoint.Y - 1 - spacerY * 2); toremove[i].EndPoint = toremove[i].ToPoint = new UnitPoint(m.StartPoint.X + 1 + spacerX * 2, m.StartPoint.Y - 1 - spacerY * 2); } Model.AddObject(Model.ActiveLayer, toremove[i]); l.Remove(toremove[i]); } while (toremove.Count > 0) { Trace(toremove[0], l); toremove.Remove(toremove[0]); } }