Link to home
Start Free TrialLog in
Avatar of nirkr
nirkr

asked on

SendMessage WM_LBUTTONDOWN to another window

Hello,
i want to send a message to another window(program - which i know only it's name - i don't have it's hwnd)

this is the code i wrote:
SendMessage((long)HWND_BROADCAST,WM_LBUTTONDOWN);
      Sleep(3000);
      SendMessage((long)HWND_BROADCAST,WM_LBUTTONUP);

like u see - i want to send after few seconds another message -(simulates a left button down for 3 sec on another window-application)?

help please?
Avatar of MichaelS
MichaelS

You have to find out window and send it directly to destination. Use Spy to figureout the window class and FindWindow() api to get the handle.
also you must determine which SendMessage version you're using:

LRESULT SendMessage(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
 
or

LRESULT SendMessage(
UINT message,
WPARAM wParam = 0,
LPARAM lParam = 0
);

The first one is a function (::SendMessage) which takes four parameters. The second one is a method of CWnd which takes 1-3 parameters) and works solely on the object window.
I think that windows hooks are usually used for mouse & keyboard events simulation (JournalRecordProc, JournalPlaybackProc, etc.)
No MichaelS and mjswart,
   A direct sendmessage from one application to another won't work unless you send WM_COPYDATA or send to HWND_BROADCAST which will be recieved by all windows. In this case, the programmer wants to send LBUTTONDOWN and LBUTTONUP. These are windows messages and not user messages. So when send to HWND_BROADCAST it will create undesirable effects in other windows present in the system.
   Tell me nirkr, are you working under windows NT? If so, you could create a remote thread in the target app and try sending messages from that thread. The whole idea is that you need to be in the same address space to send windows messages direct. Or you have to send IPC messages and take appropriate action in the target application. If you are the owner of the target application, you can try with WM_COPYDATA.
Priyesh
ASKER CERTIFIED SOLUTION
Avatar of jhattingh
jhattingh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To Priyesh. Are you sure that SendMessage() won't work? It sounds at least new for me.
Yes michael,
   You cannot send general windows messages between applications. You are thinking abt writing to the message queue of another application and it can be done by special ipc messages only (like the ones i mentioned in the last comment). You can try by finding another window in your system and sending a WM_LBUTTONDOWN to it.
Priyesh.
Didn't try it. Just used WM_HOTKEY and it works.
I've written to other app's message queues before.
I found out the command ID of a certain function (using spy and the app's own menu) and posted a message to the app. The app responded as if I had used the menu.
WM_COMMAND works aswel.