获得其他程序的子窗体

通过一个父窗体的句柄,递归的枚举它的子窗体,我们可以最终找到需要的子窗体。

用法如下:

nParentHandle: HWnd; 
nChildHandle: HWnd; 

nParentHandle := FindWindow(nil, 'Notepad'); 
if nParentHandle <> 0 then 
nChildHandle := FindChildWindow(nParentHandle, 'SomeChildEditsClassName'); 

------函数代码------ 

var 
hwndFindChildWindow : HWND; 

function EnumWindowsForFindChildWindowProc(WHandle: HWND; lParam: LPARAM): BOOL; export; stdcall; 
const 
MAX_WINDOW_NAME_LEN = 80; 
var 
sTargetClassName: string
nHandle: HWnd; 
sCurrClassName: string
bResult: Boolean; 
begin 
if (hwndFindChildWindow <> 0) then 
exit; 
sTargetClassName := PChar(lParam); 
sCurrClassName := GetWindowClass(WHandle); 
bResult := CompareText(sCurrClassName, sTargetClassName) = 0; 
If (bResult) then 
hwndFindChildWindow := WHandle 
else 
FindChildWindow(WHandle, PChar(lParam)); 
end

function FindChildWindow(hwndParent: HWnd; ClassName: PChar) : HWnd; 
begin 
try 
EnumChildWindows(hwndParent, @EnumWindowsForFindChildWindowProc, LongInt(PChar(ClassName))); 
Result := hwndFindChildWindow; 
except 
on Exception do 
Result := 0; 
end
end

//返回当前获得焦点的窗体
function GetFocusedWindowFromParent(ParentWnd:HWnd):HWnd; 
var 
OtherThread, 
Buffer : DWord; 
idCurrThread: DWord; 
begin 
OtherThread := GetWindowThreadProcessID(ParentWnd, @Buffer); 
idCurrThread := GetCurrentThreadID; 
if AttachThreadInput(idCurrThread, OtherThread, truethen begin 
Result := GetFocus; 
AttachThreadInput(idCurrThread, OtherThread, false); 
end 
else 
Result:= GetFocus; 
end

//获得当前获得焦点的子窗体,即使它是其他应用程序的窗体
function GetFocusedChildWindow: HWnd; 
begin 
Result := GetFocusedWindowFromParent(GetForegroundWindow); 
end

//获得窗体的文本
function EIGetWinText(nHandle: Integer): string
var 
pcText: array[0..32768] of char; 
begin 
SendMessage(nHandle, WM_GETTEXT, 32768, LongInt(@pcText)); 
Result := pcText; 
end

//设定窗体的文本
procedure EISetWinText(nHandle: Integer; const sNewText: string); 
begin 
SendMessage(nHandle, WM_SETTEXT, Length(sNewText), LongInt(PChar(Trim(sNewText)))); 
end

//返回窗体的类名
function EIGetWindowClass(const nHandle: HWnd): string
var 
szClassName: array[0..255] of char; 
begin 
GetClassName(nHandle, szClassName, 255); 
Result := szClassName; 
end;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值