Re: Pass data via messages
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Mon, 30 Oct 2006 22:18:26 -0600
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
.
- References:
- Pass data via messages
- From: Sidney Egnew
- Pass data via messages
- Prev by Date: Re: Delphi App with ActiveX fails under Vista
- Next by Date: Re: Newbie looking for info on basic graphics with Delphi.
- Previous by thread: Pass data via messages
- Index(es):
Relevant Pages
|