我使用这个德尔福7的代码,如果正在运行Internet Explorer检测:为什么FindWindow()不是100%可靠?
function IERunning: Boolean;
begin
Result := FindWindow('IEFrame', NIL) > 0;
end;
这适用于系统的99%,IE 8,9和10
但也有一些系统(遗憾的是我没有,但我有两个beta版测试人员拥有这样的系统,都是Win7 x64 SP1),其中FindWindow()为IEFrame返回0,即使IE在内存中也是如此。
所以我编写的另一种方法找到窗口:
function IERunningEx: Boolean;
var WinHandle : HWND;
Name: array[0..255] of Char;
begin
Result := False; // assume no IE window is present
WinHandle := GetTopWindow(GetDesktopWindow);
while WinHandle <> 0 do // go thru the window list
begin
GetClassName(WinHandle, @Name[0], 255);
if (CompareText(string(Name), 'IEFrame') = 0) then
begin // IEFrame found
Result := True;
Exit;
end;
WinHandle := GetNextWindow(WinHandle, GW_HWNDNEXT);
end;
end;
的替代方法适用于所有系统的100%。
我的问题 - 为什么FindWindow函数()在某些系统上的不可靠?
[FindWindow](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v = vs.85).aspx)和[GetWindowText](http ://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v = vs.85)MSDN上的.aspx)页可能会提供一些线索。 – 2013-03-18 05:03:15
而是通过Windows手动循环的,你应该使用['EnumWindows的()'](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633497.aspx)来代替。 – 2013-03-18 05:14:03
Remy,EnumWindows()需要一个回调函数,所以不能用在内联代码中。在这种情况下需要事件处理。而且我必须在EnumWindows()被称为ANYWAY之后循环。我在其他应用程序中使用EnumWindows(),而且速度也很慢。但是在附注中,它仍不能解释为什么FindWindow()在99%的系统上工作,但不在1%上。 – Casady 2013-03-18 05:27:32