有没有办法向一组自定义的非可视化类广播自定义的消息?高手来帮忙啊
我的程序如下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Unit2, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FPluginRegistry : TPluginRegistry;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FPluginRegistry := TPluginRegistry.Create;
RegisterWindowMessage('WM_DESERIALIZATION');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//如果我用这句就可以接收到自定义的消息
// PostMessage(FPluginRegistry.WindowHandle,WM_DESERIALIZATION,0,0);
//如果用这句就不行,怎么样才能实现广播呢?
PostMessage(HWND_BROADCAST,WM_DESERIALIZATION,0,0);
end;
end.
unit Unit2;
interface
uses Classes, Windows, Messages,Dialogs;
const
WM_DESERIALIZATION = WM_USER + 100;
type
TPluginRegistry = class(TPersistent)
private
FWindowHandle: HWND;
procedure WndProc(var Msg: TMessage);
protected
procedure ProcessSaveProperty; virtual;
public
constructor Create;
destructor Destroy; override;
property WindowHandle : HWND read FWindowHandle;
end;
implementation
uses Forms;
{ TPluginRegistry }
constructor TPluginRegistry.Create;
begin
inherited Create;
FWindowHandle := Classes.AllocateHWnd(WndProc);
end;
destructor TPluginRegistry.Destroy;
begin
Classes.DeallocateHWnd(FWindowHandle);
inherited;
end;
procedure TPluginRegistry.ProcessSaveProperty;
begin
Showmessage('Received');
end;
procedure TPluginRegistry.WndProc(var Msg: TMessage);
begin
with Msg do
if Msg = WM_DESERIALIZATION then
try
ProcessSaveProperty;
except
Application.HandleException(Self);
end
else
Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
end;
end.