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