This is main part of my C# code, this is a Windows Forms app.
My code works fine, and it runs the app very well, but I need to change it.
With this app, I can draw some lines in a picturebox, and after I draw the lines, then I can hide lines with press a Key "E" and I can show these lines with press the same key again.
When the lines are hidden, I can left click on the place of a drawn line by mouse, then the app shows that line, and I can hide this line again with a same key "E"
Now my problem is that I can not write a code that: when the mouse moves over the picturebox horizontally, I don't need to click, the app shows that line automatically and when the mouse passed through the picturebox on place of that line, then hide that line again and when mouse arrive to place of next line, app show that other line that mouse in on it and ...
What is your suggest to change the code?
This is my code:
My code works fine, and it runs the app very well, but I need to change it.
With this app, I can draw some lines in a picturebox, and after I draw the lines, then I can hide lines with press a Key "E" and I can show these lines with press the same key again.
When the lines are hidden, I can left click on the place of a drawn line by mouse, then the app shows that line, and I can hide this line again with a same key "E"
Now my problem is that I can not write a code that: when the mouse moves over the picturebox horizontally, I don't need to click, the app shows that line automatically and when the mouse passed through the picturebox on place of that line, then hide that line again and when mouse arrive to place of next line, app show that other line that mouse in on it and ...
What is your suggest to change the code?
This is my code:
C#:
namespace lines
{
public partial class Form1 : Form
{
private Graphics g;
private bool drawingMode = true, lineSelected;
private Pen semiTransPen;
List<Line> lines = new List<Line>();
public Form1()
{
InitializeComponent();
var pb = pictureBox1;
pb.Size = new Size(400, 400);
pb.Location = new Point(20, 20);
Line line = null;
pb.MouseMove += (s, e) =>
{
if (e.Button == MouseButtons.Left && drawingMode)
{
line.End = e.Location;
if (e.Location.Y < 0)
line.End = new Point(e.Location.X, 0);
else if (e.Location.Y > pb.Size.Height)
line.End = new Point(e.Location.X, pb.Size.Height);
if (e.Location.X < 0)
line.End = new Point(0, e.Location.Y);
else if (e.Location.X > pb.Size.Width)
line.End = new Point(pb.Size.Width, e.Location.Y);
pb.Invalidate();
}
};
pb.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
if (drawingMode)
line = new Line (e.Location, e.Location, Pens.Red );
else
{
lineSelected = false;
foreach (var a in lines)
{
if (a.OnLine(e.Location))
{
a.linePen = semiTransPen = new Pen(Color.FromArgb(75, 0, 0, 255), 5);
lineSelected = true;
}
else
a.linePen = semiTransPen = new Pen(Color.FromArgb(1, 0, 0, 255), 5);
}
pb.Invalidate();
}
}
};
pb.MouseUp += (s, e) =>
{
if (e.Button == MouseButtons.Left && drawingMode && line.Magnitude() > 1)
lines.Add(line);
};
pb.Paint += (s, e) =>
{
g = pb.CreateGraphics();
if (line != null)
e.Graphics.DrawLine(line.linePen, line.Start, line.End);
foreach (var l in lines)
e.Graphics.DrawLine(l.linePen, l.Start, l.End);
};
this.KeyPress += (s, e) =>
{
if (e.KeyChar == 'e')
{
if (drawingMode)
{
foreach (var a in lines)
{
a.linePen = semiTransPen = new Pen(Color.FromArgb(1, 0, 0, 255), 5);
}
}
else
{
foreach (var a in lines)
{
a.linePen = Pens.Red;
}
lineSelected = false;
}
drawingMode = !drawingMode;
pb.Invalidate();
}
};
}
class Line
{
public Line(Point s, Point e, Pen p)
{
Start = s;
End = e;
linePen = p;
}
public Line(string str)
{
Point s = new Point(0,0);
s.X = Int32.Parse(str.Substring(0, str.IndexOf(' ')));
str = str.Substring(str.IndexOf(' ') + 1);
s.Y = Int32.Parse(str.Substring(0, str.IndexOf(' ')));
str = str.Substring(str.IndexOf(' ') + 1);
Start = s;
s.X = Int32.Parse(str.Substring(0, str.IndexOf(' ')));
str = str.Substring(str.IndexOf(' ') + 1);
s.Y = Int32.Parse(str);
End = s;
linePen = Pens.Red;
}
public Pen linePen;
public Point Start { get; set; }
public Point End { get; set; }
public bool OnLine(Point p)
{
if (p == End || p == Start)
return true;
Point v1 = new Point(p.X - Start.X, p.Y - Start.Y);
Point v2 = new Point(p.X - End.X, p.Y - End.Y);
double d1 = v1.X * v2.X + v1.Y * v2.Y;
double d2 = Math.Sqrt(v1.X * v1.X + v1.Y * v1.Y) * Math.Sqrt(v2.X * v2.X + v2.Y * v2.Y);
d1 /= d2;
return (Math.Abs(d1 + 1) < 0.01);
}
public int Magnitude()
{
Point v1 = new Point(End.X - Start.X, End.Y - Start.Y);
return v1.X * v1.X + v1.Y * v1.Y;
}
public override string ToString()
{
return Start.X + " " + Start.Y + " " + End.X + " " + End.Y;
}
}
}
Last edited by a moderator: