C++ 获取窗口句柄

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

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

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;  


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

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

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


相关推荐

  • UML图之例图

    用例图主要说明的是谁要使用系统,以及他们使用该系统可以做些什么,帮助开发团队以一种可视化的方式理解系统的功能需求。一个用例图包含了多个模型元素,如系统、参与者和用例,并且显示这些元素之间的各种关系,

    2021年12月29日
    40
  • SQLSERVER存储过程语法详解

    SQLSERVER存储过程语法详解 1 2 3 4 5 6 7 8 9 10 11 CREATEPROC[EDURE]procedure_name[;number]     [{@parameterdata_type}         [VARYING][=default…

    2022年7月17日
    22
  • 树莓派4b串口通信配置

    树莓派4b串口通信配置树莓派4b本身是两个串口,运行ls/dev-al如下:请注意:在默认状态下,serial0(就是GPIO14,15)是映射到ttyS0的(就是MINI串口:/dev/ttyS0),ttyS0的特点是其工作时钟来自于CPU,CPU的时钟呢又是从600MHZ到1.5Ghz动态变化的,所以这个串口经常会因为时钟频率发生变化而发生错误,因此我们不用这个串口。默认状态下,serial1(跟板载蓝牙…

    2022年6月1日
    75
  • 【转载】.NET Remoting学习笔记(二)激活方式

    【转载】.NET Remoting学习笔记(二)激活方式

    2021年11月18日
    90
  • Binder 机制「建议收藏」

    Binder是Android系统进程间通信(IPC:InternetProcessConnection)方式之一。Linux已经拥有的IPC手段包括:管道(Pipe)、信号(Signal)、跟踪(Trace)、插口(Socket)、报文队列(Message)、共享内存(ShareMemory)和信号量(Semaphore)等。本文详细分析Binder作为Android主要IPC方式的优势。一、概述基于Client-Server的通信方式,广泛应用于从互联网和数据库访问

    2022年4月7日
    105
  • pytest-allure_pytest数据驱动

    pytest-allure_pytest数据驱动前言allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面。mac环境:

    2022年7月28日
    11

发表回复

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

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