从Delphi组件捕获WM_COPYDATA [英] Catch WM_COPYDATA from Delphi component

查看:355
本文介绍了从Delphi组件捕获WM_COPYDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个组件,通过WM_COPYDATA在应用程序之间发送字符串消息。
我想陷阱WM_COPYDATA,但这不工作:

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;

搜索Google很多,使用wndproc找到一些参考。我尝试了,但它也不工作。

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?

推荐答案

到目前为止,您所拥有的是好的,但您需要首先安排将邮件发送到您的组件。这需要一个窗口把手。致电 AllocateHWnd 并通过它是您的组件的 WndProc 方法。它将返回一个窗口句柄,当您的组件被销毁时,它将被破坏。

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's WndProc method. It will return a window handle, which you should destroy as your component is destroyed.

constructor TMyMessage.Create(AOwner: TComponent);
begin
  inhreited;
  FHandle := AllocateHWnd(WndProc);
end;

destructor TMyMessage.Destroy;
begin
  DeallocateHWnd(FHandle);
  inherited;
end;

而不是直接测试每条消息,您可以让 TObject 为你做这个这就是 调度 方法是。传递一个 TMessage 记录,它将为您找到并调用相应的消息处理程序方法。如果没有这样的处理程序,它将调用 DefaultHandler 。覆盖可以调用 DefWindowProc

Rather than testing for each message directly, you can let TObject do that for you. That's what the Dispatch method is for. Pass it a TMessage record, and it will find and call the corresponding message-handler method for you. If there is no such handler, it will call DefaultHandler instead. Override that can call 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;

这篇关于从Delphi组件捕获WM_COPYDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆