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.

311 lines
10 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: image.cpp
  4. //
  5. // Module: CMUTIL.DLL
  6. //
  7. // Synopsis: Common image loading routines
  8. //
  9. // Copyright (c) 1997-1999 Microsoft Corporation
  10. //
  11. // Author: nickball Created 03/30/98
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Function: CmLoadImageA
  18. //
  19. // Synopsis: ANSI Wrapper for LoadImage API which loads a resource based upon
  20. // pszSpec which can be any of 3 formats:
  21. //
  22. // 1) Filename
  23. // 2) Resource ID name
  24. //
  25. // Arguments: hMainInst - Our application instance handle
  26. // pszSpec - The name of the resource
  27. // nResType - The resource type
  28. // nCX - Resource X dimension (ie. 32 X 32 icon)
  29. // nCY - Resource Y dimension (ie. 32 X 32 icon)
  30. //
  31. // Notes: Now includes hInst of main app for portability, due to different OS
  32. // implementations of GetModuleHandle, the 16-bit compilation would grab
  33. // default icons (ie. Question Mark) from the system dll.
  34. //
  35. // Returns: TRUE on Success
  36. //
  37. // History: a-nichb Re-Written 03/21/97
  38. // quintinb Implemented Wide/ANSI forms 04/08/99
  39. // sumitc cleanup 03/14/2000
  40. //
  41. //----------------------------------------------------------------------------
  42. HANDLE CmLoadImageA(HINSTANCE hMainInst, LPCSTR pszSpec, UINT nResType, UINT nCX, UINT nCY)
  43. {
  44. HANDLE hRes = NULL;
  45. // Ensure that the resource is one we can handle
  46. MYDBGASSERT(nResType == IMAGE_BITMAP || nResType == IMAGE_ICON);
  47. // enforce that icons can only be 16x16 or 32x32.
  48. MYDBGASSERT(nResType != IMAGE_ICON ||
  49. ((GetSystemMetrics(SM_CXICON) == (int) nCX && GetSystemMetrics(SM_CYICON) == (int) nCY)) ||
  50. (GetSystemMetrics(SM_CXSMICON) == (int) nCX && GetSystemMetrics(SM_CYSMICON) == (int) nCY));
  51. if (NULL == pszSpec)
  52. {
  53. return NULL;
  54. }
  55. DWORD dwFlags = 0;
  56. if (HIWORD(PtrToUlong(pszSpec)))
  57. {
  58. if (NULL == *pszSpec)
  59. {
  60. return NULL;
  61. }
  62. CMASSERTMSG(NULL == CmStrchrA(pszSpec, ','), TEXT("dll,id syntax no longer supported "));
  63. // If the HIWORD is empty, it's a resource ID, else it is a string.
  64. dwFlags |= LR_LOADFROMFILE;
  65. }
  66. if (nResType == IMAGE_BITMAP)
  67. {
  68. dwFlags |= LR_CREATEDIBSECTION;
  69. }
  70. // Apparently, this is intended to cause the low-order word of the
  71. // name to used as an OEM image identifier by LoadImage on Win95.
  72. HINSTANCE hInstTmp = (dwFlags & LR_LOADFROMFILE) ? NULL : hMainInst;
  73. hRes = LoadImageA(hInstTmp, pszSpec, nResType, nCX, nCY, (UINT) dwFlags);
  74. #ifdef DEBUG
  75. if (!hRes)
  76. {
  77. if (dwFlags & LR_LOADFROMFILE)
  78. {
  79. CMTRACE3A("LoadImage(hInst=0x%x, pszSpec=%S, dwFlags|dwImageFlags=0x%x) failed.", hInstTmp, pszSpec, dwFlags);
  80. }
  81. else
  82. {
  83. CMTRACE3A("LoadImage(hInst=0x%x, pszSpec=0x%x, dwFlags|dwImageFlags=0x%x) failed.", hInstTmp, pszSpec, dwFlags);
  84. }
  85. }
  86. #endif
  87. return hRes;
  88. }
  89. //+---------------------------------------------------------------------------
  90. //
  91. // Function: CmLoadImageW
  92. //
  93. // Synopsis: Wide Wrapper for LoadImage API which loads a resource based upon
  94. // pszSpec which can be any of 2 formats:
  95. //
  96. // 1) Filename
  97. // 2) Resource ID name
  98. //
  99. // Arguments: hMainInst - Our application instance handle
  100. // pszSpec - The name of the resource
  101. // nResType - The resource type
  102. // nCX - Resource X dimension (ie. 32 X 32 icon)
  103. // nCY - Resource Y dimension (ie. 32 X 32 icon)
  104. //
  105. // Notes: Now includes hInst of main app for portability, due to different OS
  106. // implementations of GetModuleHandle, the 16-bit compilation would grab
  107. // default icons (ie. Question Mark) from the system dll.
  108. //
  109. // Returns: TRUE on Success
  110. //
  111. // History: a-nichb Re-Written 03/21/1997
  112. // quintinb Implemented Wide/ANSI forms 04/08/1999
  113. // sumitc cleanup 03/14/2000
  114. //
  115. //----------------------------------------------------------------------------
  116. HANDLE CmLoadImageW(HINSTANCE hMainInst, LPCWSTR pszSpec, UINT nResType, UINT nCX, UINT nCY)
  117. {
  118. HANDLE hRes = NULL;
  119. // Ensure that the resource is one we can handle
  120. MYDBGASSERT(nResType == IMAGE_BITMAP || nResType == IMAGE_ICON);
  121. // enforce that icons can only be 16x16 or 32x32.
  122. MYDBGASSERT(nResType != IMAGE_ICON ||
  123. ((GetSystemMetrics(SM_CXICON) == (int) nCX && GetSystemMetrics(SM_CYICON) == (int) nCY)) ||
  124. (GetSystemMetrics(SM_CXSMICON) == (int) nCX && GetSystemMetrics(SM_CYSMICON) == (int) nCY));
  125. if (NULL == pszSpec)
  126. {
  127. return NULL;
  128. }
  129. DWORD dwFlags = 0;
  130. if (HIWORD(PtrToUlong(pszSpec)))
  131. {
  132. if (NULL == *pszSpec)
  133. {
  134. return NULL;
  135. }
  136. CMASSERTMSG(NULL == CmStrchrW(pszSpec, L','), TEXT("dll,id syntax no longer supported "));
  137. // If the HIWORD is empty, it's a resource ID, else it is a string.
  138. dwFlags |= LR_LOADFROMFILE;
  139. }
  140. if (nResType == IMAGE_BITMAP)
  141. {
  142. dwFlags |= LR_CREATEDIBSECTION;
  143. }
  144. // Apparently, this is intended to cause the low-order word of the
  145. // name to used as an OEM image identifier by LoadImage on Win95.
  146. HINSTANCE hInstTmp = (dwFlags & LR_LOADFROMFILE) ? NULL : hMainInst;
  147. hRes = LoadImageU(hInstTmp, pszSpec, nResType, nCX, nCY, (UINT) dwFlags);
  148. #ifdef DEBUG
  149. if (!hRes)
  150. {
  151. if (dwFlags & LR_LOADFROMFILE)
  152. {
  153. CMTRACE3W(L"LoadImage(hInst=0x%x, pszSpec=%s, dwFlags|dwImageFlags=0x%x) failed.", hInstTmp, pszSpec, dwFlags);
  154. }
  155. else
  156. {
  157. CMTRACE3W(L"LoadImage(hInst=0x%x, pszSpec=0x%x, dwFlags|dwImageFlags=0x%x) failed.", hInstTmp, pszSpec, dwFlags);
  158. }
  159. }
  160. #endif
  161. return hRes;
  162. }
  163. //+---------------------------------------------------------------------------
  164. //
  165. // Function: CmLoadIconA
  166. //
  167. // Synopsis: This function loads a large icon from the given file path or
  168. // the given instance handle and resource ID.
  169. //
  170. // Arguments: HINSTANCE hInst - Instance Handle
  171. // LPCSTR pszSpec - either filename path or a resource ID, see
  172. // CmLoadImage for details.
  173. //
  174. // Returns: HICON - Handle to an Icon on Success, NULL on Failure
  175. //
  176. // History: quintinb Created Header 01/13/2000
  177. //
  178. //----------------------------------------------------------------------------
  179. HICON CmLoadIconA(HINSTANCE hInst, LPCSTR pszSpec)
  180. {
  181. return ((HICON) CmLoadImageA(hInst,
  182. pszSpec,
  183. IMAGE_ICON,
  184. GetSystemMetrics(SM_CXICON),
  185. GetSystemMetrics(SM_CYICON)));
  186. }
  187. //+---------------------------------------------------------------------------
  188. //
  189. // Function: CmLoadIconW
  190. //
  191. // Synopsis: This function loads a large icon from the given file path or
  192. // the given instance handle and resource ID.
  193. //
  194. // Arguments: HINSTANCE hInst - Instance Handle
  195. // LPCWSTR pszSpec - either filename path or a resource ID, see
  196. // CmLoadImage for details.
  197. //
  198. // Returns: HICON - Handle to an Icon on Success, NULL on Failure
  199. //
  200. // History: quintinb Created Header 01/13/2000
  201. //
  202. //----------------------------------------------------------------------------
  203. HICON CmLoadIconW(HINSTANCE hInst, LPCWSTR pszSpec)
  204. {
  205. return ((HICON) CmLoadImageW(hInst,
  206. pszSpec,
  207. IMAGE_ICON,
  208. GetSystemMetrics(SM_CXICON),
  209. GetSystemMetrics(SM_CYICON)));
  210. }
  211. //+---------------------------------------------------------------------------
  212. //
  213. // Function: CmLoadSmallIconA
  214. //
  215. // Synopsis: This function loads a small icon from the given file path or
  216. // the given instance handle and resource ID.
  217. //
  218. // Arguments: HINSTANCE hInst - Instance Handle
  219. // LPCWSTR pszSpec - either filename path or a resource ID, see
  220. // CmLoadImage for details.
  221. //
  222. // Returns: HICON - Handle to an Icon on Success, NULL on Failure
  223. //
  224. // History: quintinb Created Header 01/13/2000
  225. //
  226. //----------------------------------------------------------------------------
  227. HICON CmLoadSmallIconA(HINSTANCE hInst, LPCSTR pszSpec)
  228. {
  229. HICON hRes = NULL;
  230. hRes = (HICON) CmLoadImageA(hInst,
  231. pszSpec,
  232. IMAGE_ICON,
  233. GetSystemMetrics(SM_CXSMICON),
  234. GetSystemMetrics(SM_CYSMICON));
  235. if (!hRes)
  236. {
  237. hRes = CmLoadIconA(hInst, pszSpec);
  238. }
  239. return hRes;
  240. }
  241. //+---------------------------------------------------------------------------
  242. //
  243. // Function: CmLoadSmallIconW
  244. //
  245. // Synopsis: This function loads a small icon from the given file path or
  246. // the given instance handle and resource ID.
  247. //
  248. // Arguments: HINSTANCE hInst - Instance Handle
  249. // LPCWSTR pszSpec - either filename path or a resource ID, see
  250. // CmLoadImage for details.
  251. //
  252. // Returns: HICON - Handle to an Icon on Success, NULL on Failure
  253. //
  254. // History: quintinb Created Header 01/13/2000
  255. //
  256. //----------------------------------------------------------------------------
  257. HICON CmLoadSmallIconW(HINSTANCE hInst, LPCWSTR pszSpec)
  258. {
  259. HICON hRes = NULL;
  260. hRes = (HICON) CmLoadImageW(hInst,
  261. pszSpec,
  262. IMAGE_ICON,
  263. GetSystemMetrics(SM_CXSMICON),
  264. GetSystemMetrics(SM_CYSMICON));
  265. if (!hRes)
  266. {
  267. hRes = CmLoadIconW(hInst, pszSpec);
  268. }
  269. return hRes;
  270. }