WinAPI: GetWindowThreadProcessId - 获取指定窗口的进程 ID 或线程 ID

//声明:
GetWindowThreadProcessId(
  hWnd: HWND;                  {指定窗口句柄}
  lpdwProcessId: Pointer = nil {返回进程 ID 的指针}
): DWORD;                      {返回线程 ID}

 
 
 
 
 

 

 
  

//举例:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  c: Cardinal;
begin
  GetWindowThreadProcessId(Handle, @c);
  ShowMessage(IntToStr(c));                   {2792; 随机的}
  {在本例中相同于 GetCurrentProcessID 的结果}
  c := GetCurrentProcessID;
  ShowMessage(IntToStr(c));                   {2792}

  c := GetWindowThreadProcessId(Handle, nil);
  ShowMessage(IntToStr(c));                   {2748}
  {在本例中相同于 GetCurrentThreadID 的结果}
  c := GetCurrentThreadID;
  ShowMessage(IntToStr(c));                   {2748}
end;

end.

 
 
 
 
 

 

 
  

你可能感兴趣的:(WinAPI: GetWindowThreadProcessId - 获取指定窗口的进程 ID 或线程 ID)