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. }
 
 

windows获取窗口句柄的更多相关文章

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

    1.使用FindWindow函数获取窗口句柄 示例:使用FindWindow函数获取窗口句柄,然后获得窗口大小,并且移动窗口到指定位置. 我们想获得酷我音乐盒的窗口句柄并移动它,该怎么办呢? 首先打开 ...

  2. c++ windows 获取mac地址

    c++ windows 获取mac地址 GetAdaptersInfo 用windows api获取mac地址和硬盘id编号 aa

  3. windows获取本机MAC地址并写入文件的bat

    windows获取本机MAC地址并写入文件的bat MAC(Media Access Control)地址,或称为 MAC地址.硬件地址,用来定义网络设备的位置. bat代码例如以下: @echo o ...

  4. Windows 获取控制台窗口句柄

    详细信息 因为多个窗口可能具有相同的标题,您应该更改当前的控制台窗口标题为唯一的标题.这将有助于防止返回不正确的窗口句柄.使用 SetConsoleTitle() 来更改当前的控制台窗口标题.下面是此 ...

  5. Windows获取其他进程中Edit控件的内容

    最近做的MFC项目中,有个获取其他进程中Edit控件内容的需求,本来以为是个很简单的问题,但是来来回回折腾了不少时间,发博记录一下. 刚开始拿到这个问题,很自然的就想到GetDlgItemText() ...

  6. Windows获取文件大小

    Windows最初的设计允许我们处理非常大的文件,所以最初的设计者选用64位值来表示文件大小.但是我们在日常处理过程中文件大小一般不会超过4GB.故Windows提供了两个联合类型的数据结构表示文件大 ...

  7. windows获取时间的方法

    介绍       我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记录 ...

  8. 1.RABBITMQ 入门 - WINDOWS - 获取,安装,配置

    一. 背景:     公司项目有所改动,要求微信(移动端调用的接口),日志接口换位log4net,全部改成以rabbitMQ作为服务支持, 二.本地环境:     windows 10 enterpr ...

  9. Windows获取进程完整路径

    #include <stdio.h> #include <locale.h> #include <windows.h> #include <tlhelp32. ...

随机推荐

  1. 在linux下配置Nginx+Java+PHP的环境

    Apache对Java的支持很灵活,它们的结合度也很高,例如Apache+Tomcat和Apache+resin等都可以实现对Java应用 的支持.Apache一般采用一个内置模块来和Java应用服务 ...

  2. AWK 介绍

    一.模式和动作 awk脚本是由模式和操作组成的:pattern {action} pattern与{action}两者是可选的.如果没有模式,则action应用到全部记录,如果没有action,则输出 ...

  3. Python-Day1 Python基础学习

    一.Python3.5.X安装 1.Windows Windows上找度娘搜索“Python for windows下载”就OK了,安装的时候可以勾选设置环境变量,也可以安装完手动设置,这样在cmd中 ...

  4. WPF 概述

    WPF 全称是:Windows Presentation Foundation,直译为Windows表示基础.WPF是专门为GUI(Graphic User Interface)程序开发设计的. 在过 ...

  5. hdu 4679 Terrorist’s destroy 树形DP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给定一颗树,每条边有一个权值w,问切掉哪条边之后,分成的两颗树的较大的直径*切掉边的权值最小? ...

  6. PIL 安装

    1.安装依赖包 1.1 ubuntu安装 apt-get install python-devapt-get install libjpeg-dev apt-get install libjpeg8- ...

  7. showModalDialog 超过问题

    a.aspx页面打开一个弹出模式对话框b.aspx. a.aspx 页面页面代码: function SetPlay() { window.showModalDialog('SetAdvertisin ...

  8. easy ui window 相关属性

    <div class="easyui-window" title="提示" style="width:550px;height:500px;pa ...

  9. 微软职位内部推荐-Senior Network Engineer

    微软近期Open的职位: Global Foundation Services is the team behind the cloud. GFS is responsible for deliver ...

  10. 1053: [HAOI2007]反素数ant - BZOJ

    Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数 ...