using
System;
using
System.Drawing;
using
System.Windows.Forms;
namespace
WindowsFormsApp1
{
public
partial
class
Form1 : Form
{
private
Image image;
private
Rectangle imageRect;
private
bool
isDragging;
private
Point dragStart;
private
Point dragOrigin;
private
Size dragSize;
private
DragType dragType;
private
PictureBox pictureBox;
private
Point[] clickablePoints =
new
Point[]
{
new
Point(20, 20),
new
Point(80, 50),
new
Point(140, 100)
};
public
Form1()
{
InitializeComponent();
LoadImage();
CreatePictureBox();
}
private
enum
DragType
{
None,
Move,
ResizeLeft,
ResizeRight,
ResizeTop,
ResizeBottom
}
private
void
CreatePictureBox()
{
pictureBox =
new
PictureBox
{
Dock = DockStyle.Fill,
SizeMode = PictureBoxSizeMode.Normal
};
this
.Controls.Add(pictureBox);
pictureBox.Paint +=
new
PaintEventHandler(PictureBox_Paint);
pictureBox.MouseDown +=
new
MouseEventHandler(PictureBox_MouseDown);
pictureBox.MouseMove +=
new
MouseEventHandler(PictureBox_MouseMove);
pictureBox.MouseUp +=
new
MouseEventHandler(PictureBox_MouseUp);
pictureBox.MouseLeave +=
new
EventHandler(PictureBox_MouseLeave);
}
private
void
LoadImage()
{
image = Image.FromFile(
"001.jpg"
);
imageRect =
new
Rectangle(0, 0, image.Width, image.Height);
}
private
void
PictureBox_Paint(
object
sender, PaintEventArgs e)
{
e.Graphics.DrawImage(image, imageRect);
var
pen = Pens.Black;
e.Graphics.DrawRectangle(pen, imageRect);
var
pointBrush = Brushes.Red;
foreach
(
var
point
in
clickablePoints)
{
Point adjustedPoint =
new
Point(imageRect.Left + point.X, imageRect.Top + point.Y);
e.Graphics.FillEllipse(pointBrush, adjustedPoint.X - 5, adjustedPoint.Y - 5, 10, 10);
}
}
private
void
PictureBox_MouseDown(
object
sender, MouseEventArgs e)
{
for
(
int
i = 0; i < clickablePoints.Length; i++)
{
Point adjustedPoint =
new
Point(imageRect.Left + clickablePoints[i].X, imageRect.Top + clickablePoints[i].Y);
if
(IsPointInCircle(e.Location, adjustedPoint, 5))
{
OpenForm(i);
return
;
}
}
if
(e.Button == MouseButtons.Left)
{
isDragging =
true
;
dragStart = e.Location;
dragOrigin = imageRect.Location;
dragSize = imageRect.Size;
dragType = DetermineDragType(e.Location);
}
}
private
void
OpenForm(
int
index)
{
if
(index == 0)
{
Form2 form2 =
new
Form2();
form2.Show();
}
if
(index == 1)
{
Form3 form3 =
new
Form3();
form3.Show();
}
if
(index == 2)
{
Form3 form3 =
new
Form3();
form3.Show();
}
}
private
bool
IsPointInCircle(Point mousePoint, Point center,
int
radius)
{
return
Math.Pow(mousePoint.X - center.X, 2) + Math.Pow(mousePoint.Y - center.Y, 2) <= Math.Pow(radius, 2);
}
private
DragType DetermineDragType(Point location)
{
const
int
handleSize = 10;
if
(location.X < imageRect.Left + handleSize)
return
DragType.ResizeLeft;
if
(location.X > imageRect.Right - handleSize)
return
DragType.ResizeRight;
if
(location.Y < imageRect.Top + handleSize)
return
DragType.ResizeTop;
if
(location.Y > imageRect.Bottom - handleSize)
return
DragType.ResizeBottom;
return
DragType.Move;
}
private
void
PictureBox_MouseMove(
object
sender, MouseEventArgs e)
{
if
(isDragging)
{
int
deltaX = e.X - dragStart.X;
int
deltaY = e.Y - dragStart.Y;
switch
(dragType)
{
case
DragType.Move:
imageRect.X = dragOrigin.X + deltaX;
imageRect.Y = dragOrigin.Y + deltaY;
break
;
case
DragType.ResizeLeft:
imageRect.X = dragOrigin.X + deltaX;
imageRect.Width = dragSize.Width - deltaX;
break
;
case
DragType.ResizeRight:
imageRect.Width = dragSize.Width + deltaX;
break
;
case
DragType.ResizeTop:
imageRect.Y = dragOrigin.Y + deltaY;
imageRect.Height = dragSize.Height - deltaY;
break
;
case
DragType.ResizeBottom:
imageRect.Height = dragSize.Height + deltaY;
break
;
}
if
(imageRect.Width < 10)
{
imageRect.Width = 10;
}
if
(imageRect.Height < 10)
{
imageRect.Height = 10;
}
pictureBox.Invalidate();
}
}
private
void
PictureBox_MouseUp(
object
sender, MouseEventArgs e)
{
if
(isDragging)
{
isDragging =
false
;
pictureBox.Invalidate();
}
}
private
void
PictureBox_MouseLeave(
object
sender, EventArgs e)
{
if
(isDragging)
{
isDragging =
false
;
pictureBox.Invalidate();
}
}
}
}