Simulating Mouse Events
-
Monday, July 21, 2008 1:10 AMHello everyone,
I am attempting to simulate mouse click events (I have been using cursor.position to handle all the movements) and have run into a problem. I read the MSDN thread http://msdn.microsoft.com/en-us/library/ms171548(VS.80).aspx and it was very helpful, but I must be doing something wrong because it does not work. I made a test program to see if I could find the problem: I have a basic form with a button on it, when I click the button the cursor moves down and attempts to click. The cursor moves great but after that nothing; I have the code snippet below. Any thoughts? Thanks in advance!!
private void startButton_Click(object sender, EventArgs e)
{
Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 100);
MouseEventArgs click = new MouseEventArgs(MouseButtons.Left, 1, Cursor.Position.X,
Cursor.Position.Y, 0);
OnMouseClick(click);
}
All Replies
-
Monday, July 21, 2008 2:01 AMSince you moved the cursor down by increasing its Y-coordinate by 100 pixels it must be out of the button1 client rectangle, so why do you expect anything to happen? I mean in terms of the Button's reaction? If you position another button centered at the new location you should get a button2.Click event.
AlexB- Edited by AlexBB - Vista Ult64 SqlSer64 WinSer64 Monday, July 21, 2008 2:02 AM *********
-
Monday, July 21, 2008 2:57 AMSorry for not clarifying . . . I do have a second button in place :-). If it helps just pretend the move mouse cursor code isn't there. I still cannot get that to work. Any ideas Alex?
-
Thursday, July 24, 2008 7:44 AM
Hi Carret,
Based on my understanding, the “OnMouseClick” method can only apply to the current control, in this scenario, the current is Form, instead of button.
In order to raise a global mouse left click event, we can use the mouse_event Function, I have tried to written a sample to test it, works for me.
public partial class Form1 : Form{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern void mouse_event(UInt32 dwFlags,
UInt32 dx,
UInt32 dy,
UInt32 dwData,
IntPtr dwExtraInfo);
private void Startbutton_Click(object sender, EventArgs e)
{
Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 100);
MouseEventArgs click = new MouseEventArgs(MouseButtons.Left, 1, Cursor.Position.X,
Cursor.Position.Y, 0);
const UInt32 MOUSEEVENTF_LEFTDOWN = 0x2;//left mouse down
const UInt32 MOUSEEVENTF_LEFTUP = 0x4; //left mouse up
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1 clicked");
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("button1 mouse clicked");
}
}
Regards,
Xun
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer by jack 321 Friday, July 25, 2008 10:42 AM

