• 获取窗口句柄


    main

      getparent()为对话框的父窗口句柄
    不管窗口中有什么控件,button也好,ListBox也好,都可以通过GetDlgItem来获 取该控件窗口句柄,比如Button的控件ID为IDC_BUTTON1,则HWND hWnd = GetDlgItem(IDC_BUTTON1)->GetSafeHwnd();当然也可以获取该按钮的CButton对 象,CButton*pButton = CButton*(GetDlgItem(IDC_BUTTON1));想必有了CButton的指针变量,你应该知道怎么调用CButton变量吧

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

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

    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是不能通过这个方法获得的)

    示例:

    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 ",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 ",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函数得到所有的子窗口

    示例:

    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;  
  • 相关阅读:
    MySQL常用语法
    多进程+信号量Semaphore
    python3 解压错误“zlib.error: Error -3 while decompressing data: incorrect header check”
    python3-爬取cnnvd漏洞信息
    putty 的美化
    openwrt下 samba设置
    ubuntu tftp-server 服务器安装与配置
    OpenWRT添加模块 Makefile和Config.in
    WPF MVVM模式的一些理解
    RT-thread 利用Scons 工具编译提示python编码错误解决办法
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3849773.html
Copyright © 2020-2023  润新知