代码如下:
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;
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.071043014526367 seconds