MouseInput moves mouse to the left at start of loop
<p>When I do this;</p>

<pre><code>Point startpoint = Cursor.Position;
startpoint.Y -= 1;
DoMouse(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE, startpoint);
</code></pre>

<p>The mouse doesn't just move up.. it moves a bit to the left as well. But if I do it in a loop, it only moves to the left at the first iteration.</p>

<p>Here is a fully working console program presenting the problem. You have to <code>Add Reference -&gt; .NET -&gt; System.Drawing</code> and <code>System.Windows.Forms</code> to get it to compile.</p>

<p>When starting the program type <code>start</code> to move the mouse up 5 pixels <em>once</em> or type <code>start X</code> (X being a number) to move the mouse up 5 pixels X times. You will see that each new loop the mouse will move a bit to the left; it shouldn't be doing that at all.</p>

<pre><code>using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace mousemove_temp
{
    class Program
    {
        //Capture user input
        static void Main(string[] args)
        {
            while (true)
            {
                string s = Console.ReadLine();

                switch (s)
                {
                    case("start"):
                        moveMouseTest(1);
                        break;
                    default:
                        //Get # of times to run function
                        Match match = Regex.Match(s, @"start (.+)", RegexOptions.IgnoreCase);
                        if (!match.Success || match.Groups.Count != 2) break;

                        //Copy # to int
                        int amnt = -1;
                        try
                        {
                            amnt = Int32.Parse(match.Groups[1].Value);
                        }
                        catch (Exception) { break; } //fail
                        if (amnt &lt;= -1) break; //fail
                        moveMouseTest(amnt); //aaaawww yeah

                        break;
                }

                Thread.Sleep(10);
            }
        }

        //Move the mouse
        static void moveMouseTest(int repeat)
        {
            int countrepeat = 0;

            //Loop entire function X times
            while (countrepeat &lt; repeat)
            {
                Point startpoint = Cursor.Position;
                int amount = 5; //Move 5 pixels
                int counter = 0;

                //Move 1 pixel up each loop
                while (counter &lt; amount)
                {
                    startpoint.Y -= 1;
                    DoMouse(MOUSEEVENTF.MOVE | MOUSEEVENTF.ABSOLUTE, startpoint);

                    counter++;
                    Thread.Sleep(100); //Slow down so you can see it only jumps left the first time
                }

                countrepeat++;
                Console.WriteLine(String.Format("{0}/{1}", countrepeat, repeat));
                Thread.Sleep(1000); //Wait a second before next loop
            }
        }

        /*
         * Function stuff 
        */

        //Control the Mouse
        private static object mouselock = new object(); //For use with multithreading
        public static void DoMouse(MOUSEEVENTF flags, Point newPoint)
        {
            lock (mouselock)
            {
                INPUT input = new INPUT();
                MOUSEINPUT mi = new MOUSEINPUT();
                input.dwType = InputType.Mouse;
                input.mi = mi;
                input.mi.dwExtraInfo = IntPtr.Zero;
                // mouse co-ords: top left is (0,0), bottom right is (65535, 65535)
                // convert screen co-ord to mouse co-ords...
                input.mi.dx = newPoint.X * (65535 / Screen.PrimaryScreen.Bounds.Width);
                input.mi.dy = newPoint.Y * (65535 / Screen.PrimaryScreen.Bounds.Height);
                input.mi.time = 0;
                input.mi.mouseData = 0;
                // can be used for WHEEL event see msdn
                input.mi.dwFlags = flags;
                int cbSize = Marshal.SizeOf(typeof(INPUT));
                int result = SendInput(1, ref input, cbSize);
                if (result == 0)
                    Console.WriteLine("DoMouse Error:" + Marshal.GetLastWin32Error());
            }
        }

        /*
         * Native Methods 
        */

        [DllImport("user32.dll", SetLastError = true)]
        static internal extern Int32 SendInput(Int32 cInputs, ref INPUT pInputs, Int32 cbSize);

        [DllImport("user32.dll")]
        public static extern bool GetAsyncKeyState(Int32 vKey);

        [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 28)]
        internal struct INPUT
        {
            [FieldOffset(0)]
            public InputType dwType;
            [FieldOffset(4)]
            public MOUSEINPUT mi;
            [FieldOffset(4)]
            public KEYBDINPUT ki;
            [FieldOffset(4)]
            public HARDWAREINPUT hi;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct MOUSEINPUT
        {
            public Int32 dx;
            public Int32 dy;
            public Int32 mouseData;
            public MOUSEEVENTF dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct KEYBDINPUT
        {
            public Int16 wVk;
            public Int16 wScan;
            public KEYEVENTF dwFlags;
            public Int32 time;
            public IntPtr dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct HARDWAREINPUT
        {
            public Int32 uMsg;
            public Int16 wParamL;
            public Int16 wParamH;
        }

        internal enum InputType : int
        {
            Mouse = 0,
            Keyboard = 1,
            Hardware = 2
        }

        [Flags()]
        internal enum MOUSEEVENTF : int
        {
            MOVE = 0x1,
            LEFTDOWN = 0x2,
            LEFTUP = 0x4,
            RIGHTDOWN = 0x8,
            RIGHTUP = 0x10,
            MIDDLEDOWN = 0x20,
            MIDDLEUP = 0x40,
            XDOWN = 0x80,
            XUP = 0x100,
            VIRTUALDESK = 0x400,
            WHEEL = 0x800,
            ABSOLUTE = 0x8000
        }

        [Flags()]
        internal enum KEYEVENTF : int
        {
            EXTENDEDKEY = 1,
            KEYUP = 2,
            UNICODE = 4,
            SCANCODE = 8
        }
    }
}
</code></pre>

<p>Can anybody tell what's going wrong?</p>
