4

I have my function here working, but I am certainly going about it the wrong way.

My program uses FindWindow to find the correct window. I need to double click on a specific location on this window.

I made it work by always putting the window in the same location on the screen, but if I moved the window the program would try click on the hard-coded location I have provided and it would not work.

Here is the function:

void lobbyWindow(HWND main_client)
{
  //RECT arect;

   // GetWindowRect(main_client, &arect); 

    SetCursorPos(748,294);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

}

As you can see I just move the mouse to 748,294 and double click. What I want to do is set the mouse to 100,100 in the main_client window, so if I move the main_client window the mouse still clicks on the correct spot.

r-s
  • 1,035
  • 2
  • 11
  • 21
  • You're probably looking for [Microsoft Active Accessibility](http://msdn.microsoft.com/en-us/library/windows/desktop/dd373592.aspx). – IInspectable Aug 05 '13 at 14:09

3 Answers3

7

Use SendInput() instead, then you can use flags to move the cursor relative to the window -

Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;

  1. How i can simulate a double mouse click on window ( i khow handle) on x, y coordinate, using SendInput?
  2. http://www.cplusplus.com/forum/windows/97017/
Community
  • 1
  • 1
Patt Mehta
  • 4,110
  • 1
  • 23
  • 47
3

If the window you're clicking in is in another thread/process this approach is fundamentally flawed, because the window can move while you're sending the clicks - meaning even if you have the right position to start with there's no guarantee all the clicks will end up in the same place.

Having said that, you can convert a client-relative coordinate to screen-coordinates using the ClientToScreen API function:

POINT pt = { 100, 100 };
ClientToScreen(main_client, &pt);

Depending on the target window you may find you can simply post it a WM_LBUTTONDBLCLK message to simulate the input at the appropriate position:

PostMessage(main_client, WM_LBUTTONDBLCLK, MK_LBUTTON, MAKELPARAM(100, 100));
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
2

SetCursor() requires screen coordinates, so you need to calculate where your double-click would be in screen coordinates relative to the window's current screen location. You can do that by either:

  1. using GetWindowRect() to retrieve the window's current screen coordinates, then offset that by the intended relative coordinates.

  2. use ClientToScreen() or MapWindowPoints() to convert relative coordinates to screen coordinates.

Once you have the intended screen coordinates, you can pass them to SetCursor().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770