Как объединить два класса с разными обработчиками мыши
Подскажите пожалуйста!!!Мне нужно объединить два класса, у которых немного различаются обработчики мыши. Первый класс рисует круг по нажатиям мыши, другой класс рисует прямоугольник. Класс прямоугольника наследуется от класс круга. Я хочу по тому, какой CheckBox активен, рисовать нужную фигуру. Как мне это сделать? Код обоих классов представлен ниже.
partial class Ellipse : Form
{
protected virtual CheckBox checkBox1 { get; set; }
protected virtual int numberOfElipse { get; set; }
protected virtual int indexToResize { get; set; }
protected virtual Dictionary<int, List<Point>> dictOfPoints { get; set; }
protected virtual Dictionary<int, List<Point>> dictToMoveSides { get; set; }
protected virtual bool checkToResizeSides { get; set; }
protected virtual Point point { get; set; }
protected virtual Point newMousePosition { get; set; }
protected virtual Point movingMouse { get; set; }
protected virtual Point ChangeColorPoint { get; set; }
Pen pen = new Pen(Color.Black, 3);
public Ellipse()
{
this.Size = new Size(700, 500);
base.DoubleBuffered = true;
checkBox1 = new CheckBox();
checkBox1.Size = new Size(60, 20);
checkBox1.Location = new Point(290, 50);
checkBox1.Text = "Ellipse";
checkBox1.Parent = this;
}
protected virtual void Mouse_Down(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
point = newMousePosition = new Point(e.X, e.Y);
}
if (e.Button == MouseButtons.Left && !checkBox1.Checked)
{
movingMouse = new Point(e.X, e.Y);
}
}
protected virtual void Mouse_Move(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
newMousePosition = new Point(e.X, e.Y);
}
if (checkToResizeSides && e.Button == MouseButtons.Left)
{
if (indexToResize != int.MaxValue)
{
newMousePosition = dictOfPoints[indexToResize][1];
point = dictOfPoints[indexToResize][0];
int differenceX = movingMouse.X - e.X;
int differenceY = movingMouse.Y - e.Y;
int indexToMoveSide = dictToMoveSides[indexToResize].IndexOf(ChangeColorPoint);
Resize(indexToMoveSide, differenceX, differenceY);
movingMouse = new Point(e.X, e.Y);
}
Replace(indexToResize);
}
else if (!checkBox1.Checked)
{
ChangeColorPoint = FindNecessaryPoint(new Point(e.X, e.Y));
if (ChangeColorPoint != new Point(0, 0))
{
checkToResizeSides = true;
}
else
{
checkToResizeSides = false;
}
}
Invalidate();
}
protected virtual void Mouse_Up(MouseEventArgs e)
{
if (numberOfElipse == 0)
{
dictOfPoints = new Dictionary<int, List<Point>>();
dictToMoveSides = new Dictionary<int, List<Point>>();
}
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
dictOfPoints.Add(numberOfElipse, new List<Point>() { point, newMousePosition });
dictToMoveSides.Add(numberOfElipse, new List<Point>() {
new Point(newMousePosition.X, point.Y + (newMousePosition.Y - point.Y) / 2),
new Point(point.X + (newMousePosition.X - point.X) / 2, newMousePosition.Y),
new Point(point.X, point.Y + (newMousePosition.Y - point.Y) / 2),
new Point(point.X + (newMousePosition.X - point.X) / 2, point.Y)});
numberOfElipse++;
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
Mouse_Down(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
Mouse_Move(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
Mouse_Up(e);
}
protected virtual void On_Paint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
if (checkBox1.Checked)
graphics.DrawEllipse(pen, point.X, point.Y, newMousePosition.X - point.X, newMousePosition.Y - point.Y);
if (dictOfPoints != null)
{
foreach (var i in dictOfPoints.Keys)
{
graphics.DrawEllipse(pen, dictOfPoints[i][0].X, dictOfPoints[i][0].Y,
dictOfPoints[i][1].X - dictOfPoints[i][0].X, dictOfPoints[i][1].Y - dictOfPoints[i][0].Y);
}
}
if (ChangeColorPoint != new Point(0, 0))
{
graphics.FillRectangle(new SolidBrush(Color.Red), ChangeColorPoint.X - 3, ChangeColorPoint.Y - 3,
6, 6);
}
}
protected override void OnPaint(PaintEventArgs e)
{
On_Paint(e);
}
protected virtual Point FindNecessaryPoint(Point pt)
{
if (dictToMoveSides != null)
{
foreach (var i in dictToMoveSides.Keys)
{
foreach (var j in dictToMoveSides[i])
{
if (j.X + 15 > pt.X && j.X - 15 < pt.X && j.Y + 15 > pt.Y && j.Y - 15 < pt.Y)
{
indexToResize = i;
return new Point(j.X, j.Y);
}
}
}
}
indexToResize = int.MaxValue;
return new Point(0, 0);
}
protected virtual void Replace(int index)
{
dictToMoveSides.Remove(index);
dictToMoveSides.Add(index, new List<Point>() {
new Point(newMousePosition.X, point.Y + (newMousePosition.Y - point.Y) / 2),
new Point(point.X + (newMousePosition.X - point.X) / 2, newMousePosition.Y),
new Point(point.X, point.Y + (newMousePosition.Y - point.Y) / 2),
new Point(point.X + (newMousePosition.X - point.X) / 2, point.Y)});
}
protected virtual void Resize(int indexToMoveSide, int differenceX, int differenceY)
{
if (indexToMoveSide <= 1)
{
if (indexToMoveSide == 0)
{
differenceY = 0;
}
else if (indexToMoveSide == 1)
{
differenceX = 0;
}
dictOfPoints[indexToResize][1] = new Point(dictOfPoints[indexToResize][1].X - differenceX, dictOfPoints[indexToResize][1].Y - differenceY);
newMousePosition = new Point(newMousePosition.X - differenceX, newMousePosition.Y - differenceY);
ChangeColorPoint = new Point(ChangeColorPoint.X - differenceX, ChangeColorPoint.Y - differenceY);
}
else
{
if (indexToMoveSide == 2)
{
differenceY = 0;
}
else if (indexToMoveSide == 3)
{
differenceX = 0;
}
dictOfPoints[indexToResize][0] = new Point(dictOfPoints[indexToResize][0].X - differenceX, dictOfPoints[indexToResize][0].Y - differenceY);
point = new Point(point.X - differenceX, point.Y - differenceY);
ChangeColorPoint = new Point(ChangeColorPoint.X - differenceX, ChangeColorPoint.Y - differenceY);
}
}
}
partial class Rectangle : Ellipse
{
static int numberOfRectangle = 0;
protected override int indexToResize { get; set; }
protected override Dictionary<int, List<Point>> dictOfPoints { get; set; }
protected override Dictionary<int, List<Point>> dictToMoveSides { get; set; }
protected override bool checkToResizeSides { get; set; }
protected override CheckBox checkBox1 { get; set; }
protected override Point point { get; set; }
protected override Point newMousePosition { get; set; }
protected override Point movingMouse { get; set; }
protected override Point ChangeColorPoint { get; set; }
Pen pen = new Pen(Color.Black, 3);
public static void Main(string[] args)
{
Application.Run(new Ellipse());
}
public Rectangle():base()
{
checkBox1.Text = "Rectangle";
checkBox1.Size = new Size(100, 30);
checkBox1.Location = new Point(400, 50);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.Mouse_Down(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.Mouse_Move(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
Mouse_Up(e);
}
protected override void Mouse_Up(MouseEventArgs e)
{
if (numberOfRectangle == 0)
{
dictOfPoints = new Dictionary<int, List<Point>>();
dictToMoveSides = new Dictionary<int, List<Point>>();
}
if (e.Button == MouseButtons.Left && checkBox1.Checked)
{
dictOfPoints.Add(numberOfRectangle, new List<Point>() { point, newMousePosition });
dictToMoveSides.Add(numberOfRectangle, new List<Point>() {
new Point(newMousePosition.X, newMousePosition.Y),
new Point(point.X, point.Y)});
numberOfRectangle++;
}
}
protected override void OnPaint(PaintEventArgs e)
{
On_Paint(e);
}
protected override void On_Paint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
if (checkBox1.Checked)
graphics.DrawRectangle(pen, point.X, point.Y, newMousePosition.X - point.X, newMousePosition.Y - point.Y);
if (dictOfPoints != null)
{
foreach (var i in dictOfPoints.Keys)
{
graphics.DrawRectangle(pen, Math.Min(dictOfPoints[i][0].X, dictOfPoints[i][1].X),
Math.Min(dictOfPoints[i][0].Y, dictOfPoints[i][1].Y),
Math.Abs(dictOfPoints[i][1].X - dictOfPoints[i][0].X), Math.Abs(dictOfPoints[i][1].Y - dictOfPoints[i][0].Y));
}
}
if (ChangeColorPoint != new Point(0, 0))
{
graphics.FillRectangle(new SolidBrush(Color.Red), ChangeColorPoint.X - 3, ChangeColorPoint.Y - 3,
6, 6);
}
}
protected override void Replace(int index)
{
dictToMoveSides.Remove(index);
dictToMoveSides.Add(index, new List<Point>() {
new Point(newMousePosition.X, newMousePosition.Y),
new Point(point.X, point.Y)});
}
protected override Point FindNecessaryPoint(Point pt)
{
return base.FindNecessaryPoint(pt);
}
protected override void Resize(int indexToMoveSide, int differenceX, int differenceY)
{
if (indexToMoveSide == 0)
{
dictOfPoints[indexToResize][1] = new Point(dictOfPoints[indexToResize][1].X - differenceX, dictOfPoints[indexToResize][1].Y - differenceY);
newMousePosition = new Point(newMousePosition.X - differenceX, newMousePosition.Y - differenceY);
ChangeColorPoint = new Point(ChangeColorPoint.X - differenceX, ChangeColorPoint.Y - differenceY);
}
else
{
dictOfPoints[indexToResize][0] = new Point(dictOfPoints[indexToResize][0].X - differenceX, dictOfPoints[indexToResize][0].Y - differenceY);
point = new Point(point.X - differenceX, point.Y - differenceY);
ChangeColorPoint = new Point(ChangeColorPoint.X - differenceX, ChangeColorPoint.Y - differenceY);
}
}
}