As the title says I just need to correctly resize and reposition the control if I drag the 2 ellipse handles inside the custom control I made. I can't seem to figure out how to do it.
In the GIF above the my control is the one with yellow background and it's having a problem when I start moving the 2nd handle outside the 4th quadrant. It needs to correctly reposition and resize the control.
Code:
public partial class FlexibleLineControl : Control
{
public FlexibleLineControl()
{
// transparent control: https://www.c-sharpcorner.com/uploadfile/Nildo/making-transparent-control-using-gdi-and-C-Sharp-updated-to-net-3-5/
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.PaleGoldenrod;
InitializeComponent();
this.Size = new Size(HandleSize.Width * 2, HandleSize.Height * 2);
Handle1 = new Rectangle(new Point(0, 0), HandleSize);
Handle2 = new Rectangle(new Point(HandleSize.Width, HandleSize.Height), HandleSize);
GP1.AddEllipse(Handle1);
GP2.AddEllipse(Handle2);
DoubleBuffered = true;
}
private GraphicsPath GP1 = new GraphicsPath();
private GraphicsPath GP2 = new GraphicsPath();
private Size HandleSize = new Size(10, 10);
public Rectangle Handle1;
private Rectangle Handle2;
private Point CurrMousePoint1;
private bool DragState = false;
private bool Handle1Dragged = false;
private bool Handle2Dragged = false;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillEllipse(Brushes.Black, Handle1);
e.Graphics.FillEllipse(Brushes.Black, Handle2);
// get line points to draw line
Point p1 = new Point(Handle1.Location.X + Handle1.Width / 2, Handle1.Location.Y + Handle1.Height / 2);
Point p2 = new Point(Handle2.Location.X + Handle2.Width / 2, Handle2.Location.Y + Handle2.Height / 2);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawLine(new Pen(Brushes.Black), p1, p2);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (GP1.IsVisible(e.Location))
{
DragState = true;
Handle1Dragged = true;
CurrMousePoint1 = this.Parent.PointToClient(Cursor.Position);
}
else if (GP2.IsVisible(e.Location))
{
DragState = true;
Handle2Dragged = true;
CurrMousePoint1 = this.Parent.PointToClient(Cursor.Position);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (DragState)
{
Point newPoint = Parent.PointToClient(Cursor.Position);
if (Handle1Dragged)
Handle1.Location = new Point(Handle1.X + newPoint.X - CurrMousePoint1.X, Handle1.Y + newPoint.Y - CurrMousePoint1.Y);
else if (Handle2Dragged)
Handle2.Location = new Point(Handle2.X + newPoint.X - CurrMousePoint1.X, Handle2.Y + newPoint.Y - CurrMousePoint1.Y);
// get new control size and location
int width = Math.Abs(Handle1.Location.X - Handle2.Location.X);
int height = Math.Abs(Handle1.Location.Y - Handle2.Location.Y);
this.Size = new Size(width + HandleSize.Width, height + HandleSize.Height);
CurrMousePoint1 = newPoint;
Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
DragState = false;
if (Handle1Dragged)
{
GP1.Reset();
GP1.AddRectangle(Handle1);
Handle1Dragged = false;
}
else if (Handle2Dragged)
{
GP2.Reset();
GP2.AddRectangle(Handle2);
Handle2Dragged = false;
}
}
}