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?
nirkrAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jhattinghConnect With a Mentor Commented:
In the following code, a window is found by specifying the window title text and then the relevant button is pressed:


      //------------ 1st - find the window ------------//

      HWND hDlg = ::FindWindowEx(NULL, NULL, m_lpszClass, (LPCTSTR)m_strWindowText);


      //------------ 2nd - find and click the control ------------//

      if (hDlg)      
      {
            HWND hControl = 0;
            hControl = ::GetDlgItem(hDlg, m_nControlID);

            if ( ::PostMessage(hDlg, WM_COMMAND, (WPARAM)((BN_CLICKED*0x10000)+m_nControlID), (LPARAM)hControl) )
                  if ( ! m_bLurk)      
                        return true;
      }
0
 
MichaelSCommented:
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.
0
 
mjswartCommented:
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.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
abk102299Commented:
I think that windows hooks are usually used for mouse & keyboard events simulation (JournalRecordProc, JournalPlaybackProc, etc.)
0
 
PriyeshCommented:
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
0
 
MichaelSCommented:
To Priyesh. Are you sure that SendMessage() won't work? It sounds at least new for me.
0
 
PriyeshCommented:
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.
0
 
MichaelSCommented:
Didn't try it. Just used WM_HOTKEY and it works.
0
 
mjswartCommented:
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.
0
 
MichaelSCommented:
WM_COMMAND works aswel.
0
All Courses

From novice to tech pro — start learning today.