|
-
Dec 16th, 2006, 04:49 PM
#1
Thread Starter
Frenzied Member
[2.0] Stimulate a mouseclick
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?
-
Dec 16th, 2006, 05:18 PM
#2
Thread Starter
Frenzied Member
Re: [2.0] Stimulate a mouseclick
This is what I have so far.
Code:
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
-
Dec 16th, 2006, 05:57 PM
#3
Thread Starter
Frenzied Member
Re: [2.0] Stimulate a mouseclick
Finally, after 5 hours of looking and searching google I found this. However, is it the best way?
Code:
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);
}
-
Dec 16th, 2006, 07:56 PM
#4
Re: [2.0] Stimulate a mouseclick
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|