Delphi 获取其他程序窗口句柄

var

 HCurrentWindow:HWnd;

 Str:String;

begin

 HCurrentWindow:= GetWindow(Handle,GW_HWNDFIRST);

 while HCurrentWindow <>0 do

 begin

 Str:=GetWndText(HCurrentWindow);

  if UpperCase(Str)='frmMain(//窗口的标题)' then

  begin

   Syslog('获取成体句柄成功');

  end;

 HCurrentWindow:=Get(HCurrentWindow, GW_HWNDNEXT);

 end;

end;

--------------------------------------------我是分割线-----------------------------------------------

需引入 TLHelp32;

uses TLHelp32;

var

ProcessName:String;   // 进程名

FSeapshotHandle: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;

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;

引用::http://blog.csdn.net/chinazhd/article/details/6566535






  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值