jack06215

[c++ win] List of active windows

Jul 7th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <Windows.h>
  2.  
  3. BOOL CALLBACK speichereFenster(HWND hwnd, LPARAM lParam) {
  4.     const DWORD TITLE_SIZE = 1024;
  5.     WCHAR windowTitle[TITLE_SIZE];
  6.  
  7.     GetWindowTextW(hwnd, windowTitle, TITLE_SIZE);
  8.  
  9.     int length = ::GetWindowTextLength(hwnd);
  10.     std::wstring title(&windowTitle[0]);
  11.     if (!IsWindowVisible(hwnd) || length == 0 || title == L"Program Manager") {
  12.         return TRUE;
  13.     }
  14.  
  15.     // Retrieve the pointer passed into this callback, and re-'type' it.
  16.     // The only way for a C API to pass arbitrary data is by means of a void*.
  17.     std::vector<std::wstring>& titles = *reinterpret_cast<std::vector<std::wstring>*>(lParam);
  18.     titles.push_back(title);
  19.  
  20.     return TRUE;
  21. }
  22.  
  23. int main() {
  24.     std::vector<std::wstring> titles;
  25.     EnumWindows(speichereFenster, reinterpret_cast<LPARAM>(&titles));
  26.     // At this point, titles if fully populated and could be displayed, e.g.:
  27.     for (const auto& title : titles)
  28.         std::wcout << L"Title: " << title << std::endl;
  29.     std::cin.get();
  30.  
  31.     return 0;
  32. }
Add Comment
Please, Sign In to add comment