using System; using System.Drawing; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class Form1 : Form { Graphics DrawBox; Point First; PaintEventArgs e; Graphics g; bool editingMode = false; List drawList = new List(); List clicked = new List(); Drawable curent; Color curentColor=Color.FromArgb(0,0,0); bool MouseDown1=false; Rectangle sqare; Drawable a1 = null; bool sqareClicked = false; string curentShape="Линия"; public Form1() { InitializeComponent(); g =CreateGraphics(); } private void StartDraw(object sender, MouseEventArgs e) { if (!editingMode) { if (!MouseDown1) { First = new Point(e.Location.X, e.Location.Y); MouseDown1 = true; } switch(curentShape) { case "Линия": { drawList.Add(new Line1(new Point(e.Location.X, e.Location.Y), new Pen(curentColor, WidthBar.Value))); break; } case "Квадрат": { drawList.Add(new Rectangle1(new Point(e.Location.X, e.Location.Y), new Pen(curentColor, WidthBar.Value))); break; } case "Круг": { drawList.Add(new circle(new Point(e.Location.X, e.Location.Y), new Pen(curentColor, WidthBar.Value))); break; } } } else { clicked.Clear(); for (int i = 0; i < drawList.Count; i++) { if (isInArea(drawList[i], new Point(e.Location.X, e.Location.Y))) { clicked.Add(drawList[i]); } } if (clicked.Count > 0) { int minwidth=999; foreach(var a in clicked) { if (a.width < minwidth) { minwidth = a.width; a1 = a; } } curent = a1; sqare.Location = new Point(curent.First.X + curent.width-10, curent.First.Y + curent.height-10); sqare.Width = 10; sqare.Height = 10; MouseDown1 = true; g.DrawRectangle(Pens.Red, sqare); } if (isInArea(sqare, e.Location)) { MouseDown1 = true; sqareClicked = true; } else sqareClicked = false; } } protected override void OnPaint(PaintEventArgs e) { this.e = e; base.OnPaint(e); DrawBox = e.Graphics; } private void Drawing(object sender, EventArgs e) { draw(); } public void draw() { //if (mouse != null) //{ // Rectangle a = new Rectangle(0, curent.Location.Y, 10, 10); // DrawBox.DrawEllipse(Pens.Black, a); //} } private void button1_Click(object sender, EventArgs e) { editingMode =!editingMode; curent = null; } public void refresh() { g.Clear(Color.White); foreach (Drawable a in drawList) a.draw(g); if (sqare != null) g.DrawRectangle(Pens.Red, sqare); } private bool isInArea(Rectangle area, Point p1) { bool width, height; if (area.Width < 0) { if (p1.X >= area.Location.X + area.Width && p1.X <= area.Location.X) { width = true; } else width = false; } else { if (p1.X >= area.Location.X && p1.X <= area.Location.X + area.Width) { width = true; } else width = false; } if (area.Height < 0) { if (p1.Y >= area.Location.Y + area.Height && p1.Y <= area.Location.Y) { height = true; } else height = false; } else { if (p1.Y >= area.Location.Y && p1.Y <= area.Location.Y + area.Height) { height = true; } else height = false; } if (width && height) return true; return false; } private bool isInArea(Drawable area, Point p1) { bool width,height; if (area.width < 0) { if (p1.X >= area.First.X + area.width && p1.X <= area.First.X) { width = true; } else width = false; } else { if (p1.X >= area.First.X && p1.X <= area.First.X + area.width) { width = true; } else width = false; } if (area.height < 0) { if (p1.Y >= area.First.Y + area.height && p1.Y <= area.First.Y) { height = true; } else height = false; } else { if (p1.Y >= area.First.Y && p1.Y <= area.First.Y + area.height) { height = true; } else height = false; } if (width && height) return true; return false; } private void MUHUC(object sender, EventArgs e) { if (curent != null) { curent.width -= 5; curent.height -= 5; Update(); curent.update(); refresh(); curent.draw(g); } } private void PILUS(object sender, EventArgs e) { if (curent != null) { curent.width += 5; curent.height += 5; curent.update(); refresh(); curent.draw(g); } } private void ChangeColor(object sender, EventArgs e) { curentColor = Color.FromArgb(red.Value, green.Value, blue.Value); Prewiev.BackColor = curentColor; Prewiev.Height = WidthBar.Value; } private void SetShape(object sender, EventArgs e) { curentShape = ((Button)sender).Text; } private void CleanAll(object sender, EventArgs e) { g.Clear(Color.White); drawList.Clear(); } private void MouseUp1(object sender, MouseEventArgs e) { MouseDown1 = false; if(!editingMode) if (e.Location.X - drawList[drawList.Count - 1].First.X < 3 && e.Location.Y - drawList[drawList.Count - 1].First.Y < 3) drawList.RemoveAt(drawList.Count - 1); } private void MouseMove1(object sender, MouseEventArgs e) { if (MouseDown1) { if (!editingMode ) { drawList[drawList.Count - 1].changeShape(e.Location.X - First.X, e.Location.Y - First.Y); drawList[drawList.Count - 1].draw(g); refresh(); } else { if (!sqareClicked) { curent.setPosition(e.Location); refresh(); } else { sqare.Location = new Point(e.Location.X-10,e.Location.Y-10); curent.changeShape(sqare.Location.X - curent.First.X+10, sqare.Location.Y - curent.First.Y+10); refresh(); curent.draw(g); } } } } } }