using System using System Collections Generic using System ComponentMo

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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));
}
}
}
}