private void canvas_Paint(object sender, PaintEventArgs e) { SolidBrush backBrush = new SolidBrush(BackColor); e.Graphics.FillRectangle(backBrush, canvas.ClientRectangle); backBrush.Dispose(); Rectangle a = new Rectangle(10, 20, 30, 30); Graphics g = e.Graphics; for (int i = 0; i < _figures.Count; i++) { Figure fig = _figures.getAt(i); fig.draw(g); } if (canvas.Image != null) { Rectangle r = canvas.ClientRectangle; Graphics g1 = e.Graphics; Graphics g2 = Graphics.FromImage(canvas.Image); IntPtr dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); BitBlt(dc2, 0, 0, r.Width, r.Height, dc1, 0, 0, 0x00CC0020); g1.ReleaseHdc(dc1); g2.ReleaseHdc(dc2); g2.Dispose(); } }
private void canvas_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { _selectedFigure = null; for (int i = 0; i < _figures.Count; i++) { Figure fig = _figures.getAt(i); if (fig.ptInRegion(e.X, e.Y)) { _selectedFigure = fig; break; } } if (_selectedFigure != null) { _selectedFigure.popup(canvas, e.Location); } else { mainPopup.popup(canvas, e.Location); } } else { _selectedFigure = null; for (int i = 0; i < _figures.Count; i++) { Figure fig = _figures.getAt(i); if (fig.ptInRegion(e.X, e.Y)) { _selectedFigure = fig; break; } } if (_selectedFigure != null) { _figures.removeFigure(_selectedFigure); _currentX = e.X; _currentY = e.Y; _actionMode = MOVING; canvas.Invalidate(); return; } Graphics g = canvas.CreateGraphics(); Pen aPen = new Pen(Color.Black, 2); if (whatToDraw == DRAW_RECT) { _selectedFigure = new Box(canvas, e.X, e.Y, e.X, e.Y); _selectedFigure.setPopup(boxPopup); } else if (whatToDraw == DRAW_LINE) { _selectedFigure = new Line(canvas, e.X, e.Y, e.X, e.Y); _selectedFigure.setPopup(linePopup); } else if (whatToDraw == DRAW_CIRCLE) { _selectedFigure = new Circle(canvas, e.X, e.Y, e.X, e.Y); _selectedFigure.setPopup(circlePopup); } else if (whatToDraw == DRAW_POINT) { _selectedFigure = new Point(canvas, e.X, e.Y); _selectedFigure.setPopup(pointPopup); } _selectedFigure.draw(g); bMousePressed = true; _actionMode = DRAWING; } }