1
0

auto-scene-switcher-win.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <windows.h>
  2. #include <util/platform.h>
  3. #include "auto-scene-switcher.hpp"
  4. using namespace std;
  5. static bool GetWindowTitle(HWND window, string &title)
  6. {
  7. size_t len = (size_t)GetWindowTextLengthW(window);
  8. wstring wtitle;
  9. wtitle.resize(len);
  10. if (!GetWindowTextW(window, &wtitle[0], (int)len + 1))
  11. return false;
  12. len = os_wcs_to_utf8(wtitle.c_str(), 0, nullptr, 0);
  13. title.resize(len);
  14. os_wcs_to_utf8(wtitle.c_str(), 0, &title[0], len + 1);
  15. return true;
  16. }
  17. static bool WindowValid(HWND window)
  18. {
  19. LONG_PTR styles, ex_styles;
  20. RECT rect;
  21. DWORD id;
  22. if (!IsWindowVisible(window))
  23. return false;
  24. GetWindowThreadProcessId(window, &id);
  25. if (id == GetCurrentProcessId())
  26. return false;
  27. GetClientRect(window, &rect);
  28. styles = GetWindowLongPtr(window, GWL_STYLE);
  29. ex_styles = GetWindowLongPtr(window, GWL_EXSTYLE);
  30. if (ex_styles & WS_EX_TOOLWINDOW)
  31. return false;
  32. if (styles & WS_CHILD)
  33. return false;
  34. return true;
  35. }
  36. void GetWindowList(vector<string> &windows)
  37. {
  38. HWND window = GetWindow(GetDesktopWindow(), GW_CHILD);
  39. while (window) {
  40. string title;
  41. if (WindowValid(window) && GetWindowTitle(window, title))
  42. windows.emplace_back(title);
  43. window = GetNextWindow(window, GW_HWNDNEXT);
  44. }
  45. }
  46. void GetCurrentWindowTitle(string &title)
  47. {
  48. HWND window = GetForegroundWindow();
  49. DWORD id;
  50. GetWindowThreadProcessId(window, &id);
  51. if (id == GetCurrentProcessId()) {
  52. title = "";
  53. return;
  54. }
  55. GetWindowTitle(window, title);
  56. }
  57. void CleanupSceneSwitcher() {}