使用 GetWindowsText 的问题(100分)

  • 主题发起人 主题发起人 无知者无畏
  • 开始时间 开始时间

无知者无畏

Unregistered / Unconfirmed
GUEST, unregistred user!
下面一段程序:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; theHandle:HWND;<br>&nbsp; i,j:Integer;<br>&nbsp; PStr:PChar;<br>begin<br>&nbsp; PStr:=' &nbsp; &nbsp; &nbsp; ';<br>&nbsp; theHandle:=FindWindow(nil,'计算器');<br>&nbsp; If theHandle &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; i:=GetWindowTextLength(theHandle);<br>&nbsp; &nbsp; Edit1.Text:=IntToStr(i);<br>&nbsp; &nbsp; j:=GetWindowText(theHandle,PStr,i+1);<br>&nbsp; &nbsp; Edit2.Text:=IntToStr(j);<br>&nbsp; end;<br>end;<br><br>在DELPHI 5下直接运行(直接按F9),结果正常。(运行结果Edit1和Edit2都显示 6)<br>但是离开DELPHI,直接运行编译生成的 .EXE 文件时 会出现<br>&nbsp; &nbsp; Edit1显示 6<br>&nbsp; &nbsp; Edit2显示 0<br>的情况,请问那位能解决这个问题?
 
98 下显示 6,6<br>NT/2000下显示 6,0<br>所以NT/2000下:<br>var<br>&nbsp; theHandle:HWND;<br>&nbsp; i,j:Integer;<br>&nbsp; PStr:WideString;<br>begin<br>&nbsp; PStr:=' &nbsp; &nbsp; &nbsp; ';<br>&nbsp; theHandle:=FindWindow(nil,'计算器');<br>&nbsp; If theHandle &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; i:=GetWindowTextLength(theHandle);<br>&nbsp; &nbsp; Edit1.Text:=IntToStr(i);<br>&nbsp; &nbsp; j:=GetWindowTextW(theHandle,PWStr(PSTR),i+1);<br>&nbsp; &nbsp; Edit2.Text:=IntToStr(j);<br>&nbsp; end;<br>
 
PWStr是什么?编译不过去。<br>PWIN98SE下离开DELPHI 单独运行.EXE文件,结果也是 6,0 。<br>我用的是PWIN98SE。<br><br>另外,如果在最后加上一句<br>&nbsp; Showmessage(PStr);<br>那么在DELPHI 下运行,结果是6,6 并且showmessage显示“计算器”,<br>而离开DELPHI 单独运行EXE文件 结果是6,0 并且showmessage的显示为空。
 
写错了,应该是PWChar或PWideChar
 
z_cd兄:<br>还是不行,照你的写,在DELPHI 下运行都是 6,0了。
 
我那段程序是在NT/2000下用的。<br>98下试试:<br>var<br>&nbsp; theHandle:HWND;<br>&nbsp; i,j:Integer;<br>&nbsp; PStr:AnsiString;<br>begin<br>&nbsp; PStr:=' &nbsp; &nbsp; &nbsp; ';<br>&nbsp; theHandle:=FindWindow(nil,'计算器');<br>&nbsp; If theHandle &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; i:=GetWindowTextLength(theHandle);<br>&nbsp; &nbsp; Edit1.Text:=IntToStr(i);<br>&nbsp; &nbsp; j:=GetWindowTextA(theHandle,PAnsiChar(PSTR),i+1);<br>&nbsp; &nbsp; Edit2.Text:=IntToStr(j);<br>&nbsp; end;<br>end;<br>
 
试一下这样,看可不可以。<br>var<br>&nbsp; theHandle:HWND;<br>&nbsp; i,j:Integer;<br>&nbsp; PStr:PChar;<br>begin<br>&nbsp; theHandle:=FindWindow(nil,'计算器');<br>&nbsp; If theHandle &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; i:=GetWindowTextLength(theHandle);<br>&nbsp; &nbsp; Edit1.Text:=IntToStr(i);<br>&nbsp; &nbsp; GetMem(PStr, i + 1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;///<br>&nbsp; &nbsp; try &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///<br>&nbsp; &nbsp; &nbsp; j:=GetWindowText(theHandle,PStr,i+1); &nbsp; <br>&nbsp; &nbsp; &nbsp; Edit2.Text:=IntToStr(j);<br>&nbsp; &nbsp; finally &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///<br>&nbsp; &nbsp; &nbsp; FreeMem(PStr, i + 1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ///<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
和98或2000/NT无关。<br>因为你没有为PSTR分配内存空间,在DELPHI内调试方式时,DELPHI<br>会为其分配内存,单独运行时因为没有空间存储返回的内容,所以<br>j会为0,PStr也为空。<br><br>把第一句<br>&nbsp; PStr:=' &nbsp; &nbsp; &nbsp; ';<br>删掉,在<br>&nbsp; &nbsp; i:=GetWindowTextLength(theHandle);<br>&nbsp; &nbsp; Edit1.Text:=IntToStr(i);<br>之后加上下面一句:<br>&nbsp; &nbsp; Pstr:=AllocMem(i+1); 为Pstr分配内存空间。<br>记得最后要释放掉分配的空间:<br>&nbsp; &nbsp; FreeMem(PStr);<br>
 
谢谢!问题解决!
 
多人接受答案了。
 
Yes, they are all right
 
后退
顶部