Emulate mouse action in .NETCF

摘要:Emulate mouse action in .NETCF

There are some ways to do it,

SendMessage API:
            //Find topmost window of another application
            IntPtr parentWnd = WIN32.Window.FindWindow(null, "Form2");

            //Find the button
            IntPtr hButton = WIN32.Window.FindWindowEx(parentWnd, IntPtr.Zero, null, "Target");

            //Click the target button
            WIN32.Window.SendMessage(hButton, Window.WM.WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
            WIN32.Window.SendMessage(hButton, Window.WM.WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
 

mouse_event API:

        [DllImport("coredll.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        public enum MouseEventFlags
        {
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            MOVE = 0x00000001,
            ABSOLUTE = 0x00008000,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010
        }
        void ClickIt()
        {
            SetCursorPos(400, 10);
            mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
            mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);

           }

勤勞樸實