• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi 实现获取其他程序的子窗体

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

用法如下:

  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, true) then 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;

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi中的各种文件类型介绍【转】发布时间:2022-07-18
下一篇:
Delphi cxGrid加行号发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap