3

I am trying to figure out how to write the code so the response time between the mouse movement and line drawing is instant. Everytime I do this there is always lag between the 2. I have coded this in the windows forms and now I am coding it in WPF.

The problem definitly lies in the code and is not my computer.

Basicaly it works like this. Click anywhere and point1 is created. Move the mouse and point2 is updated and a line is drawn from Point1 to Point2 which changes on mouse movement.

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

Also, there will be an image layer under this in the future which for sure will cause it to lag even more.. But right now I just want to optimize this.

When I used other such programs from 5+ years ago, the line movement was actually instant. Thats why I am confused as to why with this newer 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;

    }
svick
  • 236,525
  • 50
  • 385
  • 514
Chris Fazzio
  • 375
  • 1
  • 6
  • 16
  • Adding a line control to Canvas is insanity. Try using graphics context to draw onto the screen. – Asti Mar 16 '12 at 14:36
  • 1
    Just tried your exact code on my machine and can't observe any lag at all. – Kent Boogaart Mar 16 '12 at 14:38
  • humm, thanks for the comments.. I will try using the graphics context.. The lag is very small but definitly there.. Maximize the program and make the line longer and move the mouse faster – Chris Fazzio Mar 16 '12 at 14:40
  • When you focus your eyes on the mouse pointer, you will notice the end of the line will kind of float around the mouse pointer when you move the mouse up and down quickly.. actually you just have to move the mouse at an average speed to see easily – Chris Fazzio Mar 16 '12 at 14:41

1 Answers1

2

WinAPI does not send WM_MOUSEMOVE quick enough to detect every movement of mouse. There is an very good article that shows how to detect mouse movement very precise (http://blogs.msdn.com/b/oldnewthing/archive/2012/03/14/10282406.aspx)

Nikolay
  • 3,658
  • 1
  • 24
  • 25
  • Thanks, would you say that you can only do this with C++? This article explains with c++.. This is fine if its true, I would just like to know for sure so I can either continue learning c++ or c# WPF.. – Chris Fazzio Mar 16 '12 at 15:42
  • No, i think you can do it with c# and wpf but it will be pretty difficult, because you have to use WinApi (read about p/invoke http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx) and use some tricks to hook to window messaged loop (this article can help - http://stackoverflow.com/questions/624367/how-to-handle-wndproc-messages-in-wpf) – Nikolay Mar 16 '12 at 17:52