private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(Color.White); RectanglesDoc.Draw(e.Graphics); if (_drawingContour && _startPoint != Point.Empty) { var pen = new Pen(Color.Gray, 2); pen.DashStyle = DashStyle.Dot; var position = GetPosition(); if (_ctrlPressed) // draw a contour square { int max = Math.Max(_width, _height); e.Graphics.DrawRectangle(pen, position.X, position.Y, max, max); } else // draw a contour rectangle { e.Graphics.DrawRectangle(pen, position.X, position.Y, _width, _height); } pen.Dispose(); } if (Filename != string.Empty) { this.Text = $"FDraw | {Filename.Substring(Filename.LastIndexOf(@"\") + 1)}"; } else { this.Text = "FDraw"; } }
public Form1() { InitializeComponent(); RectanglesDoc = new RectanglesDoc(); Color = Color.DarkBlue; Filename = string.Empty; this.DoubleBuffered = true; }
private void newToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure you want to start a new game?", "Start a new game", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { RectanglesDoc = new RectanglesDoc(); Filename = string.Empty; ResetValues(); Invalidate(true); } }
private void Form1_MouseDown(object sender, MouseEventArgs e) { mouseMoved = false; _mousePressed = true; _selected = RectanglesDoc.Select(e.Location); if (!_selected) { _startPoint = e.Location; _drawingContour = true; } else { _prevX = e.X; _prevY = e.Y; } }
private void Form1_MouseMove(object sender, MouseEventArgs e) { mouseMoved = true; if (!_mousePressed) { return; } if (_selected) { RectanglesDoc.Move(e.X - _prevX, e.Y - _prevY); _prevX = e.X; _prevY = e.Y; _drawingContour = false; } else { int diffX = e.Location.X - _startPoint.X; int diffY = e.Location.Y - _startPoint.Y; _width = Math.Abs(diffX); _height = Math.Abs(diffY); if (_ctrlPressed) // draw a contour square { int max = Math.Max(_width, _height); int xm = (diffX) < 0 ? -1 : 1; int ym = (diffY) < 0 ? -1 : 1; _endPoint = new Point(_startPoint.X + max * xm, _startPoint.Y + max * ym); } else // draw a contour rectangle { _endPoint = e.Location; } } Invalidate(); }
private void Form1_MouseUp(object sender, MouseEventArgs e) { _mousePressed = false; if (!_selected && mouseMoved) // nothing selected, add a shape { var position = GetPosition(); if (_ctrlPressed) // add a square { int max = Math.Max(_width, _height); RectanglesDoc.AddRectangle(position, max, max, Color); } else // add a rectangle { RectanglesDoc.AddRectangle(position, _width, _height, Color); } } _selected = false; RectanglesDoc.ClearSelected(); _startPoint = Point.Empty; _drawingContour = true; Invalidate(true); }