Thread: Moving the mouse with SendMessage / PostMessage

  1. #1
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44

    Moving the mouse with SendMessage / PostMessage

    Hi,
    I have a few questions:

    1. What is the difference between SendMessage and PostMessage?

    2. Am I using SendMessage correctly? I want to emulate mouse movement in a certain window while still being able to use the computer.

    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main() {
        POINT btn;
        HWND mWindow;
        cout<<"Set Mouse Over button and press ENTER";
        cin.get();
        GetCursorPos(&btn);
        cout<<"\nAfter Pressing ENTER, you have 5 seconds to set focus to the correct window.";
        cin.get();
        Sleep(5000);
        mWindow = GetFocus();
    
       SendMessage(mWindow,WM_MOUSEMOVE,0,MAKELPARAM(btn.x,btn.y));
       SendMessage(mWindow,WM_LBUTTONDOWN,0,MAKELPARAM(btn.x,btn.y));
       SendMessage(mWindow,WM_LBUTTONUP,0,MAKELPARAM(btn.x,btn.y));
       return 0;
    }
    Please DO NOT suggest SendInput / mouse_event. They do not do what I am looking for. (AKA they hijack the mouse)

    Thanks!
    Last edited by comwiz; 07-31-2007 at 10:22 PM.

  2. #2
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    The SendMessage function sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.
    The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.
    Does it work? It looks like your calls to SendMessage are correct, and it looks like it would work, but looks can be deceiving.
    Last edited by Bleech; 07-31-2007 at 10:30 PM. Reason: your*

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  3. #3
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    No, it doesn't. For example, I tested it with notepad. I shrunk the notepad window so I just saw the Toolbar, put the console window on the other side of my screen. I set my mouse over the (X) on the notepad window and pressed enter. I pressed enter again and Clicked on the notepad window to set focus to it. Waited 5 seconds and my program just exited. It did not click the button.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    1. PostMessage() does not wait for the message to be processed.

    2. The messages WM_MOUSEMOVE, WM_LBUTTONDOWN, WM_LBUTTONUP take coordinates in client area of the window. Therefore, I don't think it is possible to hit the (X) with them. (Since the (X) is not within the client area of the window.) Try the corresponding WM_NC* messages. (ie, WM_NCLBUTTONDOWN)

    Why not just send a WM_CLOSE?

    Edit : Just a note, the WM_NC* messages take slightly different parameters... be sure to look the messages up first.
    Last edited by Cactus_Hugger; 08-01-2007 at 12:26 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    Just tried it again with a button inside of my web browser. I don't know what is wrong. Everything builds w/o error.

    Another question:
    Does the cursor just have to be inside the client area of the application inorder for GetCapture to retrieve the window handle? Or does it have to have focus?

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by comwiz View Post
    Please DO NOT suggest SendInput / mouse_event. They do not do what I am looking for. (AKA they hijack the mouse)
    They "hijack" your mouse for a microsecond, yes...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    I Write C++ Apps, Sue Me.
    Join Date
    Feb 2006
    Location
    In My Computer
    Posts
    44
    Quote Originally Posted by maxorator View Post
    They "hijack" your mouse for a microsecond, yes...
    It's not so much the mouse hijacking, I want to be able to still use my computer while still sending mouse info the the desired window.

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    What about mouse_event?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving objects with mouse in 3D.
    By psychopath in forum Game Programming
    Replies: 15
    Last Post: 07-10-2011, 04:20 PM
  2. Moving mouse
    By Bill83 in forum Windows Programming
    Replies: 1
    Last Post: 04-02-2006, 08:58 AM
  3. Replies: 2
    Last Post: 03-23-2006, 07:08 AM
  4. mouse click coords and moving a rect
    By techrolla in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2004, 09:49 PM
  5. Moving a Static box with the mouse
    By AtomRiot in forum Windows Programming
    Replies: 4
    Last Post: 06-26-2003, 11:34 PM