Leaked source code of windows server 2003
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.

188 lines
4.2 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. ForceApplicationFocus.cpp
  5. Abstract:
  6. This shim calls SetForegroundWindow after CreateWindowEx and ShowWindow
  7. calls to fix focus problems that applications tend to have when they
  8. create/destroy windows on startup and manage to lose the foreground focus.
  9. Notes:
  10. This is a general purpose shim.
  11. History:
  12. 12/02/1999 markder Created
  13. --*/
  14. #include "precomp.h"
  15. IMPLEMENT_SHIM_BEGIN(ForceApplicationFocus)
  16. #include "ShimHookMacro.h"
  17. APIHOOK_ENUM_BEGIN
  18. APIHOOK_ENUM_ENTRY(ShowWindow)
  19. APIHOOK_ENUM_ENTRY(CreateWindowExA)
  20. APIHOOK_ENUM_ENTRY(CreateWindowExW)
  21. APIHOOK_ENUM_END
  22. /*++
  23. Calls SetForegroundWindow directly after a ShowWindow call with SW_SHOW as
  24. the operation. The mouse_event call allows the SetForegroundWindow call to
  25. succeed. This is a hack borrowed from the DirectX sources.
  26. --*/
  27. BOOL
  28. APIHOOK(ShowWindow)(
  29. HWND hWnd,
  30. INT nCmdShow
  31. )
  32. {
  33. BOOL bReturn;
  34. bReturn = ORIGINAL_API(ShowWindow)(hWnd, nCmdShow);
  35. if (nCmdShow == SW_SHOW)
  36. {
  37. if (hWnd != GetForegroundWindow()) {
  38. LOGN( eDbgLevelWarning,
  39. "ShowWindow called for non-foreground window. Forcing to foreground.");
  40. }
  41. mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 0, 0);
  42. SetForegroundWindow(hWnd);
  43. }
  44. return bReturn;
  45. }
  46. /*++
  47. Calls SetForegroundWindow directly after a CreateWindowEx call with
  48. WS_VISIBLE as a style. The mouse_event call allows the
  49. SetForegroundWindow call to succeed. This is a hack borrowed from
  50. the DirectX sources.
  51. --*/
  52. HWND
  53. APIHOOK(CreateWindowExA)(
  54. DWORD dwExStyle,
  55. LPCSTR lpClassName,
  56. LPCSTR lpWindowName,
  57. DWORD dwStyle,
  58. int x,
  59. int y,
  60. int nWidth,
  61. int nHeight,
  62. HWND hWndParent,
  63. HMENU hMenu,
  64. HINSTANCE hInstance,
  65. LPVOID lpParam
  66. )
  67. {
  68. HWND hReturn;
  69. hReturn = ORIGINAL_API(CreateWindowExA)(
  70. dwExStyle,
  71. lpClassName,
  72. lpWindowName,
  73. dwStyle,
  74. x,
  75. y,
  76. nWidth,
  77. nHeight,
  78. hWndParent,
  79. hMenu,
  80. hInstance,
  81. lpParam);
  82. if (hReturn && (dwStyle & WS_VISIBLE))
  83. {
  84. if (hReturn != GetForegroundWindow()) {
  85. LOGN( eDbgLevelWarning,
  86. "CreateWindowExA: New window not foreground. Forcing to foreground.");
  87. }
  88. mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 0, 0);
  89. SetForegroundWindow(hReturn);
  90. }
  91. return hReturn;
  92. }
  93. /*++
  94. Calls SetForegroundWindow directly after a CreateWindowEx call with
  95. WS_VISIBLE as a style. The mouse_event call allows the
  96. SetForegroundWindow call to succeed. This is a hack borrowed from
  97. the DirectX sources.
  98. --*/
  99. HWND
  100. APIHOOK(CreateWindowExW)(
  101. DWORD dwExStyle,
  102. LPCWSTR lpClassName,
  103. LPCWSTR lpWindowName,
  104. DWORD dwStyle,
  105. int x,
  106. int y,
  107. int nWidth,
  108. int nHeight,
  109. HWND hWndParent,
  110. HMENU hMenu,
  111. HINSTANCE hInstance,
  112. LPVOID lpParam
  113. )
  114. {
  115. HWND hReturn;
  116. hReturn = ORIGINAL_API(CreateWindowExW)(
  117. dwExStyle,
  118. lpClassName,
  119. lpWindowName,
  120. dwStyle,
  121. x,
  122. y,
  123. nWidth,
  124. nHeight,
  125. hWndParent,
  126. hMenu,
  127. hInstance,
  128. lpParam);
  129. if (hReturn && (dwStyle & WS_VISIBLE))
  130. {
  131. if (hReturn != GetForegroundWindow()) {
  132. LOGN( eDbgLevelWarning, "CreateWindowExW: New window not foreground. Forcing to foreground.");
  133. }
  134. mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 0, 0);
  135. SetForegroundWindow(hReturn);
  136. }
  137. return hReturn;
  138. }
  139. /*++
  140. Register hooked functions
  141. --*/
  142. HOOK_BEGIN
  143. APIHOOK_ENTRY(USER32.DLL, ShowWindow)
  144. APIHOOK_ENTRY(USER32.DLL, CreateWindowExA)
  145. APIHOOK_ENTRY(USER32.DLL, CreateWindowExW)
  146. HOOK_END
  147. IMPLEMENT_SHIM_END