gpt4 book ai didi

c++ - 如何让 EnumWindows 列出所有窗口?

转载 作者:可可西里 更新时间:2023-11-01 12:41:29 25 4
gpt4 key购买 nike

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

#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;
}

如果我调用该代码,它将列出所有最小化的窗口: enter image description here

现在,我不再只对最小化的窗口感兴趣,现在我想要所有的窗口。所以我删除了 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;
}

现在我得到所有窗口除了最小化的窗口(这次没有列出之前列出的窗口句柄): enter image description here

为了完整性,这是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>

我做错了什么?

最佳答案

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

#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);
delete[] 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;
}

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

另请参阅:

关于c++ - 如何让 EnumWindows 列出所有窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10246444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com