finder.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "finder.h"
  2. #include <Windows.h>
  3. #include <array>
  4. const std::vector<WindowFinder::Info>& WindowFinder::GetList(bool isUpdate)
  5. {
  6. if (!isUpdate) {
  7. return _list;
  8. }
  9. _list.clear();
  10. EnumWindows(_EnumWindowsProc, (LPARAM) nullptr);
  11. return _list;
  12. }
  13. std::vector<WindowFinder::Info> WindowFinder::_list;
  14. std::wstring WindowFinder::_GetWindowTextStd(HWND hwnd)
  15. {
  16. std::array<WCHAR, 1024> windowText;
  17. ::GetWindowTextW(hwnd, windowText.data(), (int)windowText.size());
  18. std::wstring title(windowText.data());
  19. return title;
  20. }
  21. BOOL CALLBACK WindowFinder::_EnumWindowsProc(HWND hwnd, LPARAM lParam)
  22. {
  23. auto title = _GetWindowTextStd(hwnd);
  24. if (!IsAltTabWindow(hwnd, title)) {
  25. return TRUE;
  26. }
  27. _list.push_back({hwnd, std::move(title)});
  28. return TRUE;
  29. }
  30. bool WindowFinder::IsAltTabWindow(HWND hwnd, const std::wstring& title)
  31. {
  32. HWND shellWindow = GetShellWindow();
  33. if (hwnd == shellWindow) {
  34. return false;
  35. }
  36. if (title.length() == 0 || title == L"NVIDIA GeForce Overlay") {
  37. return false;
  38. }
  39. if (!IsWindowVisible(hwnd)) {
  40. return false;
  41. }
  42. if (GetAncestor(hwnd, GA_ROOT) != hwnd) {
  43. return false;
  44. }
  45. LONG style = GetWindowLong(hwnd, GWL_STYLE);
  46. if (!((style & WS_DISABLED) != WS_DISABLED)) {
  47. return false;
  48. }
  49. DWORD cloaked = FALSE;
  50. HRESULT hrTemp = DwmGetWindowAttribute(hwnd, DWMWA_CLOAKED, &cloaked, sizeof(cloaked));
  51. if (SUCCEEDED(hrTemp) && cloaked == DWM_CLOAKED_SHELL) {
  52. return false;
  53. }
  54. return !IsIconic(hwnd);
  55. }
  56. const std::vector<MonitorFinder::Info>& MonitorFinder::GetList(bool isUpdate)
  57. {
  58. if (!isUpdate) {
  59. return _list;
  60. }
  61. _list.clear();
  62. EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProc, (LPARAM) nullptr);
  63. return _list;
  64. }
  65. std::vector<MonitorFinder::Info> MonitorFinder::_list;
  66. BOOL CALLBACK MonitorFinder::_MonitorEnumProc(
  67. HMONITOR hMonitor, // handle to display monitor
  68. HDC hdcMonitor, // handle to monitor-appropriate device context
  69. LPRECT lprcMonitor, // pointer to monitor intersection rectangle
  70. LPARAM dwData // data passed from EnumDisplayMonitors
  71. )
  72. {
  73. std::wstring name = L"显示器" + std::to_wstring(_list.size() + 1);
  74. MONITORINFO monitorInfo;
  75. monitorInfo.cbSize = sizeof(monitorInfo);
  76. GetMonitorInfoW(hMonitor, &monitorInfo);
  77. Info info;
  78. info.monitor = hMonitor;
  79. info.rect = monitorInfo.rcMonitor;
  80. info.title = std::move(name);
  81. _list.push_back(std::move(info));
  82. return TRUE;
  83. }