You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2010 Google Inc. All Rights Reserved
  2. #include "talk/base/win32windowpicker.h"
  3. #include <string>
  4. #include <vector>
  5. #include "talk/base/common.h"
  6. #include "talk/base/logging.h"
  7. namespace talk_base {
  8. namespace {
  9. // Window class names that we want to filter out.
  10. const char kProgramManagerClass[] = "Progman";
  11. const char kButtonClass[] = "Button";
  12. } // namespace
  13. BOOL CALLBACK Win32WindowPicker::EnumProc(HWND hwnd, LPARAM l_param) {
  14. WindowDescriptionList* descriptions =
  15. reinterpret_cast<WindowDescriptionList*>(l_param);
  16. // Skip windows that are invisible, minimized, have no title, or are owned,
  17. // unless they have the app window style set. Except for minimized windows,
  18. // this is what Alt-Tab does.
  19. // TODO: Figure out how to grab a thumbnail of a minimized window and
  20. // include them in the list.
  21. int len = GetWindowTextLength(hwnd);
  22. HWND owner = GetWindow(hwnd, GW_OWNER);
  23. LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE);
  24. if (len == 0 || IsIconic(hwnd) || !IsWindowVisible(hwnd) ||
  25. (owner && !(exstyle & WS_EX_APPWINDOW))) {
  26. // TODO: Investigate if windows without title still could be
  27. // interesting to share. We could use the name of the process as title:
  28. //
  29. // GetWindowThreadProcessId()
  30. // OpenProcess()
  31. // QueryFullProcessImageName()
  32. return TRUE;
  33. }
  34. // Skip the Program Manager window and the Start button.
  35. TCHAR class_name_w[500];
  36. ::GetClassName(hwnd, class_name_w, 500);
  37. std::string class_name = ToUtf8(class_name_w);
  38. if (class_name == kProgramManagerClass || class_name == kButtonClass) {
  39. // We don't want the Program Manager window nor the Start button.
  40. return TRUE;
  41. }
  42. TCHAR window_title[500];
  43. GetWindowText(hwnd, window_title, ARRAY_SIZE(window_title));
  44. std::string title = ToUtf8(window_title);
  45. WindowId id(hwnd);
  46. WindowDescription desc(id, title);
  47. descriptions->push_back(desc);
  48. return TRUE;
  49. }
  50. BOOL CALLBACK Win32WindowPicker::MonitorEnumProc(HMONITOR h_monitor,
  51. HDC hdc_monitor,
  52. LPRECT lprc_monitor,
  53. LPARAM l_param) {
  54. DesktopDescriptionList* desktop_desc =
  55. reinterpret_cast<DesktopDescriptionList*>(l_param);
  56. DesktopId id(h_monitor, static_cast<int>(desktop_desc->size()));
  57. // TODO: Figure out an appropriate desktop title.
  58. DesktopDescription desc(id, "");
  59. desktop_desc->push_back(desc);
  60. return TRUE;
  61. }
  62. Win32WindowPicker::Win32WindowPicker() {
  63. }
  64. bool Win32WindowPicker::Init() {
  65. return true;
  66. }
  67. // TODO: Consider changing enumeration to clear() descriptions
  68. // before append().
  69. bool Win32WindowPicker::GetWindowList(WindowDescriptionList* descriptions) {
  70. LPARAM desc = reinterpret_cast<LPARAM>(descriptions);
  71. return EnumWindows(Win32WindowPicker::EnumProc, desc) != FALSE;
  72. }
  73. bool Win32WindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) {
  74. // Create a fresh WindowDescriptionList so that we can use desktop_desc.size()
  75. // in MonitorEnumProc to compute the desktop index.
  76. DesktopDescriptionList desktop_desc;
  77. HDC hdc = GetDC(NULL);
  78. bool success = false;
  79. if (EnumDisplayMonitors(hdc, NULL, Win32WindowPicker::MonitorEnumProc,
  80. reinterpret_cast<LPARAM>(&desktop_desc)) != FALSE) {
  81. // Append the desktop descriptions to the end of the returned descriptions.
  82. descriptions->insert(descriptions->end(), desktop_desc.begin(),
  83. desktop_desc.end());
  84. success = true;
  85. }
  86. ReleaseDC(NULL, hdc);
  87. return success;
  88. }
  89. bool Win32WindowPicker::GetDesktopDimensions(const DesktopId& id,
  90. int* width,
  91. int* height) {
  92. MONITORINFOEX monitor_info;
  93. monitor_info.cbSize = sizeof(MONITORINFOEX);
  94. if (!GetMonitorInfo(id.id(), &monitor_info)) {
  95. return false;
  96. }
  97. *width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
  98. *height = monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top;
  99. return true;
  100. }
  101. bool Win32WindowPicker::IsVisible(const WindowId& id) {
  102. return (::IsWindow(id.id()) != FALSE && ::IsWindowVisible(id.id()) != FALSE);
  103. }
  104. bool Win32WindowPicker::MoveToFront(const WindowId& id) {
  105. return SetForegroundWindow(id.id()) != FALSE;
  106. }
  107. } // namespace talk_base