Thread: Simulating mouse movement in other windows

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Simulating mouse movement in other windows

    Hi

    Is it possible to write a program that can control the mouse movement, as well as being able to interact with other windows such as Internet explorer by simulating mouse clicks?

    How would I go about doing this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you can. You need to find the window handle of that application, then you can use postmessage to that handle.

    Try googling.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Moved to Win32, since you seem to be interested in how to do it in Win32. (And it's highly platform-specific anyway.)

    The answer is yes, it's possible. The main API you'll want to look at is SendInput().

    However, it is not a good way of interacting. In fact, it's a horrible way. It's hard to identify what window you're interacting with. It's hard to identify what part of the window you're interacting with. Most such programs expect controls to be in specific places, and then they don't work because the user changed the way the controls are arranged, or perhaps it's a different language version and the arrangement is different because labels have different length. Or you try to activate user elements that are disabled, but you don't detect it, with the expected disastrous results. It's worse if you try to send command messages directly - several existing apps are known to crash other applications because they send command messages for controls that are inactive, and the other application wasn't ready to handle this.

    In other words, it's possible, but you really shouldn't do it.

    Many applications provide some sort of scripting interface to control them in a proper way. IE does. All Office applications do. Look into Automation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    If you want an easy solution, take a look at WinBatch ($100 USD and up).

    In the past I've used it to automate some "nightly processes" that were not designed to be automated.

    WinBatch is well documented and easy to learn. An experienced (intermediate) programmer can probably learn to use it in one day. It takes months to learn how to program with the WinAPI! ...It won't take that long to learn how to move & click the mouse, but it will take ten times as long as learning to do it with WinBatch.

    There are other Windows automation tools, maybe even some free ones. I don't remember exactly why I chose WinBatch, and I can't compare it to these other tools... I bought it several years ago. It was rock-solid, it did everything I needed, and all of the functions were clearly documented in printed manuals. (I don't know if it still comes with printed manuals.)

    Windows actually comes with Windows Scrip Host (WSH). When I was looking into automation you couldn't use it to automate the mouse, but maybe they've improved it.


    Here's a good WinAPI Programming Tutorial. I'm not sure if it will tell you how to move the mouse pointer and simulate a click... Most programs don't do that. And, I'm pretty sure you have to do some additional research to learn how to send mouse messages to another application... Most programs don't do that either! If you work through the tutorial, and then do some research on MSDN, you can probably figure it out.

    BTW - I don't know how to do this with the WinAPI. After several years of part-time Windows programming, I'm still a novice!
    Last edited by DougDbug; 10-05-2007 at 06:25 PM.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    I thought this would go along with the topic...

    Would inputting text into a dialog box of another window be using the same thing? Say, for example, we have a box that comes up and asks for a password. Is there a way to have your program to put the text in the box? Thanks.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes,

    SetWindowText(GetDlgItem(<THE WINDOW HWND>, <THE CONTROL ID>), <THE TEXT>);

    Use Spy++ or whatever it is to get the window control id.

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Yes, it's WinSpy++.

  8. #8
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Which source file gets the handle to the window?

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The handle to the window will be different every time it's created, so you'll have to use something like FindWindow(), see MSDN.

  10. #10
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    And how would i get the control id?

  11. #11
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  12. #12
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Oh, that's easy thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  2. What would you like in a interface?
    By commanderrulz in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-29-2002, 05:29 AM
  3. Movement of the mouse
    By nep in forum Windows Programming
    Replies: 1
    Last Post: 05-19-2002, 05:32 AM
  4. Need to read mouse movement in program
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2002, 03:15 PM