type
TMainWindow = packed record
ProcessID: THandle;
MainWindow: THandle;
end;
PMainWindow =^ TMainWindow;
function IsMainWindow(AHandle: THandle): Boolean;
begin
Result :=(GetWindow(AHandle, GW_OWNER) = 0) and (IsWindowVisible(AHandle));
end;{ IsMainWindow }
function fFindMainWindow(hWnd: THandle; lParam: PMainWindow): BOOL; stdcall;
var
vProcessID: THandle;
begin
GetWindowThreadProcessId(hWnd, vProcessID);
if (lParam^.ProcessID = vProcessID) and IsMainWindow(hWnd) then
begin
lParam^.MainWindow := hWnd;
Result := False;
end else Result := True;
end;
function FindMainWindow(AProcessID: THandle): THandle;
var
vMainWindow: TMainWindow;
begin
vMainWindow.ProcessID := AProcessID;
vMainWindow.MainWindow := 0;
EnumWindows(@fFindMainWindow, Integer(@vMainWindow));
Result := vMainWindow.MainWindow;
end;{ FindMainWindow }
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := Format('Application.Handle: %d, FindMainWindow(): %d',[
Application.Handle, FindMainWindow(GetCurrentProcessId)]);
end;
http://blog.csdn.net/zswang/article/details/1598516