zoukankan      html  css  js  c++  java
  • win32-EnumChildWindows的使用

    #include <Windows.h>
    #include <iostream>
    #include <string>
    
    static BOOL CALLBACK enumchildWindowCallback(HWND hWnd, LPARAM lparam) {
        int length = GetWindowTextLength(hWnd);
        char * buffer = new char[length + 1];
        GetWindowText(hWnd, buffer, length + 1);
        std::string windowTitle(buffer);
    
        std::cout << hWnd << ":  " << windowTitle << std::endl;
        delete buffer;
        return TRUE;
    }
    
    int main()
    {
        HWND hwnd = (HWND)0x000D0DEE; //父窗口的句柄
        EnumChildWindows(hwnd, enumchildWindowCallback, NULL);
    
        std::cin.ignore();
        return 0;
    }

    拓展: 使用EnumWindows枚举窗口句柄(不包括子窗口)

    #include <Windows.h>
    #include <string>
    #include <iostream>
    
    static BOOL CALLBACK enumchildWindowCallback(HWND hWnd, LPARAM lparam) {
    int length = GetWindowTextLength(hWnd);
    char* buffer = new char[length + 1];
    GetWindowText(hWnd, buffer, length + 1);
    std::string windowTitle(buffer);
    
    int n = (int)hWnd;
    // Ignore windows if invisible or missing a title
    //    if (IsWindowVisible(hWnd) && length != 0) {
    std::cout << n << ": " << windowTitle << std::endl;
    //    }
    delete buffer;
    return TRUE;
    }
    
    static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM m) {
    TCHAR _classbuf[255];
    int length = GetWindowTextLength(hWnd);
    char* buffer = new char[length + 1];
    GetWindowText(hWnd, buffer, length + 1);
    std::string windowTitle(buffer);
    m = GetClassName(hWnd, _classbuf, 1024);
    int n = (int)hWnd;
    // Ignore windows if invisible or missing a title
    //    if (IsWindowVisible(hWnd) && length != 0) {
    std::cout << n << ": " << windowTitle << std::endl;
    //    EnumChildWindows(hWnd, enumchildWindowCallback, m);
    //    }
    return TRUE;
    }
    
    int main()
    {
    std::cout << "Enmumerating windows..." << std::endl;
    EnumWindows(enumWindowCallback, NULL);
    
    std::cin.ignore();
    return 0;
    
    }
    如果去掉EnumChildWindows(hWnd, enumWindowCallback, m)的注释,将会枚举所有的窗口

    更新之后的版本:

    #include <Windows.h>
    #include <iostream>
    #include <tchar.h>
    
    
    static BOOL CALLBACK enumchildWindowCallback(HWND hWnd, LPARAM lparam) {    
        TCHAR buffer[256] = {};
        GetWindowText(hWnd, buffer, 256);
        int n = (int)hWnd;
        if (IsWindowVisible(hWnd)) 
        {
            RECT rc;
            GetWindowRect(hWnd, &rc);
            _tprintf(TEXT("0x%x : %s  %d %d
    "), n, buffer, rc.right - rc.left, rc.bottom - rc.top);
        }
        return TRUE;
    }
    
    static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM m) {
        TCHAR buffer[256] = {};
        GetWindowText(hWnd, buffer, 256);
        int n = (int)hWnd;    
        if (IsWindowVisible(hWnd)) 
        {
            RECT rc;
            GetWindowRect(hWnd, &rc);
            _tprintf(TEXT("0x%x : %s  %d %d
    "), n, buffer, rc.right - rc.left, rc.bottom - rc.top);
            EnumChildWindows(hWnd, enumchildWindowCallback, m);
        }
        return TRUE;
    }
    
    int main()
    {
        _tprintf(TEXT("Enmumerating windows..."));
        EnumWindows(enumWindowCallback, NULL);
    
        return 0;
    
    }
  • 相关阅读:
    004-ant design -dispatch、request、fetch
    003-and design-dva.js 知识导图-02-Reducer,Effect,Subscription,Router,dva配置,工具
    002-and design-dva.js 知识导图-01JavaScript 语言,React Component
    003-and design-在create-react-app项目中使用antd
    002-and design-基于dva的基本项目搭建
    001-ant design安装及快速入门【基于纯antd的基本项目搭建】
    103-advanced-上下文
    102-advanced-代码分割
    101-advanced-React易用性,概述
    007-spring cache-缓存实现-02-springboot ehcahe2、ehcache3实现、springboot caffeine实现
  • 原文地址:https://www.cnblogs.com/strive-sun/p/12653803.html
Copyright © 2011-2022 走看看