I tried for example to send WM_LBUTTONDOWN in order to simulate left mouse button click, but the target program didn't react to this even it received it.
What reaction were you expecting, or hoping for on a left mouse button?
If to bring the NotePad to the top window the use the following:
Code:
SetForegroundWindow( hTargetWnd );
If to set the cursor position then WM_LBUTTONDOWN will work, but only the x y coordinates within the text region, the following will set the cursor coords to x=0 y=0 (top left of window):
Code:
PostMessage(hTargetWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(0,0));
PostMessage(hTargetWnd, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(0,0));
The following code example will bring the NotePad to the front and write 3 lines the insert some text into line 1:
Code:
void SendString(HWND h, char *text)
{
int len = strlen(text);
for(int i = 0; i < len; i++)
PostMessage(h, WM_CHAR, text[i], 0);
}
void SetCursor(HWND h, short x, short y )
{
PostMessage(h, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x,y));
PostMessage(h, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x,y));
}
int _tmain(int argc, _TCHAR* argv[])
{
...
SetForegroundWindow(hTargetWnd);
SetCursor(hTargetWnd, 0,0);
SendString(hTargetWnd, "Hello World line 1\n");
SendString(hTargetWnd, "Hello World line 2\n");
SendString(hTargetWnd, "Hello World line 3\n");
SetCursor(hTargetWnd, 20,0);
SendString(hTargetWnd, "(Inserted)");
...
}