前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >windows获取窗口句柄

windows获取窗口句柄

作者头像
全栈程序员站长
发布2022-09-16 08:25:32
1.4K0
发布2022-09-16 08:25:32
举报
文章被收录于专栏:全栈程序员必看

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

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

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

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

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

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. int main(int argc, _TCHAR* argv[])
  8. {
  9. //得到桌面窗口
  10. HWND hd=GetDesktopWindow();
  11. //得到屏幕上第一个子窗口
  12. hd=GetWindow(hd,GW_CHILD);
  13. char s[200]={0};
  14. //循环得到所有的子窗口
  15. while(hd!=NULL)
  16. {
  17. memset(s,0,200);
  18. GetWindowText(hd,s,200);
  19. /*if (strstr(s,”QQ2012″))
  20. {
  21. cout<<s<<endl;
  22. SetWindowText(hd,”My Windows”);
  23. }*/
  24. cout<<s<<endl;
  25. hd=GetNextWindow(hd,GW_HWNDNEXT);
  26. }
  27. return 0;
  28. }

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档