采用消息机制,应用程序A如何向应用程序B发送字符串(50分)

  • 主题发起人 主题发起人 luotuo
  • 开始时间 开始时间
L

luotuo

Unregistered / Unconfirmed
GUEST, unregistred user!
采用消息机制,应用程序A如何向应用程序B发送字符串
 
送端<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; H: THandle;<br>&nbsp; CopyDataStruct: TCopyDataStruct;<br>begin<br>&nbsp; h:=FindWindow(nil, 'App1');<br>&nbsp; if h&amp;lt;&amp;gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; CopyDataStruct.cbData:=Length(Edit1.Text)+1;<br>&nbsp; &nbsp; CopyDataStruct.lpData:=PChar(Edit1.Text);<br>&nbsp; &nbsp; SendMessage(h, WM_CopyData, 0, LParam(@CopyDataSTruct))<br>&nbsp; end<br>end;<br><br>end.<br>接受端:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure WMCopyData(var Msg: TWMCopyData); message WM_CopyData;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Caption:='App1'<br>end;<br><br>procedure TForm1.WMCopyData(var Msg: TWMCopyData);<br>var<br>&nbsp; s: string;<br>begin<br>&nbsp; s:=StrPas(PChar(Msg.CopyDataStruct^.lpData));<br>&nbsp; ShowMessage(s)<br>end;<br><br>end.
 
<br>keybd_event(VK_DOWN, 0,0,0) &nbsp; ;//向当前程序发送<br><br>在这之前你可以让其它程序各到焦点。如。<br><br>SetForegroundWindow(hWnd);<br>
 
书上讲的很清了,有很多这样的例子。
 
定义一个TCopyDataStruct变量ds,给cbData(大小),lpData(指针)参数赋值,<br>sendmessage(接受方句柄, WM_CopyData, 0,cardinal(@ds));<br>接受方定义消息自动接收<br>procedure WMCopyData(var Msg: TWMCopyData);message wm_copydata<br>procedure WMCopyData(var Msg: TWMCopyData);<br>var<br>&nbsp;s: string;<br>begin<br>&nbsp;s:=StrPas(Msg.CopyDataStruct^.lpData);<br>end;<br><br>
 
樓上的都不是很正確:<br>到這裡看一下:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=2501129
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2570322<br>http://ice.nease.net/files/article/other/270.txt &nbsp; &nbsp;
 

Similar threads

D
回复
0
查看
830
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
890
DelphiTeacher的专栏
D
后退
顶部