Delphi 通得进程ID获取主窗口句柄


本文整理自网络,侵删。

 只知道进程ID,获取主窗口句柄的方法如下:

通过EnumWindows枚举所有窗口
使用GetWindowThreadProcessID,通过窗口句柄获取进程ID
比便获取的进程ID与当前已知的进程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中调用必应搜索(bing)的api函数

Delphi 利用for循环自我复制到全盘用timer监控

Delphi 处理以逗号分隔的长字符串

纯真ip数据库解析Delphi d10.1下正常使用

Delphi 内存截取字符串

Delphi webbrowser 使滚动条滚动到底部

Delphi 获取北京时间

Delphi xe6 读取android设备联系人

Delphi tstreamwriter快速写入文件

Delphi firemonkey里触发home按键被按下的事件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...