Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?

you can see my app here: https://i.stack.imgur.com/fZySn.gif[^]

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;
            }
        }
    }


What I have tried:

please write your suggest, that i can use from my application as i want
Posted
Updated 22-Jun-21 19:45pm
v2

1 solution

You are already handling MouseMove and you know how to draw your lines - so it's just a case of handling the "else" in the MouseMove handler to call your OnLine method to check if you are over a line.
The only real complication is that you need to have a way to "hide" the line again when you move off it, but that's just a case of remembering which line you showed last time and hiding it.

To be honest, given that you wrote that code I can't see why you have a problem implementing this change - you have all the bits you require already, it's just a case of "hooking them up" slightly differently.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900