Delphi获取其它进程窗口句柄的3种方法


本文整理自网络,侵删。

 
本文主要跟大家介绍Delphi中获取其它进程的窗口句柄,在Delphi中获取其它进程的窗口句柄,绝大部分人首先想到的会使用:FindWindow或者用GetWindow来遍历查找,如:

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; 

 

    因为目前网络上绝大部分的代码都是介绍用这两种方法取得其它进程的窗口句柄。虽这两种方法都可以达到查找其它进程的窗口句柄的目的,但本人认为这两都方法存在较大的弊端。因为这两种方法都是根据其它进程的标题来查找的,如果其它进程的标题在运行时不断的发生变化,那么这两种方法就无法没办法用了。

 

    今天给大家介绍第三种通过进程的文件名来查找窗口句柄。首先通过进程快照得到要查找的进程ID(ProcessId),其次,再跟据ProcessId获取进程的窗口句柄。以下为本文章的代码:

 

uses TLHelp32;

procedure TForm1.Button1Click(Sender: TObject); 
var
  ProcessName : string; //进程名
  FSnapshotHandle:THandle; //进程快照句柄
  FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
  ContinueLoop:BOOL;
  MyHwnd:THandle;
begin
  FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
  FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
  ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
  //循环例举    
  while ContinueLoop  do   
  begin
    ProcessName := FProcessEntry32.szExeFile;

    if(ProcessName = '要找的应用程序名.exe') then begin
      MyHwnd := GetHWndByPID(FProcessEntry32.th32ProcessID);
      ...

      ...
    end;
    ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);   //   释放快照句柄
end;

 

//跟据ProcessId获取进程的窗口句柄

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;

1、如果你需要更改数据,query.requestlive必须为true //www.delphitop.com

2、如果有输入参数的时候,容易出错,通常的错法是这样:
比如:“WHERE abc = : abc”
改正如下:“WHERE abc=:abc”就是说=:前后都不能留空格。
3、ADOQuery.Open与ADOQuery.ExecSQL 有不同之处。
ADOQuery.Open一般用在查询,select时候;而ADOQuery.ExecSQL用在insert,delete,update等



相关阅读 >>

Delphi serial number of an usb flash drive 获取u盘硬件序列号

Delphi检测程序内存泄漏

Delphi 如何把一个exe做为res加入到dll中,并在运行时生成exe文件执行

Delphi 利用51.la统计程序使用量

Delphi xe8 中tidtcpclient的writeln编码变化

winapi 字符及字符串函数(5): ischaralpha - 是否是个字母

Delphi遍历指定目录下指定类型文件的函数

Delphi 获取计算机串口列表

Delphi 网上获取北京时间idhttpserver and idhttp 使用 encoding utf8

Delphi执行cmd命令

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



打赏

取消

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

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

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

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

评论

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