Sunday, March 15, 2009

Mouse Operations Using Win32

I am writing a library for automation testing. There is hell lot of tools and libraries which is not solving all the purpose. Where you need to extend or create plug-in or pay for support. This Need leads to innovation. I started with MS UI automation library, that’s a good place to start. After used it for some time I thought there is no enough support for legacy win32 applications. And it is not supporting for the mouse operation. There are few places where the mouse operation is must, that lead to found mouse simulation using win32 API.  There are few other operations like mouse wheel move …. But I guess it not need for automation

Mouse Structure

 

[StructLayout(LayoutKind.Sequential)]

        public struct MouseInput

        {

            private int dx;

            private int dy;

            private int mouseData;

            private int dwFlags;

            private int time;

            private IntPtr dwExtraInfo;

 

            public MouseInput(int dwFlags, IntPtr dwExtraInfo)

            {

                this.dwFlags = dwFlags;

                this.dwExtraInfo = dwExtraInfo;

                dx = 0;

                dy = 0;

                time = 0;

                mouseData = 0;

            }

        }

 

Mouse Constants

  public const int INPUT_KEYBOARD = 1;

        public const int INPUT_MOUSE = 0;

        public const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        public const int MOUSEEVENTF_LEFTDOWN = 0x0002;

        public const int MOUSEEVENTF_LEFTUP = 0x0004;

        public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;

        public const int MOUSEEVENTF_MIDDLEUP = 0x0040;

        public const int MOUSEEVENTF_MOVE = 0x0001;

        public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;

        public const int MOUSEEVENTF_RIGHTUP = 0x0010;

        public const int MOUSEEVENTF_VIRTUALDESK = 0x4000;

        public const int MOUSEEVENTF_WHEEL = 0x0800;

        public const int MOUSEEVENTF_XDOWN = 0x0080;

        public const int MOUSEEVENTF_XUP = 0x0100;

 

 

Win32 Methods for mouse

[DllImport("user32.dll", SetLastError = true)]

 public static extern uint SendInput(uint nInputs,ref Input pInputs, int cbSize);

 

 

        [DllImport("user32.dll")]

        public static extern bool SetCursorPos(System.Drawing.Point cursorInfo);

 

[StructLayout(LayoutKind.Explicit)] //This is used for key board and mouse

        public struct Input

        {

            [FieldOffset(0)]

            private int type;

            [FieldOffset(4)]

            Private MouseInput mi;

 

            public static Input Mouse(MouseInput mouseInput)

            {

                var input = new Input {type = INPUT_MOUSE, mi = mouseInput};

                return input;

            }

        }

 

 

Actual methods

public void Click(Point point) //System.Drawing.Point

        {

SetCursorPosition(point);

            MouseOperation(MOUSEEVENTF_LEFTDOWN);

//Add delay if it not working fine

            MouseOperation(MOUSEEVENTF_LEFTUP);

        }

  public void RightClick(Point point)

        {

SetCursorPosition(point);

 

            MouseOperation(MOUSEEVENTF_RIGHTDOWN);

//Add delay if it not working fine

            MouseOperation(MOUSEEVENTF_RIGHTUP);

        }


 

public void DoubleClick(Point point)

{

SetCursorPosition(point);

            MouseOperation(MOUSEEVENTF_LEFTDOWN);

//Add delay if it not working fine

 

            MouseOperation(MOUSEEVENTF_LEFTUP);

 

            MouseOperation(MOUSEEVENTF_LEFTDOWN);

//Add delay if it not working fine

 

            MouseOperation(MOUSEEVENTF_LEFTUP);

 

}

 

 

        private void SetCursorPosition(Point curPoint)

        {

            SetCursorPos(curPoint);

        }

  private void MouseOperation(int operation)

  {

var mInput = new MouseInput(operation,GetMessageExtraInfo());

Input input = Input.Mouse(mInput);

         SendInput(1, ref input, Marshal.SizeOf(typeof (Input)));

        }

 

Sunday, March 8, 2009

Few Win32 Constants

internal class Win32Constants
    {
      
        public const int FLASHW_ALL = (FLASHW_CAPTION | FLASHW_TRAY);
        public const int FLASHW_CAPTION = 0x00000001;
        public const int FLASHW_STOP = 0;
        public const int FLASHW_TIMER = 0x00000004;
        public const int FLASHW_TIMERNOFG = 0x0000000C;
        public const int FLASHW_TRAY = 0x00000002;
        public const int GWL_EXSTYLE = (-20);
        public const int GWL_STYLE = (-16);
        public const int SRCCOPY = 0x00CC0020;

        # region Mouse

        public const int INPUT_KEYBOARD = 1;
        public const int INPUT_MOUSE = 0;
        public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        public const int MOUSEEVENTF_LEFTUP = 0x0004;
        public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        public const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        public const int MOUSEEVENTF_MOVE = 0x0001;
        public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        public const int MOUSEEVENTF_RIGHTUP = 0x0010;
        public const int MOUSEEVENTF_VIRTUALDESK = 0x4000;
        public const int MOUSEEVENTF_WHEEL = 0x0800;
        public const int MOUSEEVENTF_XDOWN = 0x0080;
        public const int MOUSEEVENTF_XUP = 0x0100;
#endregion

        # region Window

        public const int SC_CLOSE = 0xF060;
        public const int SC_MAXIMIZE = 0xF030;
        public const int SC_MINIMIZE = 0xF020;
        public const int SC_RESTORE = 0xF120;
        public const int WM_COMMAND = 0x111;
        public const int WM_SYSCOMMAND = 0x112;
        public static int WM_GETID = -12;
        public static int WM_SETFOCUS = 0x07;

        #endregion

        #region TreeView

        public const int TV_FIRST = 0x1100;
        public const int TVGN_CARET = 0x9;
        public const int TVGN_CHILD = 0x4;
        public const int TVGN_NEXT = 0x1;
        public const int TVIF_TEXT = 0x0001;
        public const int TVM_GETITEM = 0x110C;
        public const int TVM_GETNEXTITEM = (TV_FIRST + 10);
        public const int TVM_SELECTITEM = (TV_FIRST + 11);
        public const int TVM_GETITEMRECT = (TV_FIRST + 4);


        #endregion

        #region Process

        public const uint MEM_COMMIT = 0x1000;
        public const uint MEM_RELEASE = 0x8000;
        public const uint PAGE_READWRITE = 0x04;
        public const uint PROCESS_ALL_ACCESS = (uint) (0x000F0000L | 0x00100000L | 0xFFF);

        #endregion

        #region Findwindow

        public const int GW_Child = 5;
        public const int GW_HWNDFIRST = 0;
        public const int GW_HWNDLAST = 1;
        public const int GW_HWNDNEXT = 2;
        public const int GW_HWNDPREV = 3;
        public const int GW_OWNER = 4;

        #endregion

       
    }

Few Win32 Constans