//开始画图,更改文本及状态量 private void StartDrawingButton_Click(object sender, EventArgs e) { try { PolyLineLengthLabel.Text = "折线长度:NaN (m)"; ResultLabel.Text = "点位:"; DrawingTipLabel.Text = "左键绘点,右键撤销,双击或按F2结束绘制。请绘制第1个点,按Esc取消"; DrawGroupBox.Enabled = false; PolyLineCalcGroupBox.Enabled = false; FileGroupBox.Enabled = false; PlotPointPictureBox.Cursor = CrossCur; Line.Points.Clear(); EditStateButton.CheckState = CheckState.Unchecked; PolyLineDrawing.ClearPictureBox(PlotPointPictureBox); PlotPointPictureBox.Image = new Bitmap(PlotPointPictureBox.Width, PlotPointPictureBox.Height); IsDrawing = true; CurrentPoint = 0; PlotPointPictureBox.Cursor = CrossCur; EditStateButton.Enabled = false; } catch (Exception err) { MessageBoxes.Error(err.Message); } }
//计算线段对应比例点 private void CalcButton_Click(object sender, EventArgs e) { try { float Ratio = (float)(PolyLineRatioScrollBar.Value / 100.0); double ScaleRatio = Convert.ToDouble(RatioTextBox.Text); if (Ratio < 0 || Ratio > 1) { throw new Exception("所求线段比例非法!"); } else { PlotPoint Target = new PlotPoint(); //为所求点新建对象 Target = Algorithm.GetPointPos(Line.Points, Ratio); //计算所求点位置 ClearAndDraw(); PolyLineDrawing.DrawPoints(Target, PlotPointPictureBox, Algorithm.InverseColor(PointColorPictureBox.BackColor), PointWidthTrackBar.Value); PolyLineDrawing.DrawFonts(Target, "P", PlotPointPictureBox, FontColorPictureBox.BackColor, FontStyleDisplayLabel.Font, PointWidthTrackBar.Value); PlotPointPictureBox.Invalidate(); //计算并输出实际坐标系点位 PlotPoint Corrected = Algorithm.GetActualPos(PlotPointPictureBox, Target); ResultLabel.Text = "点位:(" + (Corrected.X * ScaleRatio / PlotPointPictureBox.Width).ToString("0.0000") + ", " + (Corrected.Y * ScaleRatio / PlotPointPictureBox.Width).ToString("0.0000") + ") (m)"; IsCalculated = true; } } catch (Exception err) { MessageBoxes.Error(err.Message); } }
//清空画板 private void ClearPictureBoxButton_Click(object sender, EventArgs e) { PolyLineDrawing.ClearPictureBox(PlotPointPictureBox); Line.Points.Clear(); DrawingTipLabel.Text = "已清空,点击“开始画线”开始绘制,点击“从文件读取折线”或直接拖拽数据文件到窗体以加载数据"; Restore(); }
//画图 private void PlotPictureBox_MouseClick(object sender, MouseEventArgs e) { //正在画图,根据鼠标点击键位增删点 if (IsDrawing) { //左键添加 if (e.Button == MouseButtons.Left) { Line.Points.Add(new PlotPoint { X = e.X, Y = e.Y }); CurrentPoint++; DrawingTipLabel.Text = "请绘制第" + Convert.ToString(CurrentPoint + 1) + "个点,或按Esc取消绘制,双击或按F2结束绘制"; ClearAndDraw(); } //右键删除 if (e.Button == MouseButtons.Right) { if (CurrentPoint == 0) { DrawingTipLabel.Text = "绘制被用户取消"; PolyLineDrawing.ClearPictureBox(PlotPointPictureBox); Line.Points.Clear(); PolyLineLengthLabel.Text = "折线长度:NaN (m)"; ResultLabel.Text = "点位:"; Restore(); } else { Line.Points.RemoveAt(CurrentPoint - 1); CurrentPoint--; DrawingTipLabel.Text = "撤销绘制,请绘制第" + Convert.ToString(CurrentPoint + 1) + "个点,或按Esc取消绘制"; ClearAndDraw(); } } } //编辑状态右键删除点 if (IsEditing && IsPointSelected && !IsPointMoving) { if (e.Button == MouseButtons.Right) { DeletePoint(); if (Line.Points.Count < 1) { SelectedPointIndex = -1; IsPointSelected = false; PlotPointPictureBox.Cursor = Cursors.No; DrawingTipLabel.Text = "点已被用户全部删除,请重新绘制或加载"; } } } }
private void KeyAction(object sender, KeyEventArgs e) { //ESC取消画图 if (e.KeyCode == Keys.Escape && IsDrawing) { PolyLineDrawing.ClearPictureBox(PlotPointPictureBox); Line.Points.Clear(); DrawingTipLabel.Text = "绘制被用户取消"; Restore(); } //F2结束绘图 if (e.KeyCode == Keys.F2 && IsDrawing) { if (CurrentPoint == 0) { DrawingTipLabel.Text = "绘制被用户取消"; PolyLineDrawing.ClearPictureBox(PlotPointPictureBox); Line.Points.Clear(); PolyLineLengthLabel.Text = "折线长度:NaN (m)"; ResultLabel.Text = "点位:"; Restore(); } else { StopDrawing(); } } //S开始绘图 if (e.KeyCode == Keys.S && !IsDrawing) { StartDrawingButton.PerformClick(); } //C清空画板 if (e.KeyCode == Keys.C && !IsDrawing) { ClearPictureBoxButton.PerformClick(); } //E编辑 if (e.KeyCode == Keys.E && !IsDrawing) { EditStateButton.PerformClick(); } //编辑状态下Del删除点 if (e.KeyCode == Keys.Delete && IsEditing && IsPointSelected && !IsPointMoving) { DeletePoint(); } }
//窗口加载,进行初始化操作 private void PolyLinePlotPoints_Load(object sender, EventArgs e) { try { FilePathTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.Personal); PolyLineCalcGroupBox.Enabled = false; SaveToPicButton.Enabled = false; PolyLineDrawing.ClearPictureBox(PlotPointPictureBox); //启动时清空画板 SettingsOperation(-1); } catch (Exception err) { MessageBoxes.Error(err.Message); } }
//清空并重绘 internal void ClearAndDraw() { PolyLineDrawing.ClearPictureBox(PlotPointPictureBox); if (IsPolygonCheckBox.Checked) { if (Line.Points.Count > 1) { PolyLineDrawing.DrawPolygon(Line.Points, PlotPointPictureBox, PolygonColorPictureBox.BackColor, PolygonTransparencyTrackBar.Value * 255 / (PolygonTransparencyTrackBar.Maximum - PolygonTransparencyTrackBar.Minimum)); } if (Line.Points.Count > 0) { PolyLineDrawing.DrawLines(Line.Points[Line.Points.Count - 1], Line.Points[0], PlotPointPictureBox, LineColorPictureBox.BackColor, LineWidthTrackBar.Value); } } PolyLineDrawing.Draw(Line.Points, PlotPointPictureBox, PointColorPictureBox.BackColor, LineColorPictureBox.BackColor, FontColorPictureBox.BackColor, FontStyleDisplayLabel.Font, LineWidthTrackBar.Value, PointWidthTrackBar.Value); //PlotPointPictureBox.Invalidate(); PlotPointPictureBox.Update(); //PlotPointPictureBox.Refresh(); UpdateLengthOrArea(); UpdateDataGridView(); }
//预览或编辑 private void PreviewOrEdit(object sender, MouseEventArgs e) { try { //预览 if (IsDrawing && IsPreviewing) { PlotPoint Preview = new PlotPoint { X = e.X, Y = e.Y }; ClearAndDraw(); if (Line.Points.Count > 0) { if (PolyLineCalcGroupBox.Visible) { PolyLineLengthLabel.Text = "折线长度:" + (((Algorithm.GetPolyLineLength(Line.Points) + Algorithm.GetSingleLineLength(Line.Points[Line.Points.Count - 1], Preview)) / (PlotPointPictureBox.Width)) * Convert.ToDouble(RatioTextBox.Text)).ToString("0.0000") + " (m)"; } if (IsPolygonCheckBox.Checked) { if (Line.Points.Count > 1) { PolyLineDrawing.DrawPolygon(Line.Points, PlotPointPictureBox, PolygonColorPictureBox.BackColor, PolygonTransparencyTrackBar.Value * 255 / (PolygonTransparencyTrackBar.Maximum - PolygonTransparencyTrackBar.Minimum), Preview); List <PlotPoint> PreviewArea = new List <PlotPoint>(); for (int i = 0; i < Line.Points.Count(); i++) { PreviewArea.Add(Line.Points[i]); } PreviewArea.Add(Preview); PolygonAreaLabel.Text = "多边形面积:" + (Algorithm.GetPolygonArea(PreviewArea, PlotPointPictureBox, Convert.ToSingle(RatioTextBox.Text))).ToString("0.0000") + "(m ^ 2)"; } PolyLineDrawing.DrawPreviewLine(Line.Points[0], Preview, PlotPointPictureBox, Algorithm.InverseColor(LineColorPictureBox.BackColor), LineWidthTrackBar.Value); } PolyLineDrawing.DrawPreviewLine(Line.Points[Line.Points.Count - 1], Preview, PlotPointPictureBox, Algorithm.InverseColor(LineColorPictureBox.BackColor), LineWidthTrackBar.Value); } PolyLineDrawing.DrawPoints(Preview, PlotPointPictureBox, Algorithm.InverseColor(PointColorPictureBox.BackColor), PointWidthTrackBar.Value); PolyLineDrawing.DrawFonts(Preview, Convert.ToString(Line.Points.Count + 1), PlotPointPictureBox, FontColorPictureBox.BackColor, FontStyleDisplayLabel.Font, PointWidthTrackBar.Value); PlotPointPictureBox.Invalidate(); } //编辑 if (!IsDrawing && IsEditing && !IsPointMoving && Line.Points.Count != 0) { PlotPointPictureBox.Cursor = Cursors.Arrow; List <double> Dis = new List <double>(); PlotPoint EditPos = new PlotPoint { X = e.X, Y = e.Y }; foreach (PlotPoint Point in Line.Points) { Dis.Add(Algorithm.GetSingleLineLength(EditPos, Point)); } //找最小距离点 double MinDistance = Dis[0]; int MinDistanceIndex = 0; for (int i = 0; i < Dis.Count; i++) { if (Dis[i] < MinDistance) { MinDistance = Dis[i]; MinDistanceIndex = i; } } //小于容差则选中 if (MinDistance < Convert.ToDouble(ToleranceTextBox.Text)) { IsPointSelected = true; PlotPointPictureBox.Cursor = CrossCur; SelectedPointIndex = MinDistanceIndex; DrawingTipLabel.Text = "已选中第" + Convert.ToString(SelectedPointIndex + 1) + "个点,按住鼠标左键拖动以改变位置,按Del或鼠标右键删除"; PolyLineDrawing.DrawPoints(Line.Points[SelectedPointIndex], PlotPointPictureBox, Algorithm.InverseColor(PointColorPictureBox.BackColor), Convert.ToInt32(PointWidthTrackBar.Value * 1.5)); PlotPointPictureBox.Refresh(); } else { if (SelectedPointIndex != -1) { ClearAndDraw(); } DisSelect(); DrawingTipLabel.Text = "未选中任何点"; } } //移动选中点 if (!IsDrawing && IsEditing && IsPointMoving && Line.Points.Count != 0) { if (e.X >= 0 && e.Y >= 0 && e.X <= PlotPointPictureBox.Width && e.Y <= PlotPointPictureBox.Height) { Line.Points[SelectedPointIndex].X = e.X; Line.Points[SelectedPointIndex].Y = e.Y; ClearAndDraw(); DrawingTipLabel.Text = "正在移动第" + Convert.ToString(SelectedPointIndex + 1) + "个点"; } else { DisSelect(); DrawingTipLabel.Text = "焦点丢失"; } } } catch (Exception err) { MessageBoxes.Error(err.Message); } }