handle := FindWindow(nil,PChar('窗口的标题')); 或者: procedure TForm1.Button1Click(Sender: TObject); var hCurrentWindow: HWnd; WndText:String; begin hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); while hCurrentWindow <> 0 do begin WndText:=GetWndText(hCurrentWindow); if UpperCase(WndText)='窗口的标题' then begin ... ... end; hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT); end; end;
function TForm1.GetHWndByPID(const hPID: THandle): THandle; type PEnumInfo = ^TEnumInfo; TEnumInfo = record ProcessID: DWORD; HWND: THandle; end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall; var PID: DWORD; begin GetWindowThreadProcessID(Wnd, @PID); Result := (PID <> EI.ProcessID) or (not IsWindowVisible(WND)) or (not IsWindowEnabled(WND));
if not Result then EI.HWND := WND; end;
function FindMainWindow(PID: DWORD): DWORD; var EI: TEnumInfo; begin EI.ProcessID := PID; EI.HWND := 0; EnumWindows(@EnumWindowsProc, Integer(@EI)); Result := EI.HWND; end; begin if hPID<>0 then Result:=FindMainWindow(hPID) else Result:=0; end;