How can I move the cursor with c#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shockna1
    New Member
    • Apr 2010
    • 30

    How can I move the cursor with c#?

    Im trying to make a program so that when I hold down shift or something and press an arow key it moves the cursor. Is it posible to move the cursor using c#?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Yes it is.
    Look on MSDN for Cursor and change the Cursor.Location property

    Comment

    • shockna1
      New Member
      • Apr 2010
      • 30

      #3
      Originally posted by tlhintoq
      Yes it is.
      Look on MSDN for Cursor and change the Cursor.Location property
      Ok so this worked but I have 3 new questions naw. First haw can I click.?Second it only works on the form but not outside it the how do I make it work outside the form? And third how can I get location on the hole screen?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        First haw can I click.?
        With the mouse. What is this question supposed to even be asking?

        Second it only works on the form but not outside it the how do I make it work outside the form?
        "Global Hooks" is a much more advanced subject - but I've just told you what to research.

        And third how can I get location on the hole screen?
        Again, do a little research on MSDN. You can easily take a location and convert it To Screen Location. <hint>

        Comment

        • shockna1
          New Member
          • Apr 2010
          • 30

          #5
          Originally posted by tlhintoq
          With the mouse. What is this question supposed to even be asking?

          "Global Hooks" is a much more advanced subject - but I've just told you what to research.

          Again, do a little research on MSDN. You can easily take a location and convert it To Screen Location. <hint>
          Sory I was not cleer on what I ment by how can I click. What I ment was haw can I simulate a mouse click with c#.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            There's a couple quick library functions you can import,eg :
            Code:
                   [DllImport("user32.dll", SetLastError = true)]
                    [return: MarshalAs(UnmanagedType.Bool)]
                    public static extern bool SetCursorPos(int X, int Y);
                    [DllImport("user32.dll")]
                    static extern bool GetCursorPos(ref Point lpPoint);
                    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
                    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
            
                    /// <summary>
                    /// Not sure if we're just supposed to create our own point class.
                    /// </summary>
                    struct Point
                    {
                        public int x;
                        public int y;
                    }
            
            
                    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
                    private const int MOUSEEVENTF_LEFTUP = 0x04;
                    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
                    private const int MOUSEEVENTF_RIGHTUP = 0x10;
                    
                    
                    public static void DoMouseClick(long x, long y)
                    {   
                        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                    }
                
            
                    public static void MoveMouse(int x, int y)
                    {
                        SetCursorPos(x, y);
                    }
            Should be relatively self-explanatory as to how to use these functions. These allow clicking outside of your immediate application.
            Last edited by jkmyoung; Apr 19 '10, 09:42 PM. Reason: grammar

            Comment

            • shockna1
              New Member
              • Apr 2010
              • 30

              #7
              Originally posted by jkmyoung
              There's a couple quick library functions you can import,eg :
              Code:
                     [DllImport("user32.dll", SetLastError = true)]
                      [return: MarshalAs(UnmanagedType.Bool)]
                      public static extern bool SetCursorPos(int X, int Y);
                      [DllImport("user32.dll")]
                      static extern bool GetCursorPos(ref Point lpPoint);
                      [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
                      public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
              
                      /// <summary>
                      /// Not sure if we're just supposed to create our own point class.
                      /// </summary>
                      struct Point
                      {
                          public int x;
                          public int y;
                      }
              
              
                      private const int MOUSEEVENTF_LEFTDOWN = 0x02;
                      private const int MOUSEEVENTF_LEFTUP = 0x04;
                      private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
                      private const int MOUSEEVENTF_RIGHTUP = 0x10;
                      
                      
                      public static void DoMouseClick(long x, long y)
                      {   
                          mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                      }
                  
              
                      public static void MoveMouse(int x, int y)
                      {
                          SetCursorPos(x, y);
                      }
              Should be relatively self-explanatory as to how to use these functions. These allow clicking outside of your immediate application.
              Thanks this is exactly what I was looking for.

              Comment

              Working...