I'm trying to learn mouse/keyboard emulation using win api.
I found out that it is possible to emulate button click using sendmessage() function.
Well, I get coordinates of "play" button (it is (60;100)) and trying to push this button using the following code:
int x = 60;
int y = 100;
int lParam = ((x << 16) | (y & 0xffff));
int parentWindow = FindWindow("BaseWindow_RootWnd", "Main Window");//get main winamp window
MessageBox.Show(parentWindow.ToString());//failed if 0
SendMessage(parentWindow, WM_LBUTTONDOWN, IntPtr.Zero, new IntPtr(lParam));//send left mouse button down
SendMessage(parentWindow, WM_LBUTTONUP, IntPtr.Zero, new IntPtr(lParam));//send left mouse button up
But this code has no effect on the winamp.
Can anybody point me on the mistakes i made? Any help is greatly appreciated!
ps It's not applicable for me to move mouse to the winamp play button, than do a click, than move it back.
Also for winamp it is impossible to get button's handle. With button handle SendMessage() works fairly well, but with coordinates it doesn't at all.
ADDITION #1
Well, the code above activates winamp window and shows it, if it was minimized. But play button stil don't want to be pushed ;(
ADDITION #2
Here are messages I get after I execute the code above.
To let Winamp play/pause the current song, simply send it a Play/Pause message, which is long 40046 as defined in winamp.h
. You'll have to send it in a WM_WA_IPC
message (which is a user message, also known as WM_USER
) instead of a WM_LBUTTONDOWN
.
So, something like this should do:
SendMessage(parentWindow, WM_USER, IntPtr.Zero, new IntPtr(40046));
well, to simulate mouse click in winamp window you need to do exactly what is happening when you just click it with hardware mouse. The following was found out from spy++:
So to emulate mouse click we need to:
PostMessage()
instead of SendMessage()
because "P" in the screenshot above tells us that the message was got from the PostMessage() (according to msdn ) 005E09FA
) WM_LBUTTONDOWN = 0x201
) MK_LBUTTON = 0x1
(It seems, it is necessary to pass it. It would be great if somebody can explain why) ((Y << 16) | X)
) 005E09FA
) WM_LBUTTONUP = 0x202
) 0
or IntPtr.Zero
((Y << 16) | X)
) Thanks everyone for their input!
您应该使用SendInput()
将输入发送到其他应用程序,而不是伪造窗口消息。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.