Delphi 3 Ways to get other process window handles

Source: Internet
Author: User

This article mainly introduces you to obtain window handles of other processes in Delphi, and to obtain window handles of other processes in Delphi, most people first think of using: FindWindow or GetWindow to traverse and find, such as:

handle := FindWindow(nil,PChar('title of window'));
 
//or:
 
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)='title of window' then begin
 
      ...
 
      ...
 
    End;
 
    HCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);
 
  End;
 
end;

Because most of the code on the Internet currently uses these two methods to obtain window handles from other processes. Although these two methods can achieve the purpose of finding the window handles of other processes, I believe that both methods have greater drawbacks. Because these two methods are based on the title of other processes to find, if the title of other processes constantly changes during runtime, then these two methods can not be useless.

 

The third is to find the window handle by the file name of the process. First, obtain the process ID (ProcessId) to be searched through the process snapshot, and secondly, obtain the window handle of the process according to the ProcessId. The following is the code for this article:

uses TLHelp32;
 
procedure TForm1.Button1Click(Sender: TObject);
var
 ProcessName: string; //Process name
 FSnapshotHandle:THandle; //Process snapshot handle
 FProcessEntry32:TProcessEntry32; //Structural information of the process entry
 ContinueLoop:BOOL;
 MyHwnd:THandle;
begin
 FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //Create a process snapshot
 FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
 ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //Get the first process in the system
//Loop example
 while ContinueLoop do
 begin
 ProcessName := FProcessEntry32.szExeFile;
 
if(ProcessName ='The name of the application you are looking for.exe') then begin
MyHwnd := GetHWndByPID(FProcessEntry32.th32ProcessID);
...
 
...
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle); // Release the snapshot handle
end;
 
  
 
//Acquire the window handle of the process according to 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;

The fourth method obtains the handle of the window where the current mouse is located by the position of the mouse

var
  a:TPoint; //Used to store coordinates
  hw:HWND; //Used to store window handle
begin
  GetCursorPos(a); //Get the mouse coordinates and store them in a
  hw := WindowFromPoint(a); //Get the window handle corresponding to variable a
end


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.