Search code examples
windowsdelphifiremonkey

Delphi: SendMessage don't send to FMX


I successfully send message cross apps. But the code doesn't work in FMX. I can find the FMX form but no message receive at dest.

Sender code:

  CDS.dwData:= 0;   //Identify message
  CDS.cbData:= ByteLength(Str);
  CDS.lpData:= PChar(Str);

  if DstHandle=0 then
    DstHandle := Winapi.Windows.FindWindow(nil, PChar(TargetFormCaption));

  if DstHandle<>0 then
  begin
    Res := SendMessage(DstHandle, WM_COPYDATA, Handle, NativeInt(@CDS));
    Result:= True;
  end
  else
    Result:= False;

Result is true but WMGetData is not triggered. Receiver code:

procedure WMGetData(var Msg : TWMCopyData) ; message WM_COPYDATA;
...
procedure TForm3.WMGetData(var Msg: TWMCopyData);
begin
  Caption:= 'Got something !';
end;

Solution

  • Forms in FMX are not able to receive messages in the same way as VCL forms. FMX does not dispatch window messages that FMX does not use itself.

    The clean way to approach this is to use AllocateHWnd to create a window that can receive your messages. Even for VCL apps that is the right approach because such a window is not subject to re-creation.