windows获取窗口句柄

windows获取窗口句柄1、使用FindWindow函数获取窗口句柄示例:使用FindWindow函数获取窗口句柄,然后获得窗口大小和标题,并且移动窗口到指定位置。[html] viewplaincopy #include <Windows.h>  #include <stdio.h>  #include <string.h>  #…

大家好,又见面了,我是你们的朋友全栈君。

1、使用FindWindow函数获取窗口句柄

示例:使用FindWindow函数获取窗口句柄,然后获得窗口大小和标题,并且移动窗口到指定位置。

[html] 
view plain
copy

 

  1. #include <Windows.h>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <iostream.h>  
  5.   
  6. int main(int argc, char* argv[])  
  7. {  
  8.     //根据窗口名获取QQ游戏登录窗口句柄  
  9.     HWND hq=FindWindow(NULL,”QQ2012″);    
  10.   
  11.     //得到QQ窗口大小  
  12.     RECT rect;    
  13.     GetWindowRect(hq,&rect);      
  14.     int w=rect.right-rect.left,h=rect.bottom-rect.top;  
  15.     cout<<“宽:”<<w<<” “<<“高:”<<h<<endl;  
  16.       
  17.     //移动QQ窗口位置  
  18.     MoveWindow(hq,100,100,w,h,false);  
  19.       
  20.     //得到桌面窗口  
  21.     HWND hd=GetDesktopWindow();  
  22.     GetWindowRect(hd,&rect);      
  23.     w=rect.right-rect.left;  
  24.     h=rect.bottom-rect.top;  
  25.     cout<<“宽:”<<w<<” “<<“高:”<<h<<endl;  
  26.       
  27.     return 0;  
  28. }  

2、使用EnumWindows和EnumChildWindows函数以及相对的回调函数EnumWindowsProc和EnumChildWindowsProc获取所有顶层窗口以及它们的子窗口(有些窗口做了特殊处理,比如QQ是不能通过这个方法获得的)

示例:

[html] 
view plain
copy

 

  1. #include “stdafx.h”  
  2. #include <Windows.h>  
  3. #include <stdio.h>  
  4. #include <tchar.h>  
  5. #include <string.h>  
  6. #include <iostream.h>  
  7.   
  8. //EnumChildWindows回调函数,hwnd为指定的父窗口  
  9. BOOL CALLBACK EnumChildWindowsProc(HWND hWnd,LPARAM lParam)  
  10. {  
  11.     char WindowTitle[100]={0};      
  12.     ::GetWindowText(hWnd,WindowTitle,100);  
  13.     printf(“%s\n”,WindowTitle);  
  14.       
  15.     return true;     
  16. }  
  17.   
  18. //EnumWindows回调函数,hwnd为发现的顶层窗口  
  19. BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam)  
  20. {  
  21.     if (GetParent(hWnd)==NULL && IsWindowVisible(hWnd) )  //判断是否顶层窗口并且可见  
  22.     {  
  23.         char WindowTitle[100]={0};      
  24.         ::GetWindowText(hWnd,WindowTitle,100);  
  25.         printf(“%s\n”,WindowTitle);  
  26.   
  27.         EnumChildWindows(hWnd,EnumChildWindowsProc,NULL); //获取父窗口的所有子窗口  
  28.     }  
  29.       
  30.     return true;     
  31. }  
  32.   
  33. int main(int argc, _TCHAR* argv[])  
  34. {  
  35.     //获取屏幕上所有的顶层窗口,每发现一个窗口就调用回调函数一次  
  36.     EnumWindows(EnumWindowsProc ,NULL );  
  37.   
  38.     return 0;  
  39. }  

3、使用GetDesktopWindow和GetNextWindow函数得到所有的子窗口

示例:

[html] 
view plain
copy

 

  1. #include “stdafx.h”  
  2. #include <Windows.h>  
  3. #include <stdio.h>  
  4. #include <tchar.h>  
  5. #include <string.h>  
  6. #include <iostream.h>  
  7.   
  8. int main(int argc, _TCHAR* argv[])  
  9. {     
  10.     //得到桌面窗口  
  11.     HWND hd=GetDesktopWindow();  
  12.   
  13.     //得到屏幕上第一个子窗口  
  14.     hd=GetWindow(hd,GW_CHILD);  
  15.     char s[200]={0};  
  16.   
  17.     //循环得到所有的子窗口  
  18.     while(hd!=NULL)  
  19.     {  
  20.         memset(s,0,200);  
  21.         GetWindowText(hd,s,200);  
  22.         /*if (strstr(s,”QQ2012″))  
  23.         {  
  24.             cout<<s<<endl;  
  25.             SetWindowText(hd,”My Windows”);  
  26.         }*/  
  27.         cout<<s<<endl;  
  28.           
  29.         hd=GetNextWindow(hd,GW_HWNDNEXT);  
  30.     }  
  31.   
  32.     return 0;  
  33. }  

 

 
 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162529.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Git规范:Git提交规范

    Git规范:Git提交规范1 Commitmessag 格式 type scope subject 1 type 必须 作用 用于说明 Gitcommit 的类别 只允许使用下面的标识 feat 新功能 feature fix to 修复 bug 可以是 QA QualityAssur 发现的 BUG 也可以是研发自己发现的 BUG 备注 fix 产生 diff 并自动修复此问题 适合于一次提交直接修复问题 to 只产生 diff 不自动修复此问题 subject scope type

    2025年11月5日
    5
  • Java学习路线从入门到入土

    Java学习路线从入门到入土Java 学习路线从入门到入土简介一门永不过时的编程语言 Java 编程开发 Java 编程语言占比 据官方数据统计 在全球编程语言工程师的数量上 Java 编程语言以 900 万的程序员数量位居首位 而且很多软件的开发都离不开 Java 编程 因此其程序员的数量最多 而在以 Java 编程为核心的开发领域中 javaEE 程序员的需求量 10 年来一直居于首位 Java 工程师人才缺口 根据 IDC 的统计数字 就 2017 年来说 我国 Java 人才的缺口已达 42 5 万 并且以每年 20 左右的速度增长 在未来 5 年内 合格软件人才

    2025年12月15日
    5
  • dede被注入后台提示用户名不存在解决方法

    dede被注入后台提示用户名不存在解决方法

    2021年10月7日
    53
  • springioc和aop原理_描述spring框架的工作原理

    springioc和aop原理_描述spring框架的工作原理Spring的Ioc底层是怎么实现的?一、SpringIoc是什么IOC:控制反转,就是把对象的创建交给Spring来做二、SpringIoc所使用的技术1、xml配置文件2、dom4j解析XML文件3、工厂设计模式4、反射三、SpringIoc的具体实现第一步:配置xml文件 &lt;bean id="dic" class="com.zhy.springIoc.model.Dic"&gt;&lt…

    2026年1月14日
    3
  • jquery时间轴幻灯展示源代码

    查看效果:http://hovertree.com/texiao/jquery/75/源代码下载:http://hovertree.com/h/bjaf/8jlpc2wu.htm效果图如下:代码如下:

    2021年12月26日
    42
  • Istio安装「建议收藏」

    1、安装Istio自定义资源定义kubectlapply-finstall/kubernetes/helm/istio/templates/crds.yamlcustomresourcedefinition.apiextensions.k8s.io/virtualservices.networking.istio.iocreatedcustomresourcedefinition….

    2022年4月17日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号