如何获取EnumWindows列出所有窗口? [英] How can I get EnumWindows to list all windows?
问题描述
我假设,我要求的实际上应该是默认值,但我遇到一些我不明白的行为。
#includestdafx.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;
}
如果我调用该代码,它将列出所有最小化的窗口:
现在, m不再只对最小化的窗口感兴趣,现在我想要所有的。所以我删除 IsIconic
检查:
BOOL CALLBACK enumWindowsProc $ b __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;
}
现在我获得所有窗口 (此时不列出之前列出的窗口句柄): 为了完整性,这是
stdafx.h
:
#pragma once
#includetargetver.h
#include< iostream>
#include< map>
#include< string>
命名空间std {
#if定义_UNICODE ||定义UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif
}
#include< stdio.h>
#include< tchar.h>
#include< Windows.h>
#include< psapi.h>
我做什么错了?
这是(如我假设) EnumWindows
不是一个问题。
在调试时,我发现 enumWindowsProc
在每个窗口都被调用,但是一些迭代只是不生成输出。
暂时,我切换到使用 _tprintf
但是我不明白原来的代码有什么问题。调用 wcout.flush()
也没有理想的效果。
I'm assuming, what I'm asking should actually be the default, but I'm experiencing some behavior I don't understand.
#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;
}
If I invoke that code, it will list all minimized windows:
Now, I'm no longer interested in only the minimized windows, now I want all of them. So I remove the IsIconic
check:
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;
}
Now I get all windows except the minimized ones (none of the previously listed window handles are listed this time):
For completeness, this is the 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>
What am I doing wrong?
It's (as I assumed) not a problem with EnumWindows
at all. The problem is with the output stream.
While debugging, I noticed that enumWindowsProc
is called just fine for every window, but that some iterations are simply not generating output.
For the time being, I switched to using _tprintf
, but I don't understand what the problem with the original code is. Calling wcout.flush()
had no desirable effect either.
这篇关于如何获取EnumWindows列出所有窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!