如何向非可视控件发送自定义消息?非可视控件中如何响应自定义消息? (300分)

  • 主题发起人 主题发起人 baoling
  • 开始时间 开始时间
B

baoling

Unregistered / Unconfirmed
GUEST, unregistred user!
各位老师,向您讨教:
1、如何向非可视控件发送自定义消息?
2、非可视控件中如何响应自定义消息?
3、有没有办法向一组自定义的非可视化控件广播自定义的消息?
这些控件继承Tcomponent
 
请看:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=094911
http://www.delphibbs.com/delphibbs/dispq.asp?lid=195602
http://www.delphibbs.com/delphibbs/dispq.asp?lid=458041

应该够用了.
 
楼上的兄台:谢谢!
这些控件继承Tcomponent没有句柄,怎么向他们发送消息呀???
 
使用 FWindowHandle := AllocateHWnd(WndProc);获得一个句柄。

下面是我写的一个监视剪贴板变动的非可视控件,还没测试过,不知道对不对:
unit ZeroClipBoardSpy;
{
Web Site:http://myzeroworld.yeah.net
}


interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TZeroClipBoardSpy = class(TComponent)
private
FWindowHandle:THandle;

//next clipboard viewer handle
FNextViewerHandle:THandle;
fOnChange: TNotifyEvent;

fStarted:boolean;


//main procedure
procedure WndProc(var Msg: TMessage);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent);override;
destructor Destroy; override;

procedure StartSpy;
procedure StopSpy;
{ Public declarations }
published
property OnChange:TNotifyEvent read fOnChange write fOnChange;
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Zero Component', [TZeroClipBoardSpy]);
end;

{ TZeroClipBoardSpy }

constructor TZeroClipBoardSpy.Create(AOwner: TComponent);
begin
inherited;

FWindowHandle := AllocateHWnd(WndProc);

FNextViewerHandle:=0;

fStarted:=false;
end;

destructor TZeroClipBoardSpy.Destroy;
begin
inherited;

//call stop spy on destory
StopSpy;

//release handle
DeallocateHWnd(FWindowHandle);
end;

procedure TZeroClipBoardSpy.StartSpy;
begin
if not(fStarted) then
begin
fNextViewerHandle := SetClipboardViewer(fWindowHandle);
fStarted:=true;
end;
end;

procedure TZeroClipBoardSpy.StopSpy;
begin
if fStarted then
begin
ChangeClipboardChain(fWindowHandle, fNextViewerHandle);
fStarted:=false;
end;
end;

procedure TZeroClipBoardSpy.WndProc(var Msg: TMessage);
begin
case Msg.Msg of
WM_DRAWCLIPBOARD: //clipboard change
try
try
if assigned(fOnChange) then
fOnChange(Self);
finally
msg.Result := SendMessage(WM_DRAWCLIPBOARD, FNextViewerHandle, 0, 0);
end;
except
Application.HandleException(Self);
end;
WM_CHANGECBCHAIN://process change chain message
try
if THandle(msg.wParam) = FNextViewerHandle then
begin
FNextViewerHandle := msg.lParam;
msg.Result := 0;
end
else
msg.Result := SendMessage(FNextViewerHandle, WM_CHANGECBCHAIN,msg.wParam, msg.lParam);
except
Application.HandleException(Self);
end;

else
with Msg do
Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);

end;{Case}
end;

end.
 
看一下Timer的源码你就全明白了
要先分配给他一个句柄..........
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
484
DelphiTeacher的专栏
D
后退
顶部