Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simulate mouseclick??

Simulate mouseclick??

Scheduled Pinned Locked Moved C#
csharptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jafingi
    wrote on last edited by
    #1

    Hi, I want to send a mouseclick to a defined window in c#, without moving the cursor. It should just leftclick on a specified (X,Y) position on the screen. Can i do that without moving the cursor? And can it work if the target window is hidden/minimized? Hope someone can give me a code example :)

    R 1 Reply Last reply
    0
    • J jafingi

      Hi, I want to send a mouseclick to a defined window in c#, without moving the cursor. It should just leftclick on a specified (X,Y) position on the screen. Can i do that without moving the cursor? And can it work if the target window is hidden/minimized? Hope someone can give me a code example :)

      R Offline
      R Offline
      Rajasekharan Vengalil
      wrote on last edited by
      #2

      Here you go!

      [Flags]
      public enum MouseEvent : uint
      {
      MOUSEEVENTF_MOVE = 0x0001,
      MOUSEEVENTF_LEFTDOWN = 0x0002,
      MOUSEEVENTF_LEFTUP = 0x0004,
      MOUSEEVENTF_RIGHTDOWN = 0x0008,
      MOUSEEVENTF_RIGHTUP = 0x0010,
      MOUSEEVENTF_MIDDLEDOWN = 0x0020,
      MOUSEEVENTF_MIDDLEUP = 0x0040,
      MOUSEEVENTF_XDOWN = 0x0080,
      MOUSEEVENTF_XUP = 0x0100,
      MOUSEEVENTF_WHEEL = 0x0800,
      MOUSEEVENTF_VIRTUALDESK = 0x4000,
      MOUSEEVENTF_ABSOLUTE = 0x8000
      }

      public enum InputType : uint
      {
      INPUT_MOUSE = 0,
      INPUT_KEYBOARD = 1,
      INPUT_HARDWARE = 2
      }

      [StructLayout(LayoutKind.Sequential)]
      public struct MOUSEINPUT
      {
      private InputType type;
      public UInt32 dx;
      public UInt32 dy;
      public UInt32 mouseData;
      public MouseEvent dwFlags;
      public UInt32 time;
      public IntPtr dwExtraInfo;
      }

      public static class MouseInput
      {
      [DllImport("User32.dll")]
      public static extern UInt32 SendInput(UInt32 nInputs, MOUSEINPUT[] pInputs, int cbSize);

      private static Point Normalize(Point pt)
      {
          return new Point((pt.X \* 65535) / Screen.PrimaryScreen.Bounds.Width,
              (pt.Y \* 65535) / Screen.PrimaryScreen.Bounds.Height);
      }
      
      public static void Move(Point pt)
      {
          pt = Normalize(pt);
      
          MOUSEINPUT\[\] input = new MOUSEINPUT\[1\];
          input\[0\].dx = Convert.ToUInt32(pt.X);
          input\[0\].dy = Convert.ToUInt32(pt.Y);
          input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_MOVE | MouseEvent.MOUSEEVENTF\_ABSOLUTE;
      
          SendInput(1, input, Marshal.SizeOf(input\[0\]));
      }
      
      public static void Click(Point pt)
      {
          Move(pt);
      
          pt = Normalize(pt);
      
          MOUSEINPUT\[\] input = new MOUSEINPUT\[2\];
          input\[0\].dx = Convert.ToUInt32(pt.X);
          input\[0\].dy = Convert.ToUInt32(pt.Y);
          input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTDOWN | MouseEvent.MOUSEEVENTF\_ABSOLUTE;
      
          input\[1\].dx = Convert.ToUInt32(pt.X);
          input\[1\].dy = Convert.ToUInt32(pt.Y);
          input\[1\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTUP | MouseEvent.MOUSEEVENTF\_ABSOLUTE;
      
          SendInput(2, input, Marshal.SizeOf(input\[0\]));
      }
      

      }

      To simulate a click at 100,100 therefore you'd do this:

      MouseInput.Click( new Point( 100, 100 ) );

      I don't think you can do it without moving the cursor. You could save the current mouse position, click at the point where you want to click and then move right back to the saved position!

      J 1 Reply Last reply
      0
      • R Rajasekharan Vengalil

        Here you go!

        [Flags]
        public enum MouseEvent : uint
        {
        MOUSEEVENTF_MOVE = 0x0001,
        MOUSEEVENTF_LEFTDOWN = 0x0002,
        MOUSEEVENTF_LEFTUP = 0x0004,
        MOUSEEVENTF_RIGHTDOWN = 0x0008,
        MOUSEEVENTF_RIGHTUP = 0x0010,
        MOUSEEVENTF_MIDDLEDOWN = 0x0020,
        MOUSEEVENTF_MIDDLEUP = 0x0040,
        MOUSEEVENTF_XDOWN = 0x0080,
        MOUSEEVENTF_XUP = 0x0100,
        MOUSEEVENTF_WHEEL = 0x0800,
        MOUSEEVENTF_VIRTUALDESK = 0x4000,
        MOUSEEVENTF_ABSOLUTE = 0x8000
        }

        public enum InputType : uint
        {
        INPUT_MOUSE = 0,
        INPUT_KEYBOARD = 1,
        INPUT_HARDWARE = 2
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct MOUSEINPUT
        {
        private InputType type;
        public UInt32 dx;
        public UInt32 dy;
        public UInt32 mouseData;
        public MouseEvent dwFlags;
        public UInt32 time;
        public IntPtr dwExtraInfo;
        }

        public static class MouseInput
        {
        [DllImport("User32.dll")]
        public static extern UInt32 SendInput(UInt32 nInputs, MOUSEINPUT[] pInputs, int cbSize);

        private static Point Normalize(Point pt)
        {
            return new Point((pt.X \* 65535) / Screen.PrimaryScreen.Bounds.Width,
                (pt.Y \* 65535) / Screen.PrimaryScreen.Bounds.Height);
        }
        
        public static void Move(Point pt)
        {
            pt = Normalize(pt);
        
            MOUSEINPUT\[\] input = new MOUSEINPUT\[1\];
            input\[0\].dx = Convert.ToUInt32(pt.X);
            input\[0\].dy = Convert.ToUInt32(pt.Y);
            input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_MOVE | MouseEvent.MOUSEEVENTF\_ABSOLUTE;
        
            SendInput(1, input, Marshal.SizeOf(input\[0\]));
        }
        
        public static void Click(Point pt)
        {
            Move(pt);
        
            pt = Normalize(pt);
        
            MOUSEINPUT\[\] input = new MOUSEINPUT\[2\];
            input\[0\].dx = Convert.ToUInt32(pt.X);
            input\[0\].dy = Convert.ToUInt32(pt.Y);
            input\[0\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTDOWN | MouseEvent.MOUSEEVENTF\_ABSOLUTE;
        
            input\[1\].dx = Convert.ToUInt32(pt.X);
            input\[1\].dy = Convert.ToUInt32(pt.Y);
            input\[1\].dwFlags = MouseEvent.MOUSEEVENTF\_LEFTUP | MouseEvent.MOUSEEVENTF\_ABSOLUTE;
        
            SendInput(2, input, Marshal.SizeOf(input\[0\]));
        }
        

        }

        To simulate a click at 100,100 therefore you'd do this:

        MouseInput.Click( new Point( 100, 100 ) );

        I don't think you can do it without moving the cursor. You could save the current mouse position, click at the point where you want to click and then move right back to the saved position!

        J Offline
        J Offline
        jafingi
        wrote on last edited by
        #3

        Hi, Almost forgot to thank you! So.. Thanks! :) Nice code ;)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups