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<Drawable> drawList = new List<Drawable>();
Drawable curent;
Color curentColor=Color.FromArgb(0,0,0);
bool MouseDown1=false;
Rectangle sqare;
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
{
for (int i = 0; i < drawList.Count;i++ )
{
if (
isInArea(drawList[i], new Point(e.Location.X, e.Location.Y)))
{
curent = drawList[i];
sqare.Location = new Point(curent.First.X + curent.width, curent.First.Y + curent.height);
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;
}
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 = e.Location;
curent.changeShape(sqare.Location.X - curent.First.X, sqare.Location.Y - curent.First.Y);
refresh();
curent.draw(g);
}
}
}
}
}
}