(wpf c#)ive been trying to figure out how to add a line that starts at the button clicked and ends at the mouse pos on mouse move, losing will to live


Joined
Oct 16, 2022
Messages
1
Reaction score
0
C#:
        UserControl? movingObject;
        Point offset;
        Line l = new Line();

        bool makingConnection;
        private void X_PreviewMouseLeftDown(object sender, MouseButtonEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                movingObject = sender as UserControl;
                offset = e.GetPosition(canvas);
                offset.Y -= Canvas.GetTop(movingObject);
                offset.X -= Canvas.GetLeft(movingObject);
                canvas.CaptureMouse();
                Mouse.OverrideCursor = new Cursor(@"C:\Users\conra\source\repos\WpfApp1\WpfApp1\\grabbing.cur");
            }
            parentControl pc = (parentControl)sender;
            foreach(connector c in pc.connectors)
            {
                if (c.isClicked)
                {
                    makingConnection = true;
                    l.X1 = e.GetPosition(canvas).X;
                    l.Y1 = e.GetPosition(canvas).Y;
                }
            }
        }
        private void canvas_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (movingObject == null) return;
            Point pp = e.GetPosition(sender as IInputElement);
            Canvas.SetTop(movingObject, pp.Y - offset.Y);
            Canvas.SetLeft(movingObject, pp.X - offset.X);

            if (makingConnection)
            {
                l.Stroke = Brushes.Black;
                l.StrokeThickness = 4;
                l.X2 = e.GetPosition(sender as IInputElement).X;
                l.Y2 = e.GetPosition(sender as IInputElement).Y;
                canvas.Children.Add(l);
            }
        }
        private void canvas_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            movingObject = null;
            canvas.ReleaseMouseCapture();
            Mouse.OverrideCursor = Cursors.Hand;
        }

this is what ive got so far, it doesnt add a line
 
Ad

Advertisements


Top