using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DPumpHydr.WinFrmUI.Volute { public partial class DesignDraw1 : UserControl { public DesignDraw1() { Point start = new Point(20, 120); Point control1 = new Point(40, 90); Point control2 = new Point(60, 150); Point end1 = new Point(100, 180); Point control3 = new Point(120, 170); Point control4 = new Point(135, 190); Point end2 = new Point(150, 220); screenPoints = new List { start, control1, control2, end1, control3, control4, end2 }; } List screenPoints; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; if (_dragPointNO >= 0) { using (Pen penUn = new Pen(Color.Gray, 2f)) using (SolidBrush penSelect = new SolidBrush(Color.Red)) { for (int i = 0; i < screenPoints.Count; i++) { var pt = screenPoints[i]; if (i == _dragPointNO) g.FillEllipse(penSelect, pt.X - 4f, pt.Y - 4f, 8, 8); else g.DrawEllipse(penUn, pt.X - 4f, pt.Y - 4f, 8, 8); } } } else { using (SolidBrush penBrush = new SolidBrush(Color.Black)) { foreach (var pt in screenPoints) { g.FillEllipse(penBrush, pt.X - 4f, pt.Y - 4f, 8, 8); } } } using (Pen penCurve = new Pen(Color.Blue, 2f)) { g.DrawBeziers(penCurve, screenPoints.ToArray()); } } int _dragPointNO = -1; protected override void OnMouseClick(MouseEventArgs e) { base.OnMouseClick(e); } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); this.Cursor = Cursors.Default; if (_dragPointNO >= 0) { //鼠标未移除控件范围 if (this.ClientRectangle.Contains(e.Location)) { screenPoints[_dragPointNO] = e.Location; Invalidate(); } } _dragPointNO = -1; } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); _dragPointNO = -1; this.Cursor = Cursors.Default; //判断是否在点周围 Rectangle r = new Rectangle(e.X - 5, e.Y - 5, 10, 10); for (int i = 0; i < screenPoints.Count; i++) { if (r.Contains(screenPoints[i])) { _dragPointNO = i; this.Cursor = Cursors.Hand; Invalidate(); break; } } } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (_dragPointNO >= 0) { //鼠标未移除控件范围 if (this.ClientRectangle.Contains(e.Location)) { screenPoints[_dragPointNO] = e.Location; Invalidate(); } } } } }