Line drawing with instant feedback on mouse movement?

I am trying to figure out how to write code, so the response time between mouse movement and line drawing is instant. Every time I do this, there is always a gap between 2. I coded this in Windows Forms and now I am coding it in WPF.

The problem is definitely in the code and not my computer.

It basically works like this. Click anywhere and create point1. Move the mouse and point2 will update and a line will be drawn from Point1 to Point2, which changes as you move the mouse.

below is my extremely simple code to do this. It may seem instantaneous, but if you maximize the window and make the line longer and move the mouse quickly, you may notice it more easily.

Also, there will be an image layer in this in the future, which will surely make it lag even further. But for now, I just want to optimize this.

When I have used other similar programs since 5 years ago, the line movement was really instant. That's why I am confused why with this new coding, its laggy ..

    Point mLoc;
    Line myLine = new Line();


    public MainWindow()
    {
        InitializeComponent();
        SnapsToDevicePixels = false;

        myLine.Stroke = System.Windows.Media.Brushes.White;
        myLine.StrokeThickness = 1;

        canvas1.Children.Add(myLine);
    }

    private void onMMove(object sender, MouseEventArgs e)
    {
        mLoc = Mouse.GetPosition(canvas1);

        myLine.X2 = mLoc.X;
        myLine.Y2 = mLoc.Y;


    }

    private void onMLClick(object sender, MouseButtonEventArgs e)
    {
        mLoc = Mouse.GetPosition(canvas1);

        myLine.X1 = mLoc.X;
        myLine.Y1 = mLoc.Y;

    }

      

+3


source to share


1 answer


WinAPI does not dispatch WM_MOUSEMOVE fast enough to detect every mouse movement. There is a very good article showing how to very accurately detect mouse movement ( http://blogs.msdn.com/b/oldnewthing/archive/2012/03/14/10282406.aspx )



+2


source







All Articles