CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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.

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Simulate Mouse Click

    call the button's PerformClick method.

  3. #3
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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? =(

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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();
      }
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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. =(

  7. #7
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    Re: Simulate Mouse Click

    *Bump* Any suggestions guys? =( Still can't get it to work.

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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.

  10. #10
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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?

  11. #11
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Simulate Mouse Click

    Quote Originally Posted by MadHatter
    call the button's PerformClick method.
    ^^^ you're sending a message that's already wrapped in the above method...

  12. #12
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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.

  13. #13
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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.

  14. #14
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Simulate Mouse Click

    Quote 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!
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  15. #15
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    40

    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?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured