• [WinAPI] 获取窗口句柄的几种方法


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

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

    我们想获得酷我音乐盒的窗口句柄并移动它,该怎么办呢?

    首先打开VC或者VS里面tool中的SPY++点击查找窗口如下:

    PS:把那个靶心似的的东西移动到想查找的窗口上,这里是酷我音乐盒,下面会出现窗口的信息,包括窗口的句柄、名字、类、类型、大小和位置。点击OK还有更多信息~

    PS:因为FindWindowA(LPCSTR lpClassName ,LPCSTR lpWindowName);既可以通过窗口类名又可以通过窗口名称查找窗口句柄,如果只知道一个就把另一个写成null.这里我们会发现它的窗口名称会随着播放的歌曲的变化而变化,所以通过窗口名来查找窗口句柄是不明智的(不过我们可以通过窗口名字获得当前播放音乐的名称哦!),因此我们用窗口类名获得窗口句柄。

    哈哈,剩下2个函数都很简单,看看应该能够理解,俺就不介绍啦~

     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     //根据窗口类名获取酷我音乐窗口句柄
     9     HWND hq=FindWindow("kwmusicmaindlg",NULL);    
    10 
    11     //得到酷我音乐窗口大小
    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     //移动酷我音乐窗口位置
    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 }

    >_<:这里GetWindowRect函数可以获得窗口矩形,根据窗口矩形可以计算窗口大小;MoveWindow可以将窗口移动到指定位置而且第4、5个参数还能修改窗口大小哦!

    >_<:下面我又蛇添足加了个获得桌面的大小的操作~

    2、获取所有顶层窗口以及它们的子窗口

    示例:使用EnumWindows和EnumChildWindows函数以及相对的回调函数EnumWindowsProc和EnumChildWindowsProc获取所有顶层窗口以及它们的子窗口

    注意:有些窗口做了特殊处理,比如QQ是不能通过这个方法获得的!

    >_<:如上图,有些窗口没有名字,所以显示为空~

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

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

    PS:这样遍历之后就可以找到你想要的窗口句柄啦(不要做坏事呀!哈哈~)

     1 #include <Windows.h>
     2 #include <stdio.h>
     3 #include <tchar.h>
     4 #include <string.h>
     5 #include <iostream.h>
     6 
     7 int main()
     8 {    
     9     HWND hd=GetDesktopWindow();        //得到桌面窗口
    10     hd=GetWindow(hd,GW_CHILD);        //得到屏幕上第一个子窗口
    11     char s[200]={0};
    12     int num=1;
    13     while(hd!=NULL)                    //循环得到所有的子窗口
    14     {
    15         memset(s,0,200);
    16         GetWindowText(hd,s,200);
    17         cout<<num++<<": "<<s<<endl;
    18         hd=GetNextWindow(hd,GW_HWNDNEXT);
    19     }
    20     getchar();
    21     return 0;
    22 }

    参考链接:http://blog.csdn.net/dazhong159/article/details/7903382

  • 相关阅读:
    学习python -- 第018天 os模块
    学习python -- 第017天 文件读写
    重看算法 -- 动态规划 最大子段和问题
    重看算法 -- 递归与分治
    学习python -- 第016天 模块
    学习python -- 第016天 浅拷贝和深拷贝
    网络字节序、主机字节序(摘抄)
    C++/C常量
    结构化程序设计
    循环(高质量4.10)
  • 原文地址:https://www.cnblogs.com/zjutlitao/p/3889900.html
Copyright © 2020-2023  润新知