Source code of Windows XP (NT5)
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.

336 lines
5.6 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. wnd.c
  5. Abstract:
  6. Utilities for window management
  7. Author:
  8. Jim Schmidt (jimschm) 01-Feb-2000
  9. Revision History:
  10. --*/
  11. //
  12. // Includes
  13. //
  14. #include "pch.h"
  15. #define DBG_WND "Wnd"
  16. //
  17. // Strings
  18. //
  19. // None
  20. //
  21. // Constants
  22. //
  23. // None
  24. //
  25. // Macros
  26. //
  27. // None
  28. //
  29. // Types
  30. //
  31. typedef struct {
  32. PCSTR WindowTitle;
  33. DWORD ProcessId;
  34. HWND Match;
  35. } FINDWINDOW_STRUCTA, *PFINDWINDOW_STRUCTA;
  36. typedef struct {
  37. PCWSTR WindowTitle;
  38. DWORD ProcessId;
  39. HWND Match;
  40. } FINDWINDOW_STRUCTW, *PFINDWINDOW_STRUCTW;
  41. //
  42. // Globals
  43. //
  44. // None
  45. //
  46. // Macro expansion list
  47. //
  48. // None
  49. //
  50. // Private function prototypes
  51. //
  52. // None
  53. //
  54. // Macro expansion definition
  55. //
  56. // None
  57. //
  58. // Code
  59. //
  60. BOOL
  61. CALLBACK
  62. pEnumWndProcA (
  63. HWND hwnd,
  64. LPARAM lParam
  65. )
  66. /*++
  67. Routine Description:
  68. A callback that is called for every top level window on the system.
  69. It is used with pFindParentWindow to locate a specific window.
  70. Arguments:
  71. hwnd - Specifies the handle of the current enumerated window
  72. lParam - Specifies a pointer to a FINDWINDOW_STRUCTA variable that
  73. holds WindowTitle and ProcessId, and receives the
  74. handle if a match is found.
  75. Return Value:
  76. The handle to the matching window, or NULL if no window has the
  77. specified title and process ID.
  78. --*/
  79. {
  80. CHAR title[MAX_MBCHAR_PATH];
  81. DWORD processId;
  82. PFINDWINDOW_STRUCTA p;
  83. BOOL match = FALSE;
  84. p = (PFINDWINDOW_STRUCTA) lParam;
  85. if (!GetWindowThreadProcessId (hwnd, &processId)) {
  86. DEBUGMSG ((DBG_WND, "Enumerated hwnd no longer valid"));
  87. return TRUE;
  88. }
  89. if (processId == p->ProcessId) {
  90. match = TRUE;
  91. }
  92. if (p->WindowTitle) {
  93. GetWindowTextA (hwnd, title, ARRAYSIZE(title));
  94. DEBUGMSGA ((
  95. DBG_NAUSEA,
  96. "Testing window: %s, ID=%08Xh against %s, %08Xh",
  97. title,
  98. processId,
  99. p->WindowTitle,
  100. p->ProcessId
  101. ));
  102. match = match && StringMatchA (title, p->WindowTitle);
  103. }
  104. ELSE_DEBUGMSGA ((
  105. DBG_NAUSEA,
  106. "Testing window: Process ID=%08Xh against %08Xh",
  107. processId,
  108. p->ProcessId
  109. ));
  110. if (match) {
  111. p->Match = hwnd;
  112. #ifdef DEBUG
  113. //
  114. // Get the window title for the following debug message
  115. //
  116. GetWindowTextA (hwnd, title, ARRAYSIZE(title));
  117. DEBUGMSGA ((
  118. DBG_NAUSEA,
  119. "Window found: %s, ID=%u",
  120. title,
  121. processId
  122. ));
  123. #endif
  124. return FALSE; // stop enum
  125. }
  126. return TRUE; // continue enum
  127. }
  128. BOOL
  129. CALLBACK
  130. pEnumWndProcW (
  131. HWND hwnd,
  132. LPARAM lParam
  133. )
  134. {
  135. WCHAR title[MAX_MBCHAR_PATH];
  136. DWORD processId;
  137. PFINDWINDOW_STRUCTW p;
  138. BOOL match = FALSE;
  139. p = (PFINDWINDOW_STRUCTW) lParam;
  140. if (!GetWindowThreadProcessId (hwnd, &processId)) {
  141. DEBUGMSG ((DBG_WND, "Enumerated hwnd no longer valid"));
  142. return TRUE;
  143. }
  144. if (processId == p->ProcessId) {
  145. match = TRUE;
  146. }
  147. if (p->WindowTitle) {
  148. GetWindowTextW (hwnd, title, ARRAYSIZE(title));
  149. DEBUGMSGW ((
  150. DBG_NAUSEA,
  151. "Testing window: %s, ID=%08Xh against %s, %08Xh",
  152. title,
  153. processId,
  154. p->WindowTitle,
  155. p->ProcessId
  156. ));
  157. match = match && StringMatchW (title, p->WindowTitle);
  158. }
  159. ELSE_DEBUGMSGW ((
  160. DBG_NAUSEA,
  161. "Testing window: Process ID=%08Xh against %08Xh",
  162. processId,
  163. p->ProcessId
  164. ));
  165. if (match) {
  166. p->Match = hwnd;
  167. #ifdef DEBUG
  168. //
  169. // Get the window title for the following debug message
  170. //
  171. GetWindowTextW (hwnd, title, ARRAYSIZE(title));
  172. DEBUGMSGA ((
  173. DBG_NAUSEA,
  174. "Window found: %s, ID=%u",
  175. title,
  176. processId
  177. ));
  178. #endif
  179. return FALSE; // stop enum
  180. }
  181. return TRUE; // continue enum
  182. }
  183. HWND
  184. FindWindowInProcessA (
  185. IN DWORD ProcessId,
  186. IN PCSTR WindowTitle OPTIONAL
  187. )
  188. /*++
  189. Routine Description:
  190. Finds a window by enumerating all top-level windows, and checking the
  191. process id. The first one to match the optionally supplied title is used.
  192. Arguments:
  193. ProcessId - Specifies the ID of the process who owns the window. If
  194. zero is specified, NULL is returned.
  195. WindowTitle - Specifies the name of the window to find.
  196. Return Value:
  197. The handle to the matching window, or NULL if no window has the
  198. specified title and process ID.
  199. --*/
  200. {
  201. FINDWINDOW_STRUCTA findWndStruct;
  202. //
  203. // If no process ID, we cannot have a match
  204. //
  205. if (!ProcessId) {
  206. DEBUGMSG ((DBG_WND, "ProcessId == 0"));
  207. return NULL;
  208. }
  209. ZeroMemory (&findWndStruct, sizeof (findWndStruct));
  210. findWndStruct.WindowTitle = WindowTitle;
  211. findWndStruct.ProcessId = ProcessId;
  212. EnumWindows (pEnumWndProcA, (LPARAM) &findWndStruct);
  213. return findWndStruct.Match;
  214. }
  215. HWND
  216. FindWindowInProcessW (
  217. IN DWORD ProcessId,
  218. IN PCWSTR WindowTitle OPTIONAL
  219. )
  220. {
  221. FINDWINDOW_STRUCTW findWndStruct;
  222. //
  223. // If no process ID, we cannot have a match
  224. //
  225. if (!ProcessId) {
  226. DEBUGMSG ((DBG_WND, "ProcessId == 0"));
  227. return NULL;
  228. }
  229. ZeroMemory (&findWndStruct, sizeof (findWndStruct));
  230. findWndStruct.WindowTitle = WindowTitle;
  231. findWndStruct.ProcessId = ProcessId;
  232. EnumWindows (pEnumWndProcW, (LPARAM) &findWndStruct);
  233. return findWndStruct.Match;
  234. }