function TForm1.MainApplicationHandle: HWND;
var
MainModule: HMODULE;
CurrentPID: DWORD;
WindowsPID: DWORD;
begin
MainModule := GetModuleHandle(nil);
CurrentPID := GetCurrentProcessID();
Result := 0;
while True do
begin
Result := FindWindowEx(0, Result, ‘TApplication‘, nil);
if (Result = 0) then Exit; // 未找到
GetWindowThreadProcessID(Result, WindowsPID);
if (WindowsPID = CurrentPID) and
(HMODULE(GetWindowLong(Result, GWL_HINSTANCE)) = MainModule)
then Exit; // 找到!!
end;
end;