469,925 Members | 1,519 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,925 developers. It's quick & easy.

Mouse Events in other applications

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

Nov 17 '05 #1
2 12247
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


On 10 May 2005 18:28:53 -0700, we********@hotmail.com wrote:
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


Nov 17 '05 #2


Scott Maskiel wrote:
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


On 10 May 2005 18:28:53 -0700, we********@hotmail.com wrote:
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


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by John Champaign | last post: by
reply views Thread by Stephen Williams | last post: by
reply views Thread by Plumer | last post: by
3 posts views Thread by Rick Strahl [MVP] | last post: by
16 posts views Thread by dfaber | last post: by
18 posts views Thread by eliss.carmine | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.