1、直接传递,但只能传递给同进程的窗口,无法实现跨进程的消息传递
传递消息:
[delphi] view plaincopy
- var
- Str: String;
- begin
- Str := ‘some string‘;
- PostMessage(Handle, 0, 0, Integer(Str));
- end;
接收消息:
[delphi] view plaincopy
- var
- Str: String;
- begin
- Str := String(Msg.LParam);
- ShowMessage(Str);
- end;
2、如果线程之间传递可以使用PostThreadMessage传递,PostThreadMessage是该函数将一个消息放入(寄送)到指定线程的消息队列里,不等待线程处理消息就返回。
传递消息:
[delphi] view plaincopy
- Procedure TThread1.Execute;
- var
- sMes:String;
- pMes:^String;
- iTemp:Integer;
- begin
- sMes:=’Test’;
- New(pMes);
- pMes^:=sMes;
- PostThreadMessage(thread2.ThreadID,WM_StartRecording,Integer(pMes),0);
- iTemp:=111;
- PostThreadMessage(thread2.ThreadID,WM_OriginalData,iTemp,0);
- end;
接收消息:
[delphi] view plaincopy
- procedure TThread2.Execute;
- var
- msg:TMsg;
- sMes:String;
- pMes:^String;
- iTemp:Integer;
- begin
- //消息循环
- while GetMessage(msg,0,0,0) do
- begin
- Case msg.message of
- WM_StartRecording:
- begin
- //传过来的是String;
- pMes:=msg.wParam;
- sMes:=pMes^;
- Dispose(pMes);
- WM_OriginalData:
- begin
- //传过来的是整数
- iTemp:=msg.WParam;
- end;
- else begin
- TranslateMessage(msg);
- DispatchMessage(msg);
- end;
- end;
- end;
- end;
使用该方法同时还可以传递stringlist,方法是:
[delphi] view plaincopy
- 发送方:
- a:TStrings;
- a := TStringList.Create;
- SendMessage(Formb.Handle, WM_YOURMESSAGE, 0, LongInt(a));
- 接收方:
- b:TStrings;
- b := TStringList.Create;
- b.Assign(TStrings(Msg.WParam));
- TStrings(Msg.WParam).Free;
如果同一进程传递字符串可以使用PostMessage传递,该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里,不等待线程处理消息就返回,是异步消息模式。消息队列里的消息通过调用GetMessage和PeekMessage取得。
发送消息:
[delphi] view plaincopy
- <pre class="delphi" name="code">var
- pTel: ^string;
- begin
- sTelTmp := ‘10086‘;
- New(pTel);
- pTel^ :=sTelTmp;
- SendMessage(Handle, WM_RCV_AUTO_REPORT, EV_TEL_MT_CALLOUT, Integer(pTel));
- end;</pre><br>
- <pre></pre>
- <br>
- 接收消息:
[delphi] view plaincopy
- <pre class="delphi" name="code">procedure OnReciveDataMsg(var Msg: TMessage); message WM_RCV_AUTO_REPORT;
- var
- pTel: ^string;
- begin
- pTel := Pointer(Msg.LParam);
- sTelPhone := G_GetRealTelPhone(pTel^);
- Dispose(pTel);
- end;
- </pre>
- <pre></pre>
- <p> </p>
- <p><strong>3、使用WM_COPYDATA传递,进程间的数据传递好像只有用wm_copydata消息,它可以跨进程拷贝内存。</strong></p>
- <p>发送消息:</p>
- <pre class="delphi" name="code">procedure SendString(strMSG: string);
- var
- Data: tagCOPYDATASTRUCT;
- pBuf: PChar;
- begin
- GetMem(pBuf, Length(strMSG) + 1);
- try
- ZeroMemory(pBuf, Length(strMSG) + 1);
- StrPCopy(pBuf, strMSG);
- Data.cbData:= Length(strMSG);
- Data.dwData:= Length(strMSG);
- Data.lpData:= pBuf;
- SendMessage(hTargetWin, WM_COPYDATA, Integer(Self.Handle), Integer(@Data));
- finally
- FreeMem(pBuf);
- end;
- end;</pre>
- <p><br>
- 接收消息:</p>
- <pre class="delphi" name="code">procedure WMCopyData(var MSG: TMessage); message WM_COPYDATA;
- procedure WMCopyData(var MSG: TMessage);
- var
- Data : ^tagCOPYDATASTRUCT;
- strMSG: string;
- begin
- Data:= Pointer(Msg.lParam);
- strMSG:= StrPas(Data.lpData);
- ShowMessage(strMSG);
- end;</pre>
- <p><br>
- 可以根据实际情况使用相应的方式来实现消息传递字符串。<br>
- </p>
时间: 2024-10-11 03:52:48