Simulate Mouse without Requiring Control

I'm trying to simulate a mouse click within a Client Context and only using the X/Y coordinates of it's context. There are few ways that I know of to actually use the mouse within the WinAPI.

One way is SendInput, using and setting input flags within a simple function as I have here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void LeftClick ( )
{  
  INPUT    Input={0};
  // left down 
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
  ::SendInput(1,&Input,sizeof(INPUT));

  // left up
  ::ZeroMemory(&Input,sizeof(INPUT));
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
  ::SendInput(1,&Input,sizeof(INPUT));
}


I also can move the mouse by getting the system metrics and a little calculation within this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
void MouseMove (int x, int y )
{  
  double fScreenWidth    = ::GetSystemMetrics( SM_CXSCREEN )-1; 
  double fScreenHeight  = ::GetSystemMetrics( SM_CYSCREEN )-1; 
  double fx = x*(65535.0f/fScreenWidth);
  double fy = y*(65535.0f/fScreenHeight);
  INPUT  Input={0};
  Input.type      = INPUT_MOUSE;
  Input.mi.dwFlags  = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
  Input.mi.dx = fx;
  Input.mi.dy = fy;
  ::SendInput(1,&Input,sizeof(INPUT));
}


But what I'm trying to do is simulate this same function of using the LeftClick() function, but without the need of controlling the users mouse. This is needed for a program that runs minimized and not in the users foreground to simulate tedious task that take up time during the a users day.

I prefer sticking to WindowsAPI as I'm most comfortable with it, but if anyone has any insight I would much appreciate it.

actually use the mouse

I don't think SendInput actually uses the mouse (or the keyboard). It just generates events which end up in the same queue as those generated by the (actual) mouse and keyboard.

Anyway...

Well, I suppose you could get the required HWND or thread ID (for the thread the message pump is running on) and then you could try posting the appropriated messages to the app (using either PostMessage or PostThreadMessage): WM_LBUTTONDOWN, WM_MOUSEMOVE, ...

BUT I think the fact that the app is minimized is going to be a problem. If you need to click on a button by moving "the mouse" and the "clicking on" a button, then the app cannot be mimimized.

If you are trying to click on a button or menu item, it might be possible (and better) to just post the corresponding WM_COMMAND message to the app.

Andy
Either way, if I'm going to send a WM_COMMAND to an app be for a control on it. There seems to be two different ways, either that way or send an event in the CLIENT CONTEXT space of the application. I googled and can't find anything on it and I know thats what I need to do.
Topic archived. No new replies allowed.