Mouse Events in other applications

W

wesmanjunk

Does anyone know how to generate mouse click in another application?
using C# and Windows XP..

The Idea it to click ok buttons on a form where you know the position
of all the buttons, or you can train the mouse on the correct
locations. oh the other application may or may not be dotnet

Thanks
 
S

Scott Maskiel

Hi Wesman,

You can do this in one of a few ways. I'd recommend either the
mouse_event() or SendMessage() API depending on your requirements.

mouse_event() is suitable when you want to click a specific x/y
position on the screen, e.g. you know exactly where the button is and
you're absolutely certain that it is visible on the screen. It also
has the added benefit that it's nigh impossible for the receving
application to tell the difference between your "fake" click and a
real one.

SendMessage() is useful when you're not certain that the application
is visible but you want to send it a click anyway. It is possible
however for the receving application to determine that this was in
fact a "fake" mouse click and consequently ignore it.

To implement mouse_event:

[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int
dwData,
int dwExtraInfo);

const int MOUSEEVENTF_MOVE = 0x00000001;
const int MOUSEEVENTF_LEFTDOWN = 0x00000002;
const int MOUSEEVENTF_LEFTUP = 0x00000004;
const int MOUSEEVENTF_RIGHTDOWN = 0x00000008;
const int MOUSEEVENTF_RIGHTUP = 0x00000010;
const int MOUSEEVENTF_MIDDLEDOWN = 0x00000020;
const int MOUSEEVENTF_MIDDLEUP = 0x00000040;
const int MOUSEEVENTF_WHEEL = 0x00000800;
const int MOUSEEVENTF_ABSOLUTE = 0x00008000;

private void button1_Click(object sender, System.EventArgs e)
{
Cursor.Position = new Point(100, 100); // the x/y coordinates of
where you want to click
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

Implementing SendMessage is a bit more complicated since you need to
get a window handle for the button. I've pasted in the import
declaration for SendMessage() below. If you're interested in this
approach you should also take a look at the EnumWindows(),
EnumChildWindows() and GetWindow() API calls, since these will help
you get a handle to the window that represents the button.

After you have a window handle, you'll want to look at the following
messages:

WM_LBUTTONDOWN
WM_LBUTTONUP

[DllImport ("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int
wParam, int lParam);

Regards,
Scott
 
U

unrealpoker

Scott said:
Hi Wesman,

You can do this in one of a few ways. I'd recommend either the
mouse_event() or SendMessage() API depending on your requirements.

mouse_event() is suitable when you want to click a specific x/y
position on the screen, e.g. you know exactly where the button is and
you're absolutely certain that it is visible on the screen. It also
has the added benefit that it's nigh impossible for the receving
application to tell the difference between your "fake" click and a
real one.

SendMessage() is useful when you're not certain that the application
is visible but you want to send it a click anyway. It is possible
however for the receving application to determine that this was in
fact a "fake" mouse click and consequently ignore it.

To implement mouse_event:

[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int
dwData,
int dwExtraInfo);

const int MOUSEEVENTF_MOVE = 0x00000001;
const int MOUSEEVENTF_LEFTDOWN = 0x00000002;
const int MOUSEEVENTF_LEFTUP = 0x00000004;
const int MOUSEEVENTF_RIGHTDOWN = 0x00000008;
const int MOUSEEVENTF_RIGHTUP = 0x00000010;
const int MOUSEEVENTF_MIDDLEDOWN = 0x00000020;
const int MOUSEEVENTF_MIDDLEUP = 0x00000040;
const int MOUSEEVENTF_WHEEL = 0x00000800;
const int MOUSEEVENTF_ABSOLUTE = 0x00008000;

private void button1_Click(object sender, System.EventArgs e)
{
Cursor.Position = new Point(100, 100); // the x/y coordinates of
where you want to click
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

Implementing SendMessage is a bit more complicated since you need to
get a window handle for the button. I've pasted in the import
declaration for SendMessage() below. If you're interested in this
approach you should also take a look at the EnumWindows(),
EnumChildWindows() and GetWindow() API calls, since these will help
you get a handle to the window that represents the button.

After you have a window handle, you'll want to look at the following
messages:

WM_LBUTTONDOWN
WM_LBUTTONUP

[DllImport ("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int
wParam, int lParam);

Regards,
Scott




Does anyone know how to generate mouse click in another application?
using C# and Windows XP..

The Idea it to click ok buttons on a form where you know the position
of all the buttons, or you can train the mouse on the correct
locations. oh the other application may or may not be dotnet

Thanks
 

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