-
July 21st, 2006, 03:59 AM
#1
Simulate Mouse Click
Hey guys, i've been messing around with this and can't find out whats wrong with it X_x
public class Win32APICall
{
[DllImport("user32.dll", EntryPoint = "mouse_event")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
}
private void button1_Click(object sender, EventArgs e)
{
Win32APICall.mouse_event(Win32APICall.MOUSEEVENTF_LEFTDOWN, 544, 472, 0, 0);
Win32APICall.mouse_event(Win32APICall.MOUSEEVENTF_LEFTUP, 544, 472, 0, 0);
}
I click the button, and the mouse stays in the exact same spot. I'm trying to get it to click a link that's in a webbrowser on the form. I checked coordinates, they are right. I tried a whole bunch of times, nothing changes in there. There are no errors either. Any ideas? Thx.
-
July 21st, 2006, 09:01 AM
#2
Re: Simulate Mouse Click
call the button's PerformClick method.
-
July 21st, 2006, 12:46 PM
#3
Re: Simulate Mouse Click
private void button1_Click(object sender, EventArgs e)
{
//CursorPos.SetCursorPos(544, 472);
Win32APICall.mouse_event(Win32APICall.MOUSEEVENTF_LEFTDOWN, 544, 472, 0, 0);
Win32APICall.mouse_event(Win32APICall.MOUSEEVENTF_LEFTUP, 544, 472, 0, 0);
button1.PerformClick();
}
now has an error on the button1.performclick(); method. "An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll". Any ideas? =(
-
July 21st, 2006, 12:54 PM
#4
Re: Simulate Mouse Click
Dude.. what do you expect if you call PerformCLick() inside the click handler for the button youre PerformCLick()ing?
Its like this:
Code:
public void makeStackOverflow(){
makeStackOverflow();
}
-
July 21st, 2006, 12:57 PM
#5
Re: Simulate Mouse Click
incidentally, it sounds like youre having a button somewhere and when you click that button you want to programmatically move the mouse so it is hovering over a link in a webbrowser control and then click the mouse so that the web browser thinks the user clicked the link...
my question is, why bother? why not just grab the URL the link points to and tell the web browser to navigate() to it?
-
July 21st, 2006, 02:13 PM
#6
Re: Simulate Mouse Click
Lol. It's starting with a button, and later I will just turn it into a sub (or whatever they call it in C#). It's sort of a cheat for a game over the net, so I need it to auto click certain places. For now im just worrying about how to get it to click, later I will set those places and have it automatic instead of a button. Also, it's not a link, it's one of those games where you click somewhere and the guy moves there. Anyways, I tried to use button1.PerformClick(); in the form1_load... No error, but nothing happens like earlier. Also, I added that thing you wrote, and still nothing. =(
-
July 22nd, 2006, 02:43 AM
#7
Re: Simulate Mouse Click
*Bump* Any suggestions guys? =( Still can't get it to work.
-
July 22nd, 2006, 04:52 AM
#8
Re: Simulate Mouse Click
and again, i say you should put a WebBrowser control on the form, load the game webpage in it and then you can inspect the document and navigate round to your hearts content - youre making things too much harde rthan you need to, because youre trying nto make a computer behave like a human
-
July 22nd, 2006, 10:55 AM
#9
Re: Simulate Mouse Click
Ya I have the webbrowers on the form already. But need it to simulate what a real person would do for it to work. I made this before with VB 6.0, worked fine, but I wanted to make another version for C# .Net.
-
July 22nd, 2006, 08:01 PM
#10
Re: Simulate Mouse Click
Aright I tried
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam,
IntPtr lParam);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
Win32APICall.SendMessage(Handle, Win32APICall.MOUSEEVENTF_LEFTDOWN,UIntPtr.Zero, IntPtr.Zero);
Win32APICall.SendMessage(Handle, Win32APICall.MOUSEEVENTF_LEFTUP, UIntPtr.Zero, IntPtr.Zero);
But it just immediatly closes the project after clicking the button. No error or anything, just closes it. Mouse doesn't move either. Any ideas?
-
July 22nd, 2006, 08:48 PM
#11
Re: Simulate Mouse Click
Originally Posted by MadHatter
call the button's PerformClick method.
^^^ you're sending a message that's already wrapped in the above method...
-
July 22nd, 2006, 08:54 PM
#12
Re: Simulate Mouse Click
Ya so I tried putting that in the form_load. All that happens is the form pops up and closes right away. So I just commented it out. Also tried the
public void makeStackOverflow()
{
makeStackOverflow();
}
And used it right after the PerformClick(), and it says theres an error with an infinate loop.
-
July 22nd, 2006, 09:49 PM
#13
Re: Simulate Mouse Click
if you're clicking a button, then use the method.
if you need to click on a particluar place on the form then use send message to send a WM_LBUTTONDOWN notification with the exact x/y location you should click. also you should send it to the control you're wanting to click on and not to the main form. if you have a web browser control, then try using its handle when you call it.
whatever you do, dont have an event handler for your button click event, that p/invokes another button click on the same button you are handling the click event with another click invoker.
I dont understand how thats not absolutely clear by this point.
the makeStackOverflow() was humor pointed at you for recursively calling the same thing over and over and over and over and over again. perform click, sends the send message for a button click. when a button is clicked it calls the Click event handler in which you put the send message notification to click the button that you just clicked. so by clicking your button you start an infinite kludge of notifications being generated.
-
July 23rd, 2006, 12:29 AM
#14
Re: Simulate Mouse Click
Originally Posted by JBudOner
Ya so I tried putting that in the form_load. All that happens is the form pops up and closes right away. So I just commented it out. Also tried the
public void makeStackOverflow()
{
makeStackOverflow();
}
And used it right after the PerformClick(), and it says theres an error with an infinate loop.
LOL This is the most funny optimization I've ever heard of!
-
July 23rd, 2006, 01:08 AM
#15
Re: Simulate Mouse Click
=/
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam,
IntPtr lParam);
public const int WM_NCLBUTTONDOWN =0x00A1;
public const int WM_NCLBUTTONUP = 0x00A2;
private void button1_Click(object sender, EventArgs e)
{
Win32APICall.SendMessage(Web.Handle, Win32APICall.WM_NCLBUTTONDOWN, 544, 472);
Win32APICall.SendMessage(Web.Handle, Win32APICall.WM_NCLBUTTONUP, 544, 472);
}
So this is what I came out to now. Web being the name of the WebBrowser. Not sure if, 'WM_LBUTTONDOWN' is different from, 'WM_NCLBUTTONDOWN'. I checked around for a number that it would be, but couldn't find any. I've got an error when trying to run it, "Cannot convert from 'int' to 'System.UIntPtr'," and, "Cannot convert form 'int' to 'System.IntPtr'." I've got one of both of these for both the SendMessages. Any ideas?
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
|