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.

149 lines
3.9 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998, 1999, 2000
  4. *
  5. * TITLE: SSUTIL.CPP
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 1/13/1999
  12. *
  13. * DESCRIPTION: Useful functions that are used more than once
  14. *
  15. *******************************************************************************/
  16. #include "precomp.h"
  17. #pragma hdrstop
  18. #include "ssutil.h"
  19. #include <shlobj.h>
  20. #include "isdbg.h"
  21. bool ScreenSaverUtil::SetIcons( HWND hWnd, HINSTANCE hInstance, int nResId )
  22. {
  23. HICON hIconSmall = (HICON)LoadImage( hInstance, MAKEINTRESOURCE(nResId), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0 );
  24. HICON hIconLarge = (HICON)LoadImage( hInstance, MAKEINTRESOURCE(nResId), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0 );
  25. if (hIconSmall)
  26. {
  27. SendMessage( hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall );
  28. }
  29. if (hIconLarge)
  30. {
  31. SendMessage( hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIconLarge );
  32. }
  33. return(hIconSmall && hIconLarge);
  34. }
  35. bool ScreenSaverUtil::IsValidRect( RECT &rc )
  36. {
  37. return(rc.left < rc.right && rc.top < rc.bottom);
  38. }
  39. void ScreenSaverUtil::NormalizeRect( RECT &rc )
  40. {
  41. if (rc.left > rc.right)
  42. Swap(rc.left,rc.right);
  43. if (rc.top > rc.bottom)
  44. Swap(rc.top,rc.bottom);
  45. }
  46. void ScreenSaverUtil::EraseDiffRect( HDC hDC, const RECT &oldRect, const RECT &newRect, HBRUSH hBrush )
  47. {
  48. RECT rc;
  49. // Top
  50. rc.left = oldRect.left;
  51. rc.top = oldRect.top;
  52. rc.bottom = newRect.top;
  53. rc.right = oldRect.right;
  54. if (IsValidRect(rc))
  55. {
  56. FillRect( hDC, &rc, hBrush );
  57. }
  58. // Left
  59. rc.left = oldRect.left;
  60. rc.top = newRect.top;
  61. rc.right = newRect.left;
  62. rc.bottom = newRect.bottom;
  63. if (IsValidRect(rc))
  64. {
  65. FillRect( hDC, &rc, hBrush );
  66. }
  67. // Right
  68. rc.left = newRect.right;
  69. rc.top = newRect.top;
  70. rc.right = oldRect.right;
  71. rc.bottom = newRect.bottom;
  72. if (IsValidRect(rc))
  73. {
  74. FillRect( hDC, &rc, hBrush );
  75. }
  76. // Bottom
  77. rc.left = oldRect.left;
  78. rc.top = newRect.bottom;
  79. rc.right = oldRect.right;
  80. rc.bottom = oldRect.bottom;
  81. if (IsValidRect(rc))
  82. {
  83. FillRect( hDC, &rc, hBrush );
  84. }
  85. }
  86. static int CALLBACK ChangeDirectoryCallback( HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData )
  87. {
  88. if (uMsg == BFFM_INITIALIZED)
  89. {
  90. SendMessage( hWnd, BFFM_SETSELECTION, 1, (LPARAM)lpData );
  91. }
  92. return 0;
  93. }
  94. bool ScreenSaverUtil::SelectDirectory( HWND hWnd, LPCTSTR pszPrompt, TCHAR szDirectory[] )
  95. {
  96. bool bResult = false;
  97. LPMALLOC pMalloc;
  98. HRESULT hr = SHGetMalloc(&pMalloc);
  99. if (SUCCEEDED(hr))
  100. {
  101. TCHAR szDisplayName[MAX_PATH];
  102. BROWSEINFO BrowseInfo;
  103. ::ZeroMemory( &BrowseInfo, sizeof(BrowseInfo) );
  104. BrowseInfo.hwndOwner = hWnd;
  105. BrowseInfo.pszDisplayName = szDisplayName;
  106. BrowseInfo.lpszTitle = pszPrompt;
  107. BrowseInfo.ulFlags = BIF_RETURNONLYFSDIRS;
  108. BrowseInfo.lpfn = ChangeDirectoryCallback;
  109. BrowseInfo.lParam = (LPARAM)szDirectory;
  110. BrowseInfo.iImage = 0;
  111. LPITEMIDLIST pidl = SHBrowseForFolder(&BrowseInfo);
  112. if (pidl != NULL)
  113. {
  114. TCHAR szResult[MAX_PATH];
  115. if (SHGetPathFromIDList(pidl,szResult))
  116. {
  117. lstrcpy( szDirectory, szResult );
  118. bResult = true;
  119. }
  120. pMalloc->Free(pidl);
  121. }
  122. pMalloc->Release();
  123. }
  124. return bResult;
  125. }
  126. HPALETTE ScreenSaverUtil::SelectPalette( HDC hDC, HPALETTE hPalette, BOOL bForceBackground )
  127. {
  128. HPALETTE hOldPalette = NULL;
  129. if (hDC && hPalette)
  130. {
  131. hOldPalette = ::SelectPalette( hDC, hPalette, bForceBackground );
  132. RealizePalette( hDC );
  133. SetBrushOrgEx( hDC, 0,0, NULL );
  134. }
  135. return hOldPalette;
  136. }