如何让EnumWindows列出所有窗口?

Der*_*ler 10 c++ windows winapi

我假设,我问的实际上应该是默认值,但我遇到了一些我不理解的行为.

#include "stdafx.h"

using namespace std;

BOOL CALLBACK enumWindowsProc(
  __in  HWND hWnd,
  __in  LPARAM lParam
) {
  if( !::IsIconic( hWnd ) ) {
    return TRUE;
  }

  int length = ::GetWindowTextLength( hWnd );
  if( 0 == length ) return TRUE;

  TCHAR* buffer;
  buffer = new TCHAR[ length + 1 ];
  memset( buffer, 0, ( length + 1 ) * sizeof( TCHAR ) );

  GetWindowText( hWnd, buffer, length + 1 );
  tstring windowTitle = tstring( buffer );
  delete[] buffer;

  wcout << hWnd << TEXT( ": " ) << windowTitle << std::endl;

  return TRUE;
}

int _tmain( int argc, _TCHAR* argv[] ) {
  wcout << TEXT( "Enumerating Windows..." ) << endl;
  BOOL enumeratingWindowsSucceeded = ::EnumWindows( enumWindowsProc, NULL );
  cin.get();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我调用该代码,它将列出所有最小化的窗口: 在此输入图像描述

现在,我不再只对最小化的窗口感兴趣,现在我想要所有这些窗口.所以我删除了IsIconic支票:

BOOL CALLBACK enumWindowsProc(
  __in  HWND hWnd,
  __in  LPARAM lParam
) {
  /*
  if( !::IsIconic( hWnd ) ) {
    return TRUE;
  }
  */

  int length = ::GetWindowTextLength( hWnd );
  if( 0 == length ) return TRUE;

  TCHAR* buffer;
  buffer = new TCHAR[ length + 1 ];
  memset( buffer, 0, ( length + 1 ) * sizeof( TCHAR ) );

  GetWindowText( hWnd, buffer, length + 1 );
  tstring windowTitle = tstring( buffer );
  delete[] buffer;

  wcout << hWnd << TEXT( ": " ) << windowTitle << std::endl;

  return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

现在我得到除了最小化窗口之外的所有窗口(这次没有列出之前列出的窗口句柄): 在此输入图像描述

为了完整起见,这是stdafx.h:

#pragma once

#include "targetver.h"


#include <iostream>
#include <map>
#include <string>

namespace std {
  #if defined _UNICODE || defined UNICODE
    typedef wstring tstring;
  #else
    typedef string tstring;
  #endif
}

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <psapi.h>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Fro*_*rog 8

好吧,wcout.flush()永远不会工作,但wcout.clear()修复你的代码,至少对我来说.

wcout << hWnd << TEXT( ": " ) << windowTitle << std::endl;
wcout.clear();
return TRUE;
Run Code Online (Sandbox Code Playgroud)

我知道这个问题已经有一年了,但是回答永远都不会太晚.


Ste*_*ica 6

这是一个回调函数,列出所有打开的窗口:

#include <string>
#include <iostream>
#include <Windows.h>

static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam) {
    int length = GetWindowTextLength(hWnd);
    char* buffer = new char[length + 1];
    GetWindowText(hWnd, buffer, length + 1);
    std::string windowTitle(buffer);

    // List visible windows with a non-empty title
    if (IsWindowVisible(hWnd) && length != 0) {
        std::cout << hWnd << ":  " << windowTitle << std::endl;
    }
    return TRUE;
}

int main() {
    std::cout << "Enmumerating windows..." << std::endl;
    EnumWindows(enumWindowCallback, NULL);
    std::cin.ignore();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果要检查窗口是否最小化,可以使用IsIconic().

也可以看看:


Der*_*ler 3

EnumWindows(正如我所假设的)这根本不是问题。问题出在输出流上。

在调试时,我注意到enumWindowsProc每个窗口都可以很好地调用它,但某些迭代根本不生成输出。

暂时改用 using _tprintf,但不明白原来的代码有什么问题。打电话wcout.flush()也没有取得理想的效果。