adityatan's picture

Highlight a line when the cursor is on the line

Hello:

How do I highlight a line when the cursor is on it? The attached picture might help you to visualize the problem.

I have started but can't really solve the problem. As a start, below is the current code (it doesn't work) for the MouseEventArgs:

        private void glControl1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!loaded)
                return;
 
            for (int ii = 0; ii < UIElementsEnd.Count; ii++)
            {
                if (e.Location.X / xmag == UIElementsStart.ElementAt(ii).X &&
                    e.Location.X / xmag == UIElementsEnd.ElementAt(ii).X)
                {
                    if (UIElementsStart.ElementAt(ii).Y < (e.Location.Y / ymag) &&
                        (e.Location.Y / ymag) < UIElementsStart.ElementAt(ii).Y)
                    {
                        UIElementsHighlightStart.Add(UIElementsStart.ElementAt(ii));
                        UIElementsHighlightEnd.Add(UIElementsEnd.ElementAt(ii));
                    }
                }
 
                if (e.Location.Y / ymag == UIElementsStart.ElementAt(ii).Y &&
                    e.Location.Y / ymag == UIElementsEnd.ElementAt(ii).Y)
                {
                    if (UIElementsStart.ElementAt(ii).X < (e.Location.X / xmag) &&
                        (e.Location.X / xmag) < UIElementsStart.ElementAt(ii).X)
                    {
                        UIElementsHighlightStart.Add(UIElementsStart.ElementAt(ii));
                        UIElementsHighlightEnd.Add(UIElementsEnd.ElementAt(ii));
                    }
                }
            }
 
            UIElementsHighlightStart.Clear();
            UIElementsHighlightEnd.Clear();
 
        }

Below is the code for the PaintEventArgs:

        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            GL.LineWidth(4f);
            GL.Color3(Color.Green);
            GL.Begin(BeginMode.Lines);
 
            if (UIElementsHighlightStart.Count != 0)
            {
                GL.Vertex2(UIElementsHighlightStart.ElementAt(0).X * xmag, UIElementsHighlightStart.ElementAt(0).Y * ymag);
                GL.Vertex2(UIElementsHighlightEnd.ElementAt(0).X * xmag, UIElementsHighlightEnd.ElementAt(0).Y * ymag);
            }
 
            GL.End();
 
            glControl1.Invalidate();
            }

Thanks much guys.

AT

Inline Images

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
adityatan's picture

I found the solution!

        private void glControl1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ...
 
            elmhighlightx = Math.Round(Convert.ToDouble(e.Location.X / xmag), 0);
            elmhighlighty = Math.Round(Convert.ToDouble((glControl1.Height - e.Location.Y) / ymag), 0);
 
            for (int h = 0; h < UIElementsEnd.Count; h++)
            {
                if (UIElementsStart.ElementAt(h).X - 2 <= elmhighlightx &&
                    elmhighlightx <= UIElementsStart.ElementAt(h).X + 2 &&
                    UIElementsEnd.ElementAt(h).X - 2 <= elmhighlightx &&
                    elmhighlightx <= UIElementsEnd.ElementAt(h).X + 2)
                {
                    if (UIElementsEnd.ElementAt(h).Y < elmhighlighty &&
                        elmhighlighty < UIElementsStart.ElementAt(h).Y)
                    {
                        UIElementsHighlightStart = (UIElementsStart.ElementAt(h)); 
                        UIElementsHighlightEnd = (UIElementsEnd.ElementAt(h)); 
                        break;
                    }
                    if (UIElementsStart.ElementAt(h).Y < elmhighlighty &&
                        elmhighlighty < UIElementsEnd.ElementAt(h).Y)
                    {
                        UIElementsHighlightStart = (UIElementsStart.ElementAt(h));
                        UIElementsHighlightEnd = (UIElementsEnd.ElementAt(h));
                        break;
                    }
                }
                else if (UIElementsStart.ElementAt(h).Y - 2 <= elmhighlighty &&
                    elmhighlighty <= UIElementsStart.ElementAt(h).Y + 2 &&
                    UIElementsEnd.ElementAt(h).Y - 2 <= elmhighlighty &&
                    elmhighlighty <= UIElementsEnd.ElementAt(h).Y + 2)
                {
                    if (UIElementsEnd.ElementAt(h).X < elmhighlightx &&
                        elmhighlightx < UIElementsStart.ElementAt(h).X)
                    {
                        UIElementsHighlightStart = (UIElementsStart.ElementAt(h));
                        UIElementsHighlightEnd = (UIElementsEnd.ElementAt(h)); 
                        break;
                    }
                    if (UIElementsStart.ElementAt(h).X < elmhighlightx &&
                        elmhighlightx < UIElementsEnd.ElementAt(h).X)
                    {
                        UIElementsHighlightStart = (UIElementsStart.ElementAt(h));
                        UIElementsHighlightEnd = (UIElementsEnd.ElementAt(h)); 
                        break;
                    }
                }
                else
                {
                    UIElementsHighlightStart.X = 0;
                    UIElementsHighlightStart.Y = 0;
                    UIElementsHighlightEnd.X = 0;
                    UIElementsHighlightEnd.Y = 0;
                }
            }
 
            ...
        }
 

Thanks for checking it out guys!

AT