delphimessagewm-copydata

How can I receive message (WM_COPYDATA)?


I am sending message WM_COPYDATA to my application which has a lot of forms, but I cannot receive it. Simple Application with one form works fine.

I have this code in main form. No message received

private
 procedure ReceiveMessage(var Msg: TWMCopyData); message WM_COPYDATA;
...
procedure TForm1.ReceiveMessage;
begin
  ShowMessage(PAnsiChar(Msg.CopyDataStruct.lpData));
end;

I checked Application and main form handles. So handles looks like:

Form1.Handle (main form) = 3348672
FindWindow result = Application.Handle = 7148290
FindWindowEx(Application.Handle,0,nil,nil) = 0 

I read here(Delphi: What is Application.Handle?) in Don's answer that messages which were sent to Application handle are redirected to main form, but in my situation it doesnt happen. How can I receive messages? Why messages are not redirected to main form?

Testing under Delphi XE8, Windows 10


Solution

  • Except the fact that you don't provide any real code that you need help with, your question is probably wrong. If I assume correctly, you can send the message, you can recieve it but you send it to wrong place.

    If your form has dynamic title, you can find its handle like this. Set some fixed part of the title "MyWindowTitle" and find that with this function.

    function FindFormHandle: HWND;
    var
      NextHandle: HWND;
      NextTitle: array[0..260] of char;
    begin
      Result:=0;
      NextHandle := GetWindow(Application.Handle, GW_HWNDFIRST);
    
      while NextHandle > 0 do
      begin
        GetWindowText(NextHandle, NextTitle, 255);
    
        if Pos('MyWindowTitle', NextTitle)>0 then
          Exit(NextHandle);
    
        NextHandle := GetWindow(NextHandle, GW_HWNDNEXT);
      end;
    end;