PDA

Click to See Complete Forum and Search --> : [2.0] Stimulate a mouseclick


Fromethius
Dec 16th, 2006, 03:49 PM
Alright, ALL I want to do is stimulate a mouse click. Something like.

SendKeys.Send("{LEFTMOUSECLICK}");

but of course that doesn't work. Now, in VB6 it was easy. Something like this:

Private m_Mouse As CMouseEvent
m_Mouse.Location = 0,0
m_Mouse.ButtonClick


whatever it was something like that i forget. But anyways I can't find anything about doing this in C#. Any ideaS?

Fromethius
Dec 16th, 2006, 04:18 PM
This is what I have so far.


MouseEventArgs m;

private void btnTest_Click(object sender, EventArgs e)
{
m.Button = MouseButtons.Left;
OnMouseClick(m);
}


Because that is what it says on:
http://msdn2.microsoft.com/en-us/library/ms171548.aspx

However, m.Button is read-only even though in the second paragraph they say to assign it to a left or right click or whatever. Do I need to ovveride it? If so, how? I have never overrided anything before nor do I know how to do it or how to use it. Thanks

Fromethius
Dec 16th, 2006, 04:57 PM
Finally, after 5 hours of looking and searching google I found this. However, is it the best way?


private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;

[DllImport("user32.dll")]
private static extern void mouse_event(
UInt32 dwFlags, // motion and click options
UInt32 dx, // horizontal position or change
UInt32 dy, // vertical position or change
UInt32 dwData, // wheel movement
IntPtr dwExtraInfo // application-defined information
);

public static void SendClick(Point location)
{
Cursor.Position = location;
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}

private void btnTest_Click(object sender, EventArgs e)
{
Timer.Enabled = true;
}

private void Timer_Tick(object sender, EventArgs e)
{
Point location = new Point(MousePosition.X, MousePosition.Y);
SendClick(location);
}

jmcilhinney
Dec 16th, 2006, 06:56 PM
If you want to affect applications outside your own, or be affected by applications outside your own in the case of the keyboard hook, then it is almost always going to involve platform invoke (Windows API). What I would suggest is wrapping this sort of functionality in a class of its own, which you can then use whenever and wherever you need to with a couple of lines of code. That's the advantage with OOP. Maybe something does take a reasonable amount of code, but once you've written that code once it is a very simple thing to invoke it in many different places.