Simulating mouse movement

B

Benny Raymond

In my attempt to make a macro recording program where i can then
playback mouse movements i'm running into a big problem:

I'm trying to simulate the movement of the mouse to a point on the
screen. With my current code the mouse is only moving in the X
direction and not in the Y direction (however my debug line is telling
me that it's trying to move in the Y as well). I've read everything I
could find online, tried some hacky ways of getting the mouse to move,
etc... I'm stuck. The following is the part of my code that handles the
mouse movement - I was hoping that someone in here could figure out why
the mouse isn't moving in the Y direction, or if someone knows of an
easier way of moving the mouse around the screen:

//------------------------------------
// START CODE SNIPPIT
//------------------------------------

// in the code when I want to move the mouse I run this:
// (assuming i want to move the mouse to 500px,500px:
moveMouseTo(500,500);
// the mouse then moves 500 in the x direction but 0 in the y?


//Constants
private const UInt32 MOUSEEVENTF_ABSOLUTE = 0x8000;
private const UInt32 MOUSEEVENTF_MOVE = 0x0001;

//Screen coord conversion factors.
private const double SCREEN_X_CONV = 64.0; //65535 / 1024
private const double SCREEN_Y_CONV = 85.3; //65535 / 768

public void moveMouseTo(int x, int y) {
x = (int) (x * SCREEN_X_CONV);
y = (int) (y * SCREEN_Y_CONV);
Debug.WriteLine("mouse to: " + x.ToString() + "," + y.ToString());
User32.mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, x, y, 0,
(System.IntPtr)0);
}

private class User32 {
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll")]
public static extern void mouse_event(uint dwFlags, long dx, long dy,
uint dwData, IntPtr dwExtraInfo);
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint
dwFlags, IntPtr dwExtraInfo);
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint
modifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("kernel32.dll")]
private static extern int GlobalAddAtom(String atomString);
[DllImport("kernel32.dll")]
private static extern int GlobalDeleteAtom(int atom);
}

//------------------------------------
// END CODE SNIPPIT
//------------------------------------
 
B

Benny Raymond

Nevermind... Just figured out there's another function in User32 called:

User32.SetCursorPos(X, Y);

grabbed from:

[DllImport("user32.dll", SetLastError=true)]
[return:MarshalAs(UnmanagedType.Bool)]
public static extern bool SetCursorPos(int X, int Y);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top