c++ - How to get EnumWindows to list all windows?

C++ - How to get EnumWindows to list all windows?

To use EnumWindows in C++ to list all top-level windows on the desktop, you'll need to follow a few steps. EnumWindows is a Windows API function that enumerates all top-level windows on the screen by calling a specified callback function for each window.

Here's a step-by-step guide on how to use EnumWindows to list all windows:

1. Include Necessary Headers

You'll need to include the Windows header files to use EnumWindows and related functions:

#include <windows.h> #include <iostream> 

2. Define a Callback Function

Define a callback function that will be called by EnumWindows for each window. This function must match the WNDENUMPROC signature:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { // Retrieve window title char windowTitle[256]; GetWindowText(hwnd, windowTitle, sizeof(windowTitle)); // Print window handle and title std::cout << "Window Handle: " << hwnd << " Title: " << windowTitle << std::endl; // Continue enumeration return TRUE; } 

3. Call EnumWindows

In your main function or wherever appropriate, call EnumWindows, passing the address of your callback function:

int main() { // Enumerate all top-level windows EnumWindows(EnumWindowsProc, 0); return 0; } 

4. Compile and Run

Compile and run your program. The EnumWindowsProc function will be called for each top-level window, and you'll see the handles and titles of all top-level windows printed to the console.

Example Complete Code

Here's a complete example that lists all top-level windows and their titles:

#include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { char windowTitle[256]; // Retrieve window title GetWindowText(hwnd, windowTitle, sizeof(windowTitle)); // Print window handle and title std::cout << "Window Handle: " << hwnd << " Title: " << windowTitle << std::endl; // Continue enumeration return TRUE; } int main() { // Enumerate all top-level windows EnumWindows(EnumWindowsProc, 0); return 0; } 

Key Points

  • EnumWindows: Enumerates all top-level windows.
  • EnumWindowsProc: Callback function that receives the handle to each window.
  • GetWindowText: Retrieves the title of the window.
  • Return Value of Callback: Returning TRUE from the callback function continues the enumeration, while returning FALSE stops it.

This code provides a basic way to list all top-level windows. Depending on your needs, you might want to filter the windows, handle specific window types, or retrieve additional information.

Examples

  1. "C++ EnumWindows list all window titles"

    Description: Enumerate all windows and print their titles using EnumWindows.

    #include <windows.h> #include <iostream> #include <vector> std::vector<std::wstring> windowTitles; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { wchar_t title[256]; if (GetWindowText(hwnd, title, sizeof(title) / sizeof(title[0])) > 0) { windowTitles.push_back(title); } return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); for (const auto& title : windowTitles) { std::wcout << title << std::endl; } return 0; } 

    Explanation: Uses EnumWindows to enumerate all top-level windows and stores their titles in a vector. Prints each title to the console.

  2. "C++ EnumWindows get window handle and title"

    Description: Enumerate windows and retrieve both their handles and titles.

    #include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { wchar_t title[256]; if (GetWindowText(hwnd, title, sizeof(title) / sizeof(title[0])) > 0) { std::wcout << L"HWND: " << hwnd << L", Title: " << title << std::endl; } return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Prints both the handle (HWND) and the title of each window found by EnumWindows.

  3. "C++ EnumWindows filter by window class name"

    Description: Enumerate windows and filter by class name.

    #include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { wchar_t className[256]; GetClassName(hwnd, className, sizeof(className) / sizeof(className[0])); if (wcscmp(className, L"Notepad") == 0) { std::wcout << L"HWND: " << hwnd << L", Class: " << className << std::endl; } return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Filters windows by class name, printing details only for windows of class Notepad.

  4. "C++ EnumWindows list all window handles"

    Description: Enumerate all windows and list their handles.

    #include <windows.h> #include <iostream> #include <vector> std::vector<HWND> windowHandles; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { windowHandles.push_back(hwnd); return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); for (const auto& hwnd : windowHandles) { std::wcout << L"HWND: " << hwnd << std::endl; } return 0; } 

    Explanation: Collects all window handles in a vector and prints them.

  5. "C++ EnumWindows check if window is visible"

    Description: Enumerate windows and check if they are visible.

    #include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { if (IsWindowVisible(hwnd)) { std::wcout << L"Visible HWND: " << hwnd << std::endl; } return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Filters and prints only visible windows.

  6. "C++ EnumWindows exclude specific window titles"

    Description: Exclude specific windows by title from enumeration.

    #include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { wchar_t title[256]; GetWindowText(hwnd, title, sizeof(title) / sizeof(title[0])); if (wcscmp(title, L"Untitled - Notepad") != 0) { std::wcout << L"HWND: " << hwnd << L", Title: " << title << std::endl; } return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Excludes windows with the title Untitled - Notepad from being printed.

  7. "C++ EnumWindows get window size and position"

    Description: Enumerate windows and get their size and position.

    #include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { RECT rect; GetWindowRect(hwnd, &rect); std::wcout << L"HWND: " << hwnd << L", Position: (" << rect.left << L", " << rect.top << L"), Size: (" << (rect.right - rect.left) << L"x" << (rect.bottom - rect.top) << L")" << std::endl; return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Retrieves and prints the size and position of each window.

  8. "C++ EnumWindows get window owner process ID"

    Description: Get the process ID of the owner of each window.

    #include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { DWORD processId; GetWindowThreadProcessId(hwnd, &processId); std::wcout << L"HWND: " << hwnd << L", Process ID: " << processId << std::endl; return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Retrieves the process ID for each window and prints it.

  9. "C++ EnumWindows list all top-level windows"

    Description: Enumerate and list all top-level windows.

    #include <windows.h> #include <iostream> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { if (IsWindow(hwnd) && GetParent(hwnd) == NULL) { std::wcout << L"Top-level HWND: " << hwnd << std::endl; } return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Filters out non-top-level windows and lists only top-level windows.

  10. "C++ EnumWindows get window handles of specific process"

    Description: Enumerate windows and get handles of those belonging to a specific process.

    #include <windows.h> #include <iostream> DWORD targetProcessId = 1234; // Replace with the target process ID BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { DWORD processId; GetWindowThreadProcessId(hwnd, &processId); if (processId == targetProcessId) { std::wcout << L"Process ID: " << processId << L", HWND: " << hwnd << std::endl; } return TRUE; } int main() { EnumWindows(EnumWindowsProc, 0); return 0; } 

    Explanation: Filters windows by the process ID, printing only those belonging to the specified process.


More Tags

dhtml android-contentresolver android-gallery log4net colorama acumatica height dql entity-framework-4.1 rotativa

More Programming Questions

More Retirement Calculators

More Investment Calculators

More Entertainment Anecdotes Calculators

More Geometry Calculators