Thread: Sending a mouse message to the desktop?

  1. #1
    jakar
    Guest

    Sending a mouse message to the desktop?

    Hi,

    I want my program to send mouse clicks to whatever the mouse is currently pointing to. I thought this could be accomplished by sending the appropriate mouse message to the desktop window, so I came up with this:

    HWND lpDesktopWindow = ::GetDesktopWindow();

    ::SendMessage(lpDesktopWindow, WM_LBUTTONDOWN, 0, 0);

    Unfortunately this doesn't seem to have any effect. Is it not working because I am passing 0 in place of the mouse coordinates? If this is the reason, how can I get the mouse coordinates? I can't monitor WM_MOUSEMOVE because the mouse isn't moving over my application. If the desktop gets the message with the proper coordinates will it then send the message to the application that the mouse is pointing to?

    jakar

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Code:
    POINT ptWhereTheMouseIs;
    GetCursorPos(&ptWhereTheMouseIs);
    HWND hDeskTop=GetDesktopWindow();
    SendMessage(hDeskTop,WM_LBUTTONDOWN, ptWhereTheMouseIs.x, ptWhereTheMouseIs.y);
    It's probably a good idea to rename the POINT variable. Note that GetCursorPos returns the screen coords which is fine for the desktop. If you need client coords from that then look into ScreenToClient (and also ClientToScreen) to convert.

    Hope that helps.

  3. #3
    Unregistered
    Guest
    This looks like what I need. I had read in another post that 'GetCursorPos' wasn't the mouse position. I just found some documentation in MSDN saying that it is, so thanks for pointing back in the right direction there.

    SendMessage(hWnd, WM_LBUTTONDOWN, 0, lParam);

    This is the call I need to make. The last two 0s weren't the coordinates, just the last one. Now that I have the x and y values, how do I combine then into a double word? lParam is a double word, x is a word, and y is a word. The hi word of lParam is x, and the lo word of lParam is y, but how do I fill lParam. NOTE: I may be getting the hi and lo values of lParam mixed up, but that doesn't matter as long as I know how to combine them to make a DWORD.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    oops...sorry about that but i'm glad you have something that works for you.
    Last edited by Ken Fitlike; 01-24-2002 at 04:05 PM.

  5. #5
    jakar
    Guest
    OK, this seems to work, except you have to point to the client area. This means no pointing to the menu bar; min, max, close buttons; etc. Thanks

    POINT lMousePointer;

    GetCursorPos(&lMousePointer);

    HWND lpTargetWindow = ::WindowFromPoint(lMousePointer);

    ::ScreenToClient(lpTargetWindow, &lMousePointer);

    DWORD lMouseX;
    DWORD lMouseY;

    lMouseX = (DWORD)lMousePointer.x;
    lMouseY = (((DWORD)lMousePointer.y) << 16);

    LPARAM lParam;

    lParam = lMouseX + lMouseY;

    ::SendMessage(lpTargetWindow, WM_LBUTTONDOWN, 0, lParam);
    ::SendMessage(lpTargetWindow, WM_LBUTTONUP, 0, lParam);

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I think you might want to replace:
    Code:
    lParam = lMouseX + lMouseY;
    with
    Code:
    lParam = MAKELPARAM(lMouseX , lMouseY);

  7. #7
    jakar
    Guest
    Thanks again. I traced MAKELPARAM back to MAKELONG to see what it was doing. I didn't even think of doing an OR rather than adding. I don't think (my thinking is often wrong) it will make a functional difference, but it is a better way of doing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. Making a script language?
    By Blackroot in forum Game Programming
    Replies: 10
    Last Post: 02-16-2006, 02:22 AM
  3. Sending a message to parent from child process
    By maxorator in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 04:23 PM
  4. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM
  5. sending enter key message to window
    By mayhem in forum Windows Programming
    Replies: 2
    Last Post: 09-10-2003, 07:55 AM