我正在尝试编写一个组件,通过 WM_COPYDATA 在应用程序之间发送字符串消息。
我想捕获 WM_COPYDATA,但这不起作用:
TMyMessage = class(TComponent)
private
{ Private declarations }
…
protected
{ Protected declarations }
…
procedure WMCopyData(var Msg : TMessage); message WM_COPYDATA;
…
end;
谷歌搜索了很多,找到了一些使用wndproc的参考。我尝试过,但它也不起作用。
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;
请大家帮帮忙,有什么问题吗?
到目前为止,您所拥有的一切都很好,但您需要首先安排将消息传递到您的组件。这需要一个窗口句柄。称呼AllocateHWnd http://docwiki.embarcadero.com/VCL/en/Classes.AllocateHWnd并将其传递给您的组件WndProc
方法。它将返回一个窗口句柄,您应该在组件被销毁时销毁该窗口句柄。
constructor TMyMessage.Create(AOwner: TComponent);
begin
inhreited;
FHandle := AllocateHWnd(WndProc);
end;
destructor TMyMessage.Destroy;
begin
DeallocateHWnd(FHandle);
inherited;
end;
您可以让TObject
为你做那件事。这就是Dispatch http://docwiki.embarcadero.com/VCL/en/System.TObject.Dispatch方法是为了.传递一个TMessage
记录,它会为你找到并调用相应的消息处理方法。如果没有这样的处理程序,它将调用DefaultHandler
反而。覆盖可以调用的DefWindowProc
.
procedure TMyMessage.WndProc(var Message);
begin
Dispatch(Message);
end;
procedure TMyMessage.DefaultHandler(var Message);
begin
TMessage(Message).Result := DefWindowProc(Self.Handle, TMessage(Message).Msg,
TMessage(Message).WParam, TMessage(Message).LParam);
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)