I'm trying to write a component, to send string messages between applications by WM_COPYDATA. I'd like trap the WM_COPYDATA, but this doesn't work:
TMyMessage = class(TComponent)
private
{ Private declarations }
…
protected
{ Protected declarations }
…
procedure WMCopyData(var Msg : TMessage); message WM_COPYDATA;
…
end;
Searching Google a lot, found some reference using wndproc. I tried it, but it isn't working either.
TMyMessage = class(TComponent)
…
protected
{ Protected declarations }
…
procedure WMCopyData(var Msg : TMessage); message WM_COPYDATA;
procedure WndProc(var Msg: TMessage);
…
end;
…
procedure TMyMessage.WndProc(var Msg: TMessage);
begin
//inherited;
if Msg.Msg = WM_COPYDATA then
WMCopyData(Msg);
end;
Please help, what is wrong?
What you have so far is fine, but you need to arrange for messages to be delivered to your component in the first place. That requires a window handle. Call
AllocateHWnd
and pass it your component'sWndProc
method. It will return a window handle, which you should destroy as your component is destroyed.Rather than testing for each message directly, you can let
TObject
do that for you. That's what theDispatch
method is for. Pass it aTMessage
record, and it will find and call the corresponding message-handler method for you. If there is no such handler, it will callDefaultHandler
instead. Override that can callDefWindowProc
.Your problem is that
TComponent
is not a windowed component.WM_COPYDATA
is a windows message and is delivered via a window procedure. Hence you need a window handle. UseAllocateHwnd
to get hold of one of these.Whatever is sending the messages will need to find a way to get hold of the window handle.
I did it this way:
My web modules which are running in a thread need to send strings to a memo on the main form. FReceiverFromWS is a THandle
On create:
To send messages:
In the main form, public method
procedure WMCopyData(var Msg : TWMCopyData) ; message WM_COPYDATA;
is:
(As you can see, I actually use dwData to signal the kind of message and handle these differently)