在Delphi 开发中,常常应用到窗体消息传递,以达成某种操作要求,以下列举一个应用的例子,供大家参考。
自定义过程/函数方法:
//发送字符串到指字句柄的窗口中 (接收窗体需用发送时的消息常量WM_COPYDATA)
procedure SendMessageData(AHandle: THandle; AStr: String);
var
sData: TCopyDataStruct;
begin
sData.cbData := Length(AStr) + 1;
//为传递的数据分配内存
GetMem(sData.lpData,sData.cbData );
try
StrCopy(sData.lpData,PChar(AStr));
//发送WM_COPYDATA消息
SendMessage(AHandle,WM_COPYDATA,0,Cardinal(@sData));
finally
FreeMem(sData.lpData); //释放资源
end;
end;
//发送消息到指字句柄的窗口中
procedure SendMyMessage(AHandle: THandle; aMsg:Word);
begin
SendMessage(AHandle,aMsg,0,0)
end;
//------------------
调用:
SendMessageData(TForm(FindGlobalComponent('FrmHint')).Handle,'ABC');
SendMyMessage(TForm(FindGlobalComponent('FrmHint')).Handle, WM_USER+1001);
'FrmHint' 是你想要接收消息的窗体的Name
//=================================
在接收的窗体中定义如下常量及过程
const
WM_MYDEFMSG = WM_USER+1001;
声明部分:
procedure WMCOPYDATA(var AMsg: TWmCopyData); message WM_COPYDATA;
procedure ReceiedFormMsg(var msg :TMsg); message WM_MYDEFMSG;
过程实体:
//接收发送的字符串,即预定消息WM_COPYDATA
procedure WMCOPYDATA(var AMsg: TWmCopyData);
var s : string;
begin
s := StrPas(AMsg.CopyDataStruct^.lpData);
//To do something
end;
//接收预定的消息
procedure ReceiedFormMsg(var msg: TMsg);
begin
//Make you want to do
end;
>>http://blog.csdn.net/webroad/archive/2009/03/04/3955998.aspx