----====我是弱智,简单问题,快来拿分====----(100分)

I

illcat

Unregistered / Unconfirmed
GUEST, unregistred user!
WM_COPYDATA,居然搞了两个小时都没有搞定,大家看看是怎么回事<br><br>发送端:<br>procedure TAForm.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; hd: HWND;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; hd := FindWindow(nil, 'Form1');<br>&nbsp; &nbsp; &nbsp; &nbsp; if hd &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(hd, WM_COPYDATA, handle, integer(pChar('asd')));<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br><br>接收端:<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; private<br>&nbsp; &nbsp; procedure ReceiveMsg(var message :TWMCOPYDATA); message WM_COPYDATA;<br>&nbsp; public<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>procedure TForm1.ReceiveMsg(var message :TWMCOPYDATA);<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('Get WM_COPYDATA Messages');<br>end;<br><br>end.<br><br>接收端总是收不到消息,真是急死我了,小小问题,请帮忙啊!!!!!
 

教父

Unregistered / Unconfirmed
GUEST, unregistred user!
hd := FindWindow(nil, 'Form1');<br>hd有值吗?
 
B

BDFan

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码我试验通过:<br><br>新建一工程,有一FORM1和EDIT1和BUTTON1,代码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button1: TButton;<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 ds:tcopydatastruct;<br>&nbsp; &nbsp; hd:thandle;<br>begin<br>&nbsp; ds.cbData :=length(edit1.text)+1;<br>&nbsp; getmem(ds.lpdata,ds.cbdata);<br>&nbsp; strcopy(ds.lpdata,pchar(edit1.text));<br>&nbsp; hd:=findwindow(nil,'form2');<br>&nbsp; if hd&lt;&gt;0 then<br>&nbsp; &nbsp; sendmessage(hd,wm_copydata,handle,cardinal(@ds))<br>&nbsp; else<br>&nbsp; &nbsp; showmessage('Not find the other form!');<br>&nbsp; freemem(ds.lpdata);<br>end;<br><br>end.<br><br>编译保存。<br><br>再建一工程,有一FORM2和EDIT1,代码如下:<br>unit Unit2;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm2 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; procedure mymessage(var t:twmcopydata);message wm_copydata;<br>&nbsp; end;<br><br>var<br>&nbsp; Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br>procedure Tform2.mymessage(var t:twmcopydata);<br>begin<br>&nbsp; edit1.text:='';<br>&nbsp; edit1.text:=strpas(t.copydatastruct^.lpdata);<br>end;<br>end.<br>编译保存。<br><br>执行上述两个可执行文件,按下程序一的BUTTON1,<br>程序一将把其EDIT1中的数据传送至程序二中的EDIT1中显示<br>
 
S

shangshang

Unregistered / Unconfirmed
GUEST, unregistred user!
一定要把你设计期的Form1也关掉, 关了她的unit文件也可。否则,消息可能发到他那里了饿。
 
P

pcc_mmz1

Unregistered / Unconfirmed
GUEST, unregistred user!
你用的是什么OS, 我在Win98上试了一下,你的程序没有错。
 
I

illcat

Unregistered / Unconfirmed
GUEST, unregistred user!
To:BDFan<br>&nbsp; &nbsp; 我先试试你的代码<br>To:教父<br>&nbsp; &nbsp;没问题,我可以得到Form1的句并,因为我的发送段的<br>Form的Caption为别的值,而非Form1
 
I

illcat

Unregistered / Unconfirmed
GUEST, unregistred user!
faint,还是不成,我开始怀疑我的操作系统了
 
C

crystal

Unregistered / Unconfirmed
GUEST, unregistred user!
改成sendmessage(HWND_BROADCAST,wm_copydata,handle,cardinal(@ds))试试看?
 
I

illcat

Unregistered / Unconfirmed
GUEST, unregistred user!
多人接受答案了。
 

Similar threads

I
回复
0
查看
469
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部