分享

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

 Gavin-book 2014-09-06
  1. 本文主要跟大家介绍Delphi中获取其它进程的窗口句柄,在Delphi中获取其它进程的窗口句柄,绝大部分人首先想到的会使用:FindWindow或者用GetWindow来遍历查找,如:  
  2.   
  3. handle := FindWindow(nil,PChar('窗口的标题'));  
  4.   
  5. 或者:  
  6.   
  7. procedure TForm1.Button1Click(Sender: TObject);   
  8.   
  9. var   
  10.   
  11.   hCurrentWindow: HWnd;   
  12.   
  13.   WndText:String;   
  14.   
  15. begin   
  16.   
  17.   hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);   
  18.   
  19.   while hCurrentWindow <> 0 do   
  20.   
  21.   begin   
  22.   
  23.     WndText:=GetWndText(hCurrentWindow);   
  24.   
  25.     if UpperCase(WndText)='窗口的标题' then begin   
  26.   
  27.       ...   
  28.   
  29.       ...   
  30.   
  31.     end;   
  32.   
  33.     hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);   
  34.   
  35.   end;   
  36.   
  37. end;   
  38.   
  39.    
  40.   
  41.     因为目前网络上绝大部分的代码都是介绍用这两种方法取得其它进程的窗口句柄。虽这两种方法都可以达到查找其它进程的窗口句柄的目的,但本人认为这两都方法存在较大的弊端。因为这两种方法都是根据其它进程的标题来查找的,如果其它进程的标题在运行时不断的发生变化,那么这两种方法就无法没办法用了。  
  42.   
  43.    
  44.   
  45.     今天给大家介绍第三种通过进程的文件名来查找窗口句柄。首先通过进程快照得到要查找的进程ID(ProcessId),其次,再跟据ProcessId获取进程的窗口句柄。以下为本文章的代码:  
  46.   
  47.    
  48.   
  49. uses TLHelp32;  
  50.   
  51. procedure TForm1.Button1Click(Sender: TObject);   
  52. var  
  53.   ProcessName : string; //进程名  
  54.   FSnapshotHandle:THandle; //进程快照句柄  
  55.   FProcessEntry32:TProcessEntry32; //进程入口的结构体信息  
  56.   ContinueLoop:BOOL;  
  57.   MyHwnd:THandle;  
  58. begin  
  59.   FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照  
  60.   FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);  
  61.   ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程  
  62.   //循环例举      
  63.   while ContinueLoop  do     
  64.   begin  
  65.     ProcessName := FProcessEntry32.szExeFile;  
  66.   
  67.     if(ProcessName = '要找的应用程序名.exe') then begin  
  68.       MyHwnd := GetHWndByPID(FProcessEntry32.th32ProcessID);  
  69.       ...  
  70.   
  71.       ...  
  72.     end;  
  73.     ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);  
  74.   end;  
  75.   CloseHandle(FSnapshotHandle);   //   释放快照句柄  
  76. end;  
  77.   
  78.    
  79.   
  80. //跟据ProcessId获取进程的窗口句柄  
  81.   
  82. function TForm1.GetHWndByPID(const hPID: THandle): THandle;  
  83. type  
  84.     PEnumInfo = ^TEnumInfo;  
  85.     TEnumInfo = record  
  86.     ProcessID: DWORD;  
  87.     HWND: THandle;  
  88.     end;  
  89.   
  90.     function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;  
  91.     var  
  92.     PID: DWORD;  
  93.     begin  
  94.     GetWindowThreadProcessID(Wnd, @PID);  
  95.     Result := (PID <> EI.ProcessID) or  
  96.         (not IsWindowVisible(WND)) or  
  97.         (not IsWindowEnabled(WND));  
  98.   
  99.     if not Result then EI.HWND := WND;   
  100.     end;  
  101.   
  102.     function FindMainWindow(PID: DWORD): DWORD;  
  103.     var  
  104.     EI: TEnumInfo;  
  105.     begin  
  106.     EI.ProcessID := PID;  
  107.     EI.HWND := 0;  
  108.     EnumWindows(@EnumWindowsProc, Integer(@EI));  
  109.     Result := EI.HWND;  
  110.     end;  
  111. begin  
  112.     if hPID<>0 then  
  113.     Result:=FindMainWindow(hPID)  
  114.     else  
  115.     Result:=0;  
  116. end;  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多