/// <summary> /// 获取鼠标点击时的坐标(界面—>点),并排序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CurveControl_MouseDown(object sender, MouseEventArgs e) { if (e.X < _workRect.Left || e.X > _workRect.Right || e.Y > _workRect.Bottom || e.Y < _workRect.Top) { return; } //左键按下时新增控制点 if (e.Button == MouseButtons.Left) { //判断路径是否包含鼠标点,包含则将选中点赋给当前点,不包含则重新出创建当前点 foreach (PixelControlPoint pt in _pixelControlPoints) { if (pt.Path.IsVisible(e.X, e.Y)) { _currentPixelPoint = pt; return; } } AddControlPoint(e.X, e.Y); } //右键按下时删除控制点 if (e.Button == MouseButtons.Right) { DeleteControlPoint(e.X, e.Y); } }
/// <summary> /// 鼠标弹起时设置当前值为空 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CurveControl_MouseUp(object sender, MouseEventArgs e) { if (_currentPixelPoint != null) { _currentPixelPoint = null; } Invalidate(); }
/// <summary> /// 创建当前控制点 /// </summary> /// <param name="x"></param> /// <param name="y"></param> private void CreatCurrentPoint(int x, int y) { _currentPixelPoint = new PixelControlPoint(); _currentPixelPoint.Location = new Point(x, y); GraphicsPath path = new GraphicsPath(); path.AddEllipse(_currentPixelPoint.Location.X - POINT_RADIUS, _currentPixelPoint.Location.Y - POINT_RADIUS, POINT_RADIUS << 1, POINT_RADIUS << 1); _currentPixelPoint.Path = path; }
private void InitControlPointList() { PixelControlPoint beginPoint = new PixelControlPoint(); beginPoint.Location = new Point(OFFSET + BLANK, Height - OFFSET - BLANK); PixelControlPoint endPoint = new PixelControlPoint(); endPoint.Location = new Point(ClientRectangle.Right - BLANK, BLANK); _pixelControlPoints.Add(beginPoint); _pixelControlPoints.Add(endPoint); }
/// <summary> /// 鼠标拖动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CurveControl_MouseMove(object sender, MouseEventArgs e) { //鼠标没有按下时,当鼠标放在已有控制点上方时,将鼠标位置传给当前点; if (e.Button == System.Windows.Forms.MouseButtons.None) { foreach (PixelControlPoint pt in _pixelControlPoints) { if (pt.Path == null) { continue; } if (pt.Path.IsVisible(e.X, e.Y)) { _currentPixelPoint = pt; Invalidate(); return; } } _currentPixelPoint = null; Invalidate(); return; } //当鼠标已经按下时,设置鼠标拖动的效果 if (_currentPixelPoint != null) { //判断按下时选中的点是否为两个端点,是则x值不变,y改变(只能上下拖动);否则重新赋值给当前值 if (_pixelControlPoints[0] == _currentPixelPoint || _pixelControlPoints[_pixelControlPoints.Count - 1] == _currentPixelPoint) { if (e.Y < _workRect.Bottom && e.Y > _workRect.Top) { _currentPixelPoint.Location.Y = e.Y; } } else { if (e.X > _workRect.Left && e.X < _workRect.Right && e.Y < _workRect.Bottom && e.Y > _workRect.Top) { _currentPixelPoint.Location.X = e.X; _currentPixelPoint.Location.Y = e.Y; } } _pixelControlPoints.Sort(new PixelControlPointXComparer()); Invalidate(); TryApply(); } }
/// <summary> /// 删除控制点 /// </summary> /// <param name="x"></param> /// <param name="y"></param> internal void DeleteControlPoint(int x, int y) { foreach (PixelControlPoint pt in _pixelControlPoints) { if (pt.Path.IsVisible(x, y)) { if (_pixelControlPoints[0] == pt || _pixelControlPoints[_pixelControlPoints.Count - 1] == pt) { return; } _pixelControlPoints.Remove(pt); _currentPixelPoint = pt; break; } } CreatCurrentPoint(x, y); _pixelControlPoints.Sort(new PixelControlPointXComparer()); Invalidate(); TryApply(); }