I'm using the following code to draw a selection rectangle over a picturebox and allow the user to select and drag it to the desired position.
What I intend to achieve is to allow the user to adjust the rectangle size by implementing option to adjust the rectangle size. Currently I have managed to achieve the following.
How to solve this issue?
public class DraggablePictureBox : PictureBox
{
Boolean hit1 = false, hit2 = false;
public Boolean notagimg = true;
public Boolean editedflag = false;
public Boolean notext = false;
public Boolean tdrawflag = false, tdrawflag2 = false;
Bitmap l;
public Form1 LaunchOrigin2 { get; set; }
public Point point = new Point(0, 0);
public Point point2 = new Point(0, 0);
public int sizemode = 1;
public DraggablePictureBox()
{
this.Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
this.Cursor = Cursors.Arrow;
if (e.Button == MouseButtons.Left)
{
point = e.Location;
base.OnMouseDown(e);
this.Invalidate();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
PointF x = new PointF(e.X, e.Y);
Pen p = new Pen(Brushes.Red, 2f);
RectangleF rect2 = new RectangleF(1, 1, 1, 1);
RectangleF rect = new RectangleF(1, 1, 1, 1);
if (e.Button == MouseButtons.Left)
{
this.Cursor = Cursors.Hand;
//Creating Rectangles to check to find the selected object
if (notext == false)
{
rect = new RectangleF(point, new Size(400,400));
}
if (rect.Contains(x) && notext == false)
{
hit1 = true;
}
if (hit1 == true )
{
this.Invalidate();
}
this.Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
tdrawflag = false;
}
if (e.Button == MouseButtons.Left)
{
point = e.Location;
base.OnMouseDown(e);
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Pen p = new Pen(Brushes.Red, 5);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Pen p2 = new Pen(Brushes.LightYellow, 5);
p2.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
if (!hit1)
{
pe.Graphics.DrawRectangle(p, new Rectangle(point, new Size(400, 400)));
}
else
{
pe.Graphics.DrawRectangle(p2, new Rectangle(point, new Size(400, 400)));
hit1 = false;
}
}
}
rect.contains
in other cases. – Ful