只知道進程ID,獲取主窗口句柄的方法如下:
代碼如下: function GetHwndFromProcess(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; h: THandle; begin Result := True; GetWindowThreadProcessID(Wnd, @PID); if PID = EI.ProcessID then begin h := GetWindowLong(Wnd, GWL_HWNDPARENT); if h = 0 then begin EI.HWND := Wnd; Result := False; end; end; 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; |
|
來自: delphi_筆記 > 《系統(tǒng)》