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.

570 lines
17 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: DimmedWindow.cpp
  3. //
  4. // Copyright (c) 2000, Microsoft Corporation
  5. //
  6. // Class that implements the dimmed window when displaying logoff / shut down
  7. // dialog.
  8. //
  9. // History: 2000-05-18 vtan created
  10. // --------------------------------------------------------------------------
  11. #include "StandardHeader.h"
  12. #include "DimmedWindow.h"
  13. #include "RegistryResources.h"
  14. // --------------------------------------------------------------------------
  15. // CDimmedWindow::s_szWindowClassName
  16. //
  17. // Purpose: static member variables.
  18. //
  19. // History: 2000-05-17 vtan created
  20. // --------------------------------------------------------------------------
  21. const TCHAR CDimmedWindow::s_szWindowClassName[] = TEXT("DimmedWindowClass");
  22. const TCHAR CDimmedWindow::s_szExplorerKeyName[] = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer");
  23. const TCHAR CDimmedWindow::s_szExplorerPolicyKeyName[] = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
  24. const TCHAR CDimmedWindow::s_szForceDimValueName[] = TEXT("ForceDimScreen");
  25. #define RCW(rc) ((rc).right - (rc).left)
  26. #define RCH(r) ((r).bottom - (r).top)
  27. #define CHUNK_SIZE 20
  28. void DimPixels(void* pvBitmapBits, int cLen, int Amount)
  29. {
  30. ULONG* pulSrc = (ULONG*)pvBitmapBits;
  31. for (int i = cLen - 1; i >= 0; i--)
  32. {
  33. ULONG ulR = GetRValue(*pulSrc);
  34. ULONG ulG = GetGValue(*pulSrc);
  35. ULONG ulB = GetBValue(*pulSrc);
  36. ULONG ulGray = (54 * ulR + 183 * ulG + 19 * ulB) >> 8;
  37. ULONG ulTemp = ulGray * (0xff - Amount);
  38. ulR = (ulR * Amount + ulTemp) >> 8;
  39. ulG = (ulG * Amount + ulTemp) >> 8;
  40. ulB = (ulB * Amount + ulTemp) >> 8;
  41. *pulSrc = (*pulSrc & 0xff000000) | RGB(ulR, ulG, ulB);
  42. pulSrc++;
  43. }
  44. }
  45. // --------------------------------------------------------------------------
  46. // CDimmedWindow::CDimmedWindow
  47. //
  48. // Arguments: hInstance = HINSTANCE of the hosting process/DLL.
  49. //
  50. // Returns: <none>
  51. //
  52. // Purpose: Constructor for CDimmedWindow. Registers the window class
  53. // DimmedWindowClass.
  54. //
  55. // History: 2000-05-17 vtan created
  56. // --------------------------------------------------------------------------
  57. CDimmedWindow::CDimmedWindow (HINSTANCE hInstance) :
  58. _lReferenceCount(1),
  59. _hInstance(hInstance),
  60. _atom(0),
  61. _hwnd(NULL),
  62. _fDithered(false),
  63. _pvPixels(NULL),
  64. _idxChunk(0),
  65. _idxSaturation(0),
  66. _hdcDimmed(NULL),
  67. _hbmOldDimmed(NULL),
  68. _hbmDimmed(NULL)
  69. {
  70. WNDCLASSEX wndClassEx;
  71. ZeroMemory(&wndClassEx, sizeof(wndClassEx));
  72. wndClassEx.cbSize = sizeof(wndClassEx);
  73. wndClassEx.lpfnWndProc = WndProc;
  74. wndClassEx.hInstance = hInstance;
  75. wndClassEx.lpszClassName = s_szWindowClassName;
  76. wndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
  77. _atom = RegisterClassEx(&wndClassEx);
  78. }
  79. // --------------------------------------------------------------------------
  80. // CDimmedWindow::~CDimmedWindow
  81. //
  82. // Arguments: <none>
  83. //
  84. // Returns: <none>
  85. //
  86. // Purpose: Destructor for CDimmedWindow. Destroys the dimmed window and
  87. // unregisters the window class.
  88. //
  89. // History: 2000-05-17 vtan created
  90. // --------------------------------------------------------------------------
  91. CDimmedWindow::~CDimmedWindow (void)
  92. {
  93. if (_hdcDimmed)
  94. {
  95. SelectObject(_hdcDimmed, _hbmOldDimmed);
  96. DeleteDC(_hdcDimmed);
  97. }
  98. if (_hbmDimmed)
  99. {
  100. DeleteObject(_hbmDimmed);
  101. }
  102. if (_hwnd != NULL)
  103. {
  104. (BOOL)DestroyWindow(_hwnd);
  105. }
  106. if (_atom != 0)
  107. {
  108. TBOOL(UnregisterClass(MAKEINTRESOURCE(_atom), _hInstance));
  109. }
  110. }
  111. // --------------------------------------------------------------------------
  112. // CDimmedWindow::QueryInterface
  113. //
  114. // Arguments: riid = Interface to query support of.
  115. // ppvObject = Returned interface if successful.
  116. //
  117. // Returns: HRESULT
  118. //
  119. // Purpose: Returns the specified interface implemented by this object.
  120. //
  121. // History: 2000-05-18 vtan created
  122. // --------------------------------------------------------------------------
  123. HRESULT CDimmedWindow::QueryInterface (REFIID riid, void **ppvObject)
  124. {
  125. HRESULT hr;
  126. if (IsEqualGUID(riid, IID_IUnknown))
  127. {
  128. *ppvObject = static_cast<IUnknown*>(this);
  129. (LONG)InterlockedIncrement(&_lReferenceCount);
  130. hr = S_OK;
  131. }
  132. else
  133. {
  134. *ppvObject = NULL;
  135. hr = E_NOINTERFACE;
  136. }
  137. return(hr);
  138. }
  139. // --------------------------------------------------------------------------
  140. // CDimmedWindow::AddRef
  141. //
  142. // Arguments: <none>
  143. //
  144. // Returns: ULONG
  145. //
  146. // Purpose: Increments the reference count and returns that value.
  147. //
  148. // History: 2000-05-18 vtan created
  149. // --------------------------------------------------------------------------
  150. ULONG CDimmedWindow::AddRef (void)
  151. {
  152. return(static_cast<ULONG>(InterlockedIncrement(&_lReferenceCount)));
  153. }
  154. // --------------------------------------------------------------------------
  155. // CDimmedWindow::Release
  156. //
  157. // Arguments: <none>
  158. //
  159. // Returns: ULONG
  160. //
  161. // Purpose: Decrements the reference count and if it reaches zero deletes
  162. // the object.
  163. //
  164. // History: 2000-05-18 vtan created
  165. // --------------------------------------------------------------------------
  166. ULONG CDimmedWindow::Release (void)
  167. {
  168. ASSERT( 0 != _lReferenceCount );
  169. ULONG cRef = InterlockedDecrement(&_lReferenceCount);
  170. if ( 0 == cRef )
  171. {
  172. delete this;
  173. }
  174. return cRef;
  175. }
  176. // --------------------------------------------------------------------------
  177. // CDimmedWindow::Create
  178. //
  179. // Arguments: <none>
  180. //
  181. // Returns: HWND
  182. //
  183. // Purpose: Creates the dimmed window. Creates the window so that it
  184. // covers the whole screen area.
  185. //
  186. // History: 2000-05-17 vtan created
  187. // --------------------------------------------------------------------------
  188. HWND CDimmedWindow::Create (void)
  189. {
  190. BOOL fScreenReader;
  191. bool fNoDebuggerPresent, fConsoleSession, fNoScreenReaderPresent;
  192. fNoDebuggerPresent = !IsDebuggerPresent();
  193. fConsoleSession = (GetSystemMetrics(SM_REMOTESESSION) == FALSE);
  194. fNoScreenReaderPresent = ((SystemParametersInfo(SPI_GETSCREENREADER, 0, &fScreenReader, 0) == FALSE) || (fScreenReader == FALSE));
  195. if (fNoDebuggerPresent &&
  196. fConsoleSession &&
  197. fNoScreenReaderPresent)
  198. {
  199. _xVirtualScreen = GetSystemMetrics(SM_XVIRTUALSCREEN);
  200. _yVirtualScreen = GetSystemMetrics(SM_YVIRTUALSCREEN);
  201. _cxVirtualScreen = GetSystemMetrics(SM_CXVIRTUALSCREEN);
  202. _cyVirtualScreen = GetSystemMetrics(SM_CYVIRTUALSCREEN);
  203. _hwnd = CreateWindowEx(WS_EX_TOPMOST,
  204. s_szWindowClassName,
  205. NULL,
  206. WS_POPUP,
  207. _xVirtualScreen, _yVirtualScreen,
  208. _cxVirtualScreen, _cyVirtualScreen,
  209. NULL, NULL, _hInstance, this);
  210. if (_hwnd != NULL)
  211. {
  212. bool fDimmed;
  213. fDimmed = false;
  214. (BOOL)ShowWindow(_hwnd, SW_SHOW);
  215. TBOOL(SetForegroundWindow(_hwnd));
  216. // For beta: Always use a dither
  217. // if ((GetLowestScreenBitDepth() <= 8) || !IsDimScreen())
  218. {
  219. _fDithered = true;
  220. }
  221. (BOOL)EnableWindow(_hwnd, FALSE);
  222. }
  223. }
  224. return(_hwnd);
  225. }
  226. // --------------------------------------------------------------------------
  227. // CDimmedWindow::GetLowestScreenBitDepth
  228. //
  229. // Arguments: <none>
  230. //
  231. // Returns: int
  232. //
  233. // Purpose: Iterates the display devices looking the display with the
  234. // lowest bit depth.
  235. //
  236. // History: 2000-05-22 vtan created
  237. // --------------------------------------------------------------------------
  238. int CDimmedWindow::GetLowestScreenBitDepth (void) const
  239. {
  240. enum
  241. {
  242. INITIAL_VALUE = 256
  243. };
  244. BOOL fResult;
  245. int iLowestScreenBitDepth, iDeviceNumber;
  246. DISPLAY_DEVICE displayDevice;
  247. iLowestScreenBitDepth = INITIAL_VALUE; // Start at beyond 32-bit depth.
  248. iDeviceNumber = 0;
  249. displayDevice.cb = sizeof(displayDevice);
  250. fResult = EnumDisplayDevices(NULL, iDeviceNumber, &displayDevice, 0);
  251. while (fResult != FALSE)
  252. {
  253. if ((displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0)
  254. {
  255. HDC hdcDisplay;
  256. hdcDisplay = CreateDC(displayDevice.DeviceName, displayDevice.DeviceName, NULL, NULL);
  257. if (hdcDisplay != NULL)
  258. {
  259. int iResult;
  260. iResult = GetDeviceCaps(hdcDisplay, BITSPIXEL);
  261. if (iResult < iLowestScreenBitDepth)
  262. {
  263. iLowestScreenBitDepth = iResult;
  264. }
  265. TBOOL(DeleteDC(hdcDisplay));
  266. }
  267. }
  268. displayDevice.cb = sizeof(displayDevice);
  269. fResult = EnumDisplayDevices(NULL, ++iDeviceNumber, &displayDevice, 0);
  270. }
  271. if (INITIAL_VALUE == iLowestScreenBitDepth)
  272. {
  273. iLowestScreenBitDepth = 8;
  274. }
  275. return(iLowestScreenBitDepth);
  276. }
  277. // --------------------------------------------------------------------------
  278. // CDimmedWindow::IsForcedDimScreen
  279. //
  280. // Arguments: <none>
  281. //
  282. // Returns: bool
  283. //
  284. // Purpose: Returns whether the force override of dimming is set on this
  285. // user or this machine. Check the local machine first. Then
  286. // check the user setting. Then check the user policy. Then
  287. // check the local machine policy.
  288. //
  289. // History: 2000-05-23 vtan created
  290. // --------------------------------------------------------------------------
  291. bool CDimmedWindow::IsForcedDimScreen (void) const
  292. {
  293. DWORD dwForceDimScreen;
  294. CRegKey regKey;
  295. dwForceDimScreen = 0;
  296. if (ERROR_SUCCESS == regKey.Open(HKEY_LOCAL_MACHINE, s_szExplorerKeyName, KEY_QUERY_VALUE))
  297. {
  298. (LONG)regKey.GetDWORD(s_szForceDimValueName, dwForceDimScreen);
  299. }
  300. if (ERROR_SUCCESS == regKey.OpenCurrentUser(s_szExplorerKeyName, KEY_QUERY_VALUE))
  301. {
  302. (LONG)regKey.GetDWORD(s_szForceDimValueName, dwForceDimScreen);
  303. }
  304. if (ERROR_SUCCESS == regKey.OpenCurrentUser(s_szExplorerPolicyKeyName, KEY_QUERY_VALUE))
  305. {
  306. (LONG)regKey.GetDWORD(s_szForceDimValueName, dwForceDimScreen);
  307. }
  308. if (ERROR_SUCCESS == regKey.Open(HKEY_LOCAL_MACHINE, s_szExplorerPolicyKeyName, KEY_QUERY_VALUE))
  309. {
  310. (LONG)regKey.GetDWORD(s_szForceDimValueName, dwForceDimScreen);
  311. }
  312. return(dwForceDimScreen != 0);
  313. }
  314. // --------------------------------------------------------------------------
  315. // CDimmedWindow::IsDimScreen
  316. //
  317. // Arguments: <none>
  318. //
  319. // Returns: bool
  320. //
  321. // Purpose: Returns whether the screen should be dimmed. If not then the
  322. // screen will be dithered instead which is a cheaper operation
  323. // by doesn't look as nice.
  324. //
  325. // 1) If UI effects are disabled then don't ever dim.
  326. // 2) Dim if screen area is small enough OR forced to dim.
  327. //
  328. // History: 2000-05-23 vtan created
  329. // --------------------------------------------------------------------------
  330. bool CDimmedWindow::IsDimScreen (void) const
  331. {
  332. bool fIsUIEffectsActive;
  333. BOOL fTemp;
  334. fIsUIEffectsActive = (SystemParametersInfo(SPI_GETUIEFFECTS, 0, &fTemp, 0) != FALSE) && (fTemp != FALSE);
  335. return(fIsUIEffectsActive && IsForcedDimScreen());
  336. }
  337. BOOL CDimmedWindow::StepDim()
  338. {
  339. HDC hdcWindow = GetDC(_hwnd);
  340. if (_idxChunk >= 0 )
  341. {
  342. //
  343. // In the first couple of passes, we slowly collect the screen
  344. // into our bitmap. We do this because Blt-ing the whole thing
  345. // causes the system to hang. By doing it this way, we continue
  346. // to pump messages, the UI stays responsive and it keeps the
  347. // mouse alive.
  348. //
  349. int y = _idxChunk * CHUNK_SIZE;
  350. BitBlt(_hdcDimmed, 0, y, _cxVirtualScreen, CHUNK_SIZE, hdcWindow, 0, y, SRCCOPY);
  351. _idxChunk--;
  352. if (_idxChunk < 0)
  353. {
  354. //
  355. // We're done getting the bitmap, now reset the timer
  356. // so we slowly fade to grey.
  357. //
  358. SetTimer(_hwnd, 1, 250, NULL);
  359. _idxSaturation = 16;
  360. }
  361. return TRUE; // don't kill the timer.
  362. }
  363. else
  364. {
  365. //
  366. // In these passes, we are making the image more and more grey and
  367. // then Blt-ing the result to the screen.
  368. //
  369. DimPixels(_pvPixels, _cxVirtualScreen * _cyVirtualScreen, 0xd5);
  370. BitBlt(hdcWindow, 0, 0, _cxVirtualScreen, _cyVirtualScreen, _hdcDimmed, 0, 0, SRCCOPY);
  371. _idxSaturation--;
  372. return (_idxSaturation > 0); // when we hit zero, kill the timer.
  373. }
  374. }
  375. void CDimmedWindow::SetupDim()
  376. {
  377. HDC hdcWindow = GetDC(_hwnd);
  378. if (hdcWindow != NULL)
  379. {
  380. _hdcDimmed = CreateCompatibleDC(hdcWindow);
  381. if (_hdcDimmed != NULL)
  382. {
  383. BITMAPINFO bmi;
  384. ZeroMemory(&bmi, sizeof(bmi));
  385. bmi.bmiHeader.biSize = sizeof(bmi);
  386. bmi.bmiHeader.biWidth = _cxVirtualScreen;
  387. bmi.bmiHeader.biHeight = _cyVirtualScreen;
  388. bmi.bmiHeader.biPlanes = 1;
  389. bmi.bmiHeader.biBitCount = 32;
  390. bmi.bmiHeader.biCompression = BI_RGB;
  391. bmi.bmiHeader.biSizeImage = 0;
  392. _hbmDimmed = CreateDIBSection(_hdcDimmed, &bmi, DIB_RGB_COLORS, &_pvPixels, NULL, 0);
  393. if (_hbmDimmed != NULL)
  394. {
  395. _hbmOldDimmed = (HBITMAP) SelectObject(_hdcDimmed, _hbmDimmed);
  396. _idxChunk = _cyVirtualScreen / CHUNK_SIZE;
  397. }
  398. else
  399. {
  400. ASSERT( NULL == _pvPixels );
  401. DeleteDC(_hdcDimmed);
  402. _hdcDimmed = NULL;
  403. }
  404. }
  405. ReleaseDC(_hwnd, hdcWindow);
  406. }
  407. }
  408. void CDimmedWindow::Dither()
  409. {
  410. static const WORD s_dwGrayBits[] =
  411. {
  412. 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA
  413. };
  414. HDC hdcWindow = GetDC(_hwnd);
  415. if (hdcWindow != NULL)
  416. {
  417. HBITMAP hbmDimmed = CreateBitmap(8, 8, 1, 1, s_dwGrayBits);
  418. if (hbmDimmed != NULL)
  419. {
  420. HBRUSH hbrDimmed = CreatePatternBrush(hbmDimmed);
  421. if (hbrDimmed != NULL)
  422. {
  423. static const int ROP_DPna = 0x000A0329;
  424. RECT rc;
  425. HBRUSH hbrSelected = static_cast<HBRUSH>(SelectObject(hdcWindow, hbrDimmed));
  426. TBOOL(GetClientRect(_hwnd, &rc));
  427. TBOOL(PatBlt(hdcWindow, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, ROP_DPna));
  428. SelectObject(hdcWindow, hbrSelected);
  429. TBOOL(DeleteObject(hbrDimmed));
  430. }
  431. TBOOL(DeleteObject(hbmDimmed));
  432. }
  433. TBOOL(ReleaseDC(_hwnd, hdcWindow));
  434. }
  435. }
  436. // --------------------------------------------------------------------------
  437. // CDimmedWindow::WndProc
  438. //
  439. // Arguments: See the platform SDK under WindowProc.
  440. //
  441. // Returns: See the platform SDK under WindowProc.
  442. //
  443. // Purpose: WindowProc for the dimmed window. This just passes the
  444. // messages thru to DefWindowProc.
  445. //
  446. // History: 2000-05-17 vtan created
  447. // --------------------------------------------------------------------------
  448. LRESULT CALLBACK CDimmedWindow::WndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  449. {
  450. LRESULT lResult = 0;
  451. CDimmedWindow *pThis;
  452. pThis = reinterpret_cast<CDimmedWindow*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
  453. switch (uMsg)
  454. {
  455. case WM_CREATE:
  456. {
  457. CREATESTRUCT *pCreateStruct;
  458. pCreateStruct = reinterpret_cast<CREATESTRUCT*>(lParam);
  459. pThis = reinterpret_cast<CDimmedWindow*>(pCreateStruct->lpCreateParams);
  460. (LONG_PTR)SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
  461. lResult = 0;
  462. if (pThis->_fDithered)
  463. pThis->Dither();
  464. else
  465. {
  466. pThis->SetupDim();
  467. if (pThis->_hdcDimmed)
  468. {
  469. SetTimer(hwnd, 1, 30, NULL);
  470. }
  471. }
  472. break;
  473. }
  474. case WM_TIMER:
  475. if (!pThis->StepDim())
  476. KillTimer(hwnd, 1);
  477. break;
  478. case WM_PAINT:
  479. {
  480. HDC hdcPaint;
  481. PAINTSTRUCT ps;
  482. hdcPaint = BeginPaint(hwnd, &ps);
  483. TBOOL(EndPaint(hwnd, &ps));
  484. lResult = 0;
  485. break;
  486. }
  487. default:
  488. lResult = DefWindowProc(hwnd, uMsg, wParam, lParam);
  489. break;
  490. }
  491. return(lResult);
  492. }