D
darzui
Unregistered / Unconfirmed
GUEST, unregistred user!
有下面两个程序<br>程序1:<br>按f4时,edit1里面的数字加1,按a时,edit2里面的数字加1。主窗体标题为form1<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Edit1: TEdit; //text属性初始值为0<br> Edit2: TEdit; ////text属性初始值为0<br> procedure FormKeyDown(Sender: TObject; var Key: Word;<br> Shift: TShiftState);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;<br> Shift: TShiftState);<br>var<br> num1: integer;<br> num2: integer;<br>begin<br> if key=VK_F4 then<br> begin<br> num1:= strtoint(edit1.text)+1;<br> edit1.Text:= inttostr(num1)<br> end<br> else if key=$41 then<br> num2:= strtoint(edit2.Text)+1;<br> edit2.Text:= inttostr(num2)<br>end;<br><br>end.<br><br>程序二:<br>找到标题为form1的窗体,发送按键消息。主窗体标题为form2<br>unit Unit2;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm2 = class(TForm)<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form2: TForm2;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm2.Button1Click(Sender: TObject);<br>var<br> h: HWND;<br>begin<br> h:= findwindow('tform1', 'form1');<br> sendmessage(h, WM_KEYDOWN, VK_F4, 0);<br> sendmessage(h, WM_KEYDOWN, $41, 0);<br>end;<br><br>end.<br>运行两个程序,点击form2上的button,form1上edit1中的数字能随按键增长,<br>而edit2中的数字却变成了4423401且不再改变。我修改了TForm2.Button1Click代码,<br>只发送第二条消息,form1.edit2就能正常的增长了。但如果修改TForm2.Button1Click<br>代码,只发送第一条消息的话,form1.edit2中的数字变成了4423400。<br>真是奇怪,我不知道是怎么回事,请各位指点,谢谢。<br>以上程序运行环境为windows 2000,delphi6。<br>