pnda
pnda

Reputation: 135

PostMessage mouse click to background window

I've seen a lot of threads about this directing to SendInput, but that doesn't work in this case.

I'm trying to send a mouse click to a certain location to a background window (game).

I can successfully send a mouse click to a background window using PostMessage, but I need to externally set the mouse position for it to work.

[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;

PostMessage(hWnd, WM_LBUTTONDOWN, 1, 0);
PostMessage(hWnd, WM_LBUTTONUP, 0, 0);

I've also tried extending with point

public int MakeLParam(int LoWord, int HiWord)
{
    return (int)((HiWord << 16) | (LoWord & 0xFFFF));
}

PostMessage(hWnd, WM_LBUTTONDOWN, 1, MakeLParam(pt.X, pt.Y));

But nothing seems to work. Is it possible to set location via PostMessage or do I have to do it externally, then click, then set cursor back to original position?

Any help is appreciated.

Upvotes: 0

Views: 20349

Answers (2)

Ahmad headr
Ahmad headr

Reputation: 41

you need to change your location op point like this :

 PostMessage(pl, WM_LBUTTONDOWN, 1,MakeLParam(70,20));
        PostMessage(pl, WM_LBUTTONUP, 0,MakeLParam(71,21));

Upvotes: 1

J3soon
J3soon

Reputation: 3143

pt.X and pt.Y means the position in the hWnd, not the position in the screen.
Because rest of your code works pretty well.

Upvotes: 2

Related Questions