Re: Pass data via messages



Sidney Egnew wrote:
The following web page has good examples of how to pass data via
custom window messages:

http://www.cryer.co.uk/brian/delphi/howto_send_custom_window_message.htm

Here is a sample of the code:

SendMessage(LinkCheckForm.Handle,WM_ANO_MESSAGE,Integer(self),0);

and at the recipient end:

procedure TMyForm.OnAnoMessage(var Msg: TMessage);
var
myObject: TMyClass;
begin
myObject := TMyClass(msg.WParam);

The problem is that I have only been able to get this to work for
simple objects. Here is an example:

type
TLogMsg = record
MessageText: String;
end;

// Sending
LogMsg.MessageText := GetFileName(S);
SendMessage(Application.MainForm.Handle,WM_Log,Integer(LogMsg),0);

That's not an object. That's a record. Totally different animal, especially since it has a field of type string.

// Receiving
procedure TfmMain.OnLog(var Msg: TMessage);
begin
LogMsg := TLogMsg(Msg.WParam);
LogMemo.Lines.Add(LogMsg.MessageText);
end;

This works fine. However if I try to use a more complex object I get
an "Invalid typecast" error in the SendMessage when I try to compile.

TProgressMsg = record
Module: String;
Count: Integer;
end;

// Sending
ProgressMsg.Module := 'Hi There';
ProgressMsg.Cnt := 1;
SendMessage(Application.MainForm.Handle,WM_Progress,Integer(ProgressMsg),0);

Is it possible to pass this more complicated object using WParam and
LParam?

WParam and LParam are four bytes each today. If you can fit your data into eight bytes, then go ahead.

On the other hand, since the messages you're sending are within your own application, and they're being transmitted using SendMessage and not PostMessage, you really don't need to use messages at all. You can add a method to the receiving form and then call that method with whatever parameters you need.

--
Rob
.



Relevant Pages

  • Re: Pass data via messages
    ... procedure TMyForm.OnAnoMessage(var Msg: TMessage); ... myObject: TMyClass; ... method to the receiving form and then call that method with whatever ...
    (comp.lang.pascal.delphi.misc)
  • Pass data via messages
    ... procedure TMyForm.OnAnoMessage(var Msg: TMessage); ... myObject: TMyClass; ...
    (comp.lang.pascal.delphi.misc)
  • Re: String --> Pointer --> LongInt
    ... > procedure TForm1.OnMsg1(var Msg: TMessage); ... If S is an empty string, ... but from your statement that it "worx fine" not what happens. ...
    (alt.comp.lang.borland-delphi)
  • Re: Refresh eines Objektes, aber wie?
    ... procedure WndProc(var MsgRec: TMessage); ... procedure Button1Click(Sender: TObject); ... Msg: TMsg; ...
    (de.comp.lang.delphi.misc)