一个关于查阅可视窗口标题的问题(50分)

  • 主题发起人 flyhorse
  • 开始时间
F

flyhorse

Unregistered / Unconfirmed
GUEST, unregistred user!
我想查阅所有可视窗口标题,我用了如下的代码(从网上看到的)<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> hCurrentWindow: HWnd;<br> szText: array[0..254] of char;<br>begin<br> hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);<br> while hCurrentWindow &lt;&gt; 0 do<br> begin<br> if GetWindowText(hCurrentWindow, @szText, 255)&gt;0 then<br> Memo1.Lines.Add(StrPas(@szText));<br> hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);<br> end;<br>end; <br>但结果不是无限循环就是循环根本不执行,请问这么做可以嘛?<br>如果不行,应该怎么做呢?<br>
 
以下是我不知从哪里抄来的一段代码,与你的没什么差别.在我的机器上运行正确.<br>不知你的运行环境如何?<br><br>&nbsp; var<br>&nbsp; &nbsp; hCurrentWindow: HWnd;<br>&nbsp; &nbsp; szText: array[0..254] of char;<br>&nbsp; begin<br>&nbsp; &nbsp; hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);<br>&nbsp; &nbsp; While hCurrentWindow &lt;&gt; 0 Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; If GetWindowText(hCurrentWindow, @szText, 255) &gt; 0 Then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListBox1.Items.add(Strpas(@szText));<br>&nbsp; &nbsp; &nbsp; &nbsp; hCurrentWindow := GetNextWindow(hCurrentWindow, GW_HWNDNEXT);<br>&nbsp; &nbsp; End;<br>
 
1)用EnumWindows()枚举所有顶层窗口。<br>2)对每一个顶层窗口,递归调用用EnumChildWindows()枚举出所有的子窗口。<br>3)对每一个子窗口调用GetWindowText()获得标题。<br>其结果就象你在SPXX中所看到的那样。<br>
 
flyhorse! Hehe, 你的名字和我的是一样的耶!:)<br><br>在某些情况下确实会无限循环,上次我在Visual C++的MFC类库的源代码里面<br>发现了一个Bug,就是因为这个导致程序死掉了。解决的办法是不但判断要是不<br>是0,还要与第一个值比较,如果相同就要退出。<br><br>对Form的handle使用EnumChildWindow枚举子窗口的方法是可靠的。
 
to all: 谢谢你们的回答<br>to 视窗王子: 你的这个方法我在钱达智的enumwidow中看到了,可是列举的很多是   一样的,我想是不是还有其他方法,才用了上面的代码<br>to pegasus: hi 真高兴,有人跟我同名:(握个手吧。。 ^_^)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 出现循环的情况是memo1不断显示,直到没有任何反应(我估计是堆栈       满了),出现不循环是hcurrentwidown=0(一直等于)即使重新运行<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delphi也还是一样。(我想了半天,也没搞明白,怎么回这样)<br>to wuyi: 我的环境是win98+delphi 4.0 update 3
 
应该有个回调函数!
 
循环不止的原因是hCurrentWindow总也不是0, 所以我说您需要记住第一个窗口的handle, 除了比较是不是0之外,还要看看是不是第一个窗口,不然就会死循<br>环下去。
 
多人接受答案了。
 
顶部