发送,接受按键消息时的怪现象。(100分)

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>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit; //text属性初始值为0<br>&nbsp; &nbsp; Edit2: TEdit; ////text属性初始值为0<br>&nbsp; &nbsp; procedure FormKeyDown(Sender: TObject; var Key: Word;<br>&nbsp; &nbsp; &nbsp; Shift: TShiftState);<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.FormKeyDown(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>var<br>&nbsp; &nbsp; num1: integer;<br>&nbsp; &nbsp; num2: integer;<br>begin<br>&nbsp; &nbsp; if key=VK_F4 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; num1:= strtoint(edit1.text)+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; edit1.Text:= inttostr(num1)<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else if key=$41 then<br>&nbsp; &nbsp; &nbsp; &nbsp; num2:= strtoint(edit2.Text)+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; edit2.Text:= inttostr(num2)<br>end;<br><br>end.<br><br>程序二:<br>找到标题为form1的窗体,发送按键消息。主窗体标题为form2<br>unit Unit2;<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; TForm2 = class(TForm)<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; Form2: TForm2;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm2.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp; h: HWND;<br>begin<br>&nbsp; &nbsp; h:= findwindow('tform1', 'form1');<br>&nbsp; &nbsp; sendmessage(h, WM_KEYDOWN, VK_F4, 0);<br>&nbsp; &nbsp; 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>
 
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>var<br>&nbsp; &nbsp; num1: integer;<br>&nbsp; &nbsp; num2: integer;<br>begin<br>&nbsp; &nbsp; if key=VK_F4 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; num1:= strtoint(edit1.text)+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; edit1.Text:= inttostr(num1)<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else if key=$41 then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin//<br>&nbsp; &nbsp; &nbsp; &nbsp; num2:= strtoint(edit2.Text)+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; edit2.Text:= inttostr(num2);<br>&nbsp; &nbsp; &nbsp; &nbsp;end;//原来的写法不对 num2在没按a的情况下是随机值<br>end;<br><br>end.<br>
 
你说的第一点,我后来看出来了,哎,自己太太太粗心了。<br>至于“num2在没按a的情况下是随机值”,好像不是这样,因为每次num2的值都是4423401。这<br>是怎么回事?<br>
 
这是书上说的,<br>我也不知道是对是错,<br>也可以这们理解<br><br>就是程序一但载入内存,<br>则num2都指向同一固定地址,<br>说它是不可预测的值可以更准确一点
 
我重新启动计算机,num2的值还是4423401,为什么会这样?
 
接受答案了.
 

Similar threads

I
回复
0
查看
677
import
I
S
回复
0
查看
709
SUNSTONE的Delphi笔记
S
I
回复
0
查看
544
import
I
顶部