//开始画图,更改文本及状态量 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 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(); }