using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Graphics_lab_2_dop
{
public partial class FormBackgroundScreen : Form
{
Rectangle rectSelected; //прямоугольник которым выделяют
Rectangle rectBack; //прямоугольник для заливки всего окна
Size size;
Point clickPoint;
Point nowPoint;
Graphics gr;
BufferedGraphics bufferGraphics;
BufferedGraphicsContext bufferGraphicsContext;
SolidBrush brushBack; //кисти
Pen pen;
Brush brushInRect;
public FormBackgroundScreen()
{
//InitializeComponent();
MouseDown += new MouseEventHandler(FormBackgroundScreen_MouseDown);
MouseMove += new MouseEventHandler(FormBackgroundScreen_MouseMove);
MouseUp += new MouseEventHandler(FormBackgroundScreen_MouseUp);
size = new Size(0, 0);
clickPoint = new Point(0, 0);
nowPoint = new Point(0, 0);
rectSelected = new Rectangle(nowPoint, size);
//окрашенное окно стало прозрачным внутри прямоугольника
brushBack = new SolidBrush(Color.Beige);
brushInRect = new SolidBrush(Color.BlanchedAlmond);
pen = new Pen(Color.DeepSkyBlue, 1); //то чем рисуется выделяемая область
rectBack = Screen.PrimaryScreen.Bounds;
this.BackColor = brushBack.Color;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
this.UpdateStyles();
gr = Graphics.FromHwnd(this.Handle);
bufferGraphicsContext = BufferedGraphicsManager.Current;
}
/// <summary>Отрисовываем всё в буфере </summary>
public void DrawToBuffer()
{ //выставляем размер буфера
bufferGraphicsContext.MaximumBuffer = new Size(this.Width, this.Height);
bufferGraphics = bufferGraphicsContext.Allocate(gr,
new Rectangle(0, 0, this.Width, this.Height)); //создаём буфер
bufferGraphics.Graphics.FillRectangle(brushBack, ClientRectangle);
//начинаем это делать именно здесь, потому-что нужно сдвигать только рамку
rectSelected = new Rectangle(rectSelected.X - 1, rectSelected.Y - 1, rectSelected.Width + 1, rectSelected.Height + 1);
bufferGraphics.Graphics.DrawRectangle(pen, rectSelected); //рисуем новый прямоугольник
//возвращаем всё на место как было
rectSelected = new Rectangle(rectSelected.X + 1, rectSelected.Y + 1, rectSelected.Width - 1, rectSelected.Height - 1);
bufferGraphics.Graphics.FillRectangle(brushInRect, rectSelected); //закрашиваем внутреннюю часть выделения
bufferGraphics.Render(); //выводим, то что отрисовалось в буфере
}
private void FormBackgroundScreen_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) //если нажата левая кнопка мыши
{
clickPoint.X = e.Location.X;
clickPoint.Y = e.Location.Y;
}
}
int PY = 0, PX = 0;
int LY = 0, LX = 0;
private void FormBackgroundScreen_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
nowPoint.X = e.X;
PX = e.X;
nowPoint.Y = e.Y;
PY = e.Y;
if (nowPoint.X > clickPoint.X)
{
size.Width = nowPoint.X - clickPoint.X; //вычитаем текущее место из места где был первый клик
rectSelected.X = clickPoint.X;
}
else
{
size.Width = clickPoint.X - nowPoint.X;
rectSelected.X = clickPoint.X - size.Width;
}
if (nowPoint.Y > clickPoint.Y)
{
size.Height = nowPoint.Y - clickPoint.Y;
rectSelected.Y = clickPoint.Y;
}
else
{
size.Height = clickPoint.Y - nowPoint.Y;
rectSelected.Y = clickPoint.Y - size.Height;
}
rectSelected.Width = size.Width;
rectSelected.Height = size.Height;
DrawToBuffer(); //Производим отрисовку в буфере
}
}
private void FormBackgroundScreen_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
LX = e.X;
LY = e.Y;
int xCoord = (PX - LX) / 2;
int yCoord = (PY - LY) / 2;
int a = PX - LX;
int b = PY - LY;
Circul(xCoord, yCoord, a, b);
}
}
private void Circul(int x, int y, int a, int b)
{
int col = 0, row = 0;
int a_square, b_square, two_a_square, two_b_square, four_a_square, four_b_square, d;
Graphics g = Graphics.FromHwnd(this.Handle);
Pen Brush = new Pen(new SolidBrush(Color.Blue), 2f);
b_square = b * b;
a_square = a * a;
row = b;
col = 0;
two_a_square = a_square << 1;
four_a_square = a_square << 2;
four_b_square = b_square << 2;
two_b_square = b_square << 1;
d = two_a_square * ((row - 1) * (row)) + a_square + two_b_square * (1 - a_square);
while (a_square * (row) > b_square * (col))
{
g.DrawRectangle(Brush, col + x, row + y, 1, 1);
g.DrawRectangle(Brush, col + x, y - row, 1, 1);
g.DrawRectangle(Brush, x - col, row + y, 1, 1);
g.DrawRectangle(Brush, x - col, y - row, 1, 1);
if (d >= 0)
{
row--;
d -= four_a_square * (row);
}
d += two_b_square * (3 + (col << 1));
col++;
}
d = two_b_square * (col + 1) * col + two_a_square * (row * (row - 2) + 1) + (1 - two_a_square) * b_square;
while (((row) + 1) > 0)
{
g.DrawRectangle(Brush, col + x, row + y, 1, 1);
g.DrawRectangle(Brush, col + x, y - row, 1, 1);
g.DrawRectangle(Brush, x - col, row + y, 1, 1);
g.DrawRectangle(Brush, x - col, y - row, 1, 1);
if (d <= 0)
{
col++;
d += four_b_square * col;
}
row--;
d += two_a_square * (3 - (row << 1));
}
}
}
}