发送方:Form2const
WM_MyMessage = WM_User + 2000;procedure TForm2.btnlClick(Sender: TObject);
var
blmsg: array[0..3] of string;
List: TStringList;
begin
blmsg[0]:='test1';
blmsg[1]:='test2';
blmsg[2]:='test2';
blmsg[3]:='test3'; List := TStringList.Create;
List.Add(blmsg[0]);
List.Add(blmsg[1]);
List.Add(blmsg[2]);
List.Add(blmsg[3]);
//ShowMessage(List.Strings[2]); SendMessage(Form1.Handle, WM_MyMessage, 0, LongInt(List));
List.Free; Close;
end;接收方:Form1//自定义消息声明
const
WM_MyMessage = WM_User + 2000; procedure MyMessage(var msg: TMessage); message WM_MyMessage;......................
procedure TForm1.MyMessage(var msg: TMessage);
var
getbllist: TStringList;
begin
getbllist := TStringList.Create;
getbllist.Assign(TStrings(Msg.WParam));
Application.MessageBox(PChar(getbllist.Strings[2]), '提示', MB_OK + MB_ICONINFORMATION);
TStrings(Msg.WParam).Free;
getbllist.Free;
end;
上述代码编译通过,但运行时出错,提示“Cannot assign a nil to a TStringList”
请问是什么问题?该如何修改呢 ?
WM_MyMessage = WM_User + 2000;procedure TForm2.btnlClick(Sender: TObject);
var
blmsg: array[0..3] of string;
List: TStringList;
begin
blmsg[0]:='test1';
blmsg[1]:='test2';
blmsg[2]:='test2';
blmsg[3]:='test3'; List := TStringList.Create;
List.Add(blmsg[0]);
List.Add(blmsg[1]);
List.Add(blmsg[2]);
List.Add(blmsg[3]);
//ShowMessage(List.Strings[2]); SendMessage(Form1.Handle, WM_MyMessage, 0, LongInt(List));
List.Free; Close;
end;接收方:Form1//自定义消息声明
const
WM_MyMessage = WM_User + 2000; procedure MyMessage(var msg: TMessage); message WM_MyMessage;......................
procedure TForm1.MyMessage(var msg: TMessage);
var
getbllist: TStringList;
begin
getbllist := TStringList.Create;
getbllist.Assign(TStrings(Msg.WParam));
Application.MessageBox(PChar(getbllist.Strings[2]), '提示', MB_OK + MB_ICONINFORMATION);
TStrings(Msg.WParam).Free;
getbllist.Free;
end;
上述代码编译通过,但运行时出错,提示“Cannot assign a nil to a TStringList”
请问是什么问题?该如何修改呢 ?
procedure TForm1.MyMessage(var msg: TMessage);
var
getbllist: Pointer;
begin
integer(getbllist):= Msg.WParam;
Application.MessageBox(PChar(TStrings(getbllist).Strings[2]), '提示', MB_OK + MB_ICONINFORMATION);
end;
Application.MessageBox(PChar(TStrings(getbllist).Strings[2]), '提示', MB_OK + MB_ICONINFORMATION);
//发送端 参数:接收端的句柄、发送的字符串
procedure TForm1.SendData(AHandle: THandle; AStr: String);
var
sData: TCopyDataStruct;
begin
sData.cbData := Length(AStr) + 1;
GetMem(sData.lpData,sData.cbData ); //为传递的数据区分配内存
StrCopy(sData.lpData,PChar(AStr)); SendMessage(AHandle,WM_COPYDATA,Handle,Cardinal(@sData)); // 发送WM_COPYDATA消息
FreeMem(sData.lpData); //释放资源
end;//接收端
public
procedure WMCOPYDATA(var AMsg: TWmCopyData);message WM_COPYDATA;procedure TForm1.WMCOPYDATA(var AMsg: TWmCopyData);
begin
ShowMessage(StrPas(AMsg.CopyDataStruct^.lpData));
end;
var
getbllist: TStringList;
i:integer;
begin
getbllist:=TStringList(Msg.WParam);
for i := 0 to getbllist.Count - 1 do
Application.MessageBox(PChar(getbllist.Strings[i]), '提示', MB_OK + MB_ICONINFORMATION);
end;
blmsg: array[0..3] of string;
List: TStringList;
begin
blmsg[0]:='test1';
blmsg[1]:='test2';
blmsg[2]:='test2';
blmsg[3]:='test3';
List := TStringList.Create;
List.Add(blmsg[0]);
List.Add(blmsg[1]);
List.Add(blmsg[2]);
List.Add(blmsg[3]);
SendMessage(Form1.Handle, WM_MYMESSAGE, LongInt(List){注意这个才是WParam参数},0);
List.Free;
end;