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

用法如下:

  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;

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

Delphi 实现获取其他程序的子窗体的更多相关文章

  1. delphi有关获取其他程序的窗口及对窗口内控件的操作

    1.获取当前所有窗口 procedure TForm1.Button1Click(Sender: TObject);var szText: array[0..254] of char; hCurren ...

  2. delphi 7 mdi子窗体。。。无法更改以命令对象为源的记录集对象的 ActiveConnection 属性。

    问题是这样的 我做了一个小程序 把 adoconnection放到了主窗体  连接的是access数据库; 新建了一个子窗体继承自FBase  新建了一个pubulic方法 qrySearch 实现了 ...

  3. Delphi MDI程序 父窗体如何调用当前活动子窗体的函数/过程

    一个MDI文本文件编辑器打开了N个子窗体子窗体的.pas文件有一些public的过程和函数我想在父窗体调用当前活动的子窗体函数我用Self.ActiveChildForm无法调用直接frmEdit.x ...

  4. 主窗体里面打开子窗体&&打印饼图《Delphi 6数据库开发典型实例》--图表的绘制

    \Delphi 6数据库开发典型实例\图表的绘制 1.在主窗体里面打开子窗体:ShowForm(Tfrm_Print); procedure Tfrm_Main.ShowForm(AFormClass ...

  5. Delphi中实现MDI子窗体(转)

        Delphi中实现MDI子窗体 用MDI实现浏览子窗口,具有窗口管理功能,同屏观看多个网页的内容  ① 多文档窗体(MDI) MDI窗体是一种具有主子结构的窗体体系,微软的Word便是其中的一 ...

  6. Delphi MDI 子窗体的创建和销毁 [zhuan]

    1.如果要创建一个mdi child,先看是否有这个child 存在,如果有,则用它,如果没有再创建 //该函数判断MDI 子窗体是否存在,再进行创建和显示function isInclude(for ...

  7. C#如何在子窗体获取父窗体的实例

    在子窗体可以使用this.Owner来获取父窗体的实例

  8. Delphi编写的Android程序获取Root权限实现(2015.4.15更新,支持Android 4.4)

    借助谷歌,并经过本大侠施展坑.蒙.拐.骗.偷五大绝技,终于成功实现在Delphi下获取Root权限并将其扩展为一个完整功能更加完整的TQAndroidShell记录,在华为荣耀2(Android 4. ...

  9. C#WinForm窗体内Panel容器中嵌入子窗体、程序主窗体设计例子

    C#WinForm父级窗体内Panel容器中嵌入子窗体.程序主窗体设计例子 在项目开发中经常遇到父级窗体嵌入子窗体所以写了一个例子程序,顺便大概划分了下界面模块和配色,不足之处还望指点 主窗体窗体采用 ...

随机推荐

  1. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  2. C++ 文件和流

    到目前为止,我们已经使用了 iostream 标准库,它提供了 cin 和 cout 方法分别用于从标准输入读取流和向标准输出写入流. 本教程介绍如何从文件读取流和向文件写入流.这就需要用到 C++ ...

  3. Linux系列教程(七)——Linux常用命令之帮助和用户管理命令

    上篇博客我们介绍了Linux文件搜索命令,其中find是用的最多的也是功能最强大的文件或目录搜索命令,和另一个搜索命令locate的区别是,find命令是全盘搜索,刚创建的文件也能搜索的到,而loca ...

  4. Shiro的认证授权

    shiro安全框架入门整理 package com.shiro.test; import org.apache.shiro.SecurityUtils; import org.apache.shiro ...

  5. IIS导入.pfx证书时报错:"A specified logon session does not exist. It may already have been terminated."

    在IIS中可以直接导入.pfx文件来给站点绑定https协议: 如果在导入.pfx文件时,不选择"Allow this certificate to be exported"的话 ...

  6. WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法

    原文:WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法 WPF 实现主从的datagrid以及操作rowdetailtemplate 的方法        最近在做 ...

  7. Ionic2 调用Custom Cordova Plugin方法

    APP升级到Ionic2之后,如何调用自己写的pulgin,一直测试不成功,现记录这一经过. plugin目前可以分为3类,A类是ionic-native自带的,可以直接导入Typescript类,直 ...

  8. [Spark][Hive]外部文件导入到Hive的例子

    外部文件导入到Hive的例子: [training@localhost ~]$ cd ~[training@localhost ~]$ pwd/home/training[training@local ...

  9. Scala学习(七)---包和引入

    包和引入 摘要: 在本篇中,你将会了解到Scala中的包和引入语句是如何工作的.相比Java不论是包还是引入都更加符合常规,也更灵活一些.本篇的要点包括: 1. 包也可以像内部类那样嵌套 2. 包路径 ...

  10. (8)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Ocelot网关(Api GateWay)

    说到现在现有微服务的几点不足: 1) 对于在微服务体系中.和 Consul 通讯的微服务来讲,使用服务名即可访问.但是对于手 机.web 端等外部访问者仍然需要和 N 多服务器交互,需要记忆他们的服务 ...