atluser.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. // Windows Template Library - WTL version 10.0
  2. // Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
  3. //
  4. // This file is a part of the Windows Template Library.
  5. // The use and distribution terms for this software are covered by the
  6. // Microsoft Public License (http://opensource.org/licenses/MS-PL)
  7. // which can be found in the file MS-PL.txt at the root folder.
  8. #ifndef __ATLUSER_H__
  9. #define __ATLUSER_H__
  10. #pragma once
  11. #ifndef __ATLAPP_H__
  12. #error atluser.h requires atlapp.h to be included first
  13. #endif
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // Classes in this file:
  16. //
  17. // CMenuItemInfo
  18. // CMenuT<t_bManaged>
  19. // CAcceleratorT<t_bManaged>
  20. // CIconT<t_bManaged>
  21. // CCursorT<t_bManaged>
  22. // CResource
  23. //
  24. // Global functions:
  25. // AtlMessageBox()
  26. //
  27. // AtlLoadAccelerators()
  28. // AtlLoadMenu()
  29. // AtlLoadBitmap()
  30. // AtlLoadSysBitmap()
  31. // AtlLoadCursor()
  32. // AtlLoadSysCursor()
  33. // AtlLoadIcon()
  34. // AtlLoadSysIcon()
  35. // AtlLoadBitmapImage()
  36. // AtlLoadCursorImage()
  37. // AtlLoadIconImage()
  38. // AtlLoadSysBitmapImage()
  39. // AtlLoadSysCursorImage()
  40. // AtlLoadSysIconImage()
  41. // AtlLoadString()
  42. namespace WTL
  43. {
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // AtlMessageBox - accepts both memory and resource based strings
  46. inline int AtlMessageBox(HWND hWndOwner, ATL::_U_STRINGorID message, ATL::_U_STRINGorID title = (LPCTSTR)NULL, UINT uType = MB_OK | MB_ICONINFORMATION)
  47. {
  48. ATLASSERT((hWndOwner == NULL) || ::IsWindow(hWndOwner));
  49. LPTSTR lpstrMessage = NULL;
  50. if(IS_INTRESOURCE(message.m_lpstr))
  51. {
  52. for(int nLen = 256; ; nLen *= 2)
  53. {
  54. ATLTRY(lpstrMessage = new TCHAR[nLen]);
  55. if(lpstrMessage == NULL)
  56. {
  57. ATLASSERT(FALSE);
  58. return 0;
  59. }
  60. int nRes = ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(message.m_lpstr), lpstrMessage, nLen);
  61. if(nRes < nLen - 1)
  62. break;
  63. delete [] lpstrMessage;
  64. lpstrMessage = NULL;
  65. }
  66. message.m_lpstr = lpstrMessage;
  67. }
  68. LPTSTR lpstrTitle = NULL;
  69. if(IS_INTRESOURCE(title.m_lpstr) && (LOWORD(title.m_lpstr) != 0))
  70. {
  71. for(int nLen = 256; ; nLen *= 2)
  72. {
  73. ATLTRY(lpstrTitle = new TCHAR[nLen]);
  74. if(lpstrTitle == NULL)
  75. {
  76. ATLASSERT(FALSE);
  77. return 0;
  78. }
  79. int nRes = ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(title.m_lpstr), lpstrTitle, nLen);
  80. if(nRes < nLen - 1)
  81. break;
  82. delete [] lpstrTitle;
  83. lpstrTitle = NULL;
  84. }
  85. title.m_lpstr = lpstrTitle;
  86. }
  87. int nRet = ::MessageBox(hWndOwner, message.m_lpstr, title.m_lpstr, uType);
  88. delete [] lpstrMessage;
  89. delete [] lpstrTitle;
  90. return nRet;
  91. }
  92. ///////////////////////////////////////////////////////////////////////////////
  93. // CMenu
  94. class CMenuItemInfo : public MENUITEMINFO
  95. {
  96. public:
  97. CMenuItemInfo()
  98. {
  99. memset(this, 0, sizeof(MENUITEMINFO));
  100. cbSize = sizeof(MENUITEMINFO);
  101. }
  102. };
  103. // forward declarations
  104. template <bool t_bManaged> class CMenuT;
  105. typedef CMenuT<false> CMenuHandle;
  106. typedef CMenuT<true> CMenu;
  107. template <bool t_bManaged>
  108. class CMenuT
  109. {
  110. public:
  111. // Data members
  112. HMENU m_hMenu;
  113. // Constructor/destructor/operators
  114. CMenuT(HMENU hMenu = NULL) : m_hMenu(hMenu)
  115. { }
  116. ~CMenuT()
  117. {
  118. if(t_bManaged && (m_hMenu != NULL))
  119. DestroyMenu();
  120. }
  121. CMenuT<t_bManaged>& operator =(HMENU hMenu)
  122. {
  123. Attach(hMenu);
  124. return *this;
  125. }
  126. void Attach(HMENU hMenuNew)
  127. {
  128. ATLASSERT(::IsMenu(hMenuNew));
  129. if(t_bManaged && (m_hMenu != NULL) && (m_hMenu != hMenuNew))
  130. ::DestroyMenu(m_hMenu);
  131. m_hMenu = hMenuNew;
  132. }
  133. HMENU Detach()
  134. {
  135. HMENU hMenu = m_hMenu;
  136. m_hMenu = NULL;
  137. return hMenu;
  138. }
  139. operator HMENU() const { return m_hMenu; }
  140. bool IsNull() const { return (m_hMenu == NULL); }
  141. BOOL IsMenu() const
  142. {
  143. return ::IsMenu(m_hMenu);
  144. }
  145. // Create/destroy methods
  146. BOOL CreateMenu()
  147. {
  148. ATLASSERT(m_hMenu == NULL);
  149. m_hMenu = ::CreateMenu();
  150. return (m_hMenu != NULL) ? TRUE : FALSE;
  151. }
  152. BOOL CreatePopupMenu()
  153. {
  154. ATLASSERT(m_hMenu == NULL);
  155. m_hMenu = ::CreatePopupMenu();
  156. return (m_hMenu != NULL) ? TRUE : FALSE;
  157. }
  158. BOOL LoadMenu(ATL::_U_STRINGorID menu)
  159. {
  160. ATLASSERT(m_hMenu == NULL);
  161. m_hMenu = ::LoadMenu(ModuleHelper::GetResourceInstance(), menu.m_lpstr);
  162. return (m_hMenu != NULL) ? TRUE : FALSE;
  163. }
  164. BOOL LoadMenuIndirect(const void* lpMenuTemplate)
  165. {
  166. ATLASSERT(m_hMenu == NULL);
  167. m_hMenu = ::LoadMenuIndirect(lpMenuTemplate);
  168. return (m_hMenu != NULL) ? TRUE : FALSE;
  169. }
  170. BOOL DestroyMenu()
  171. {
  172. if (m_hMenu == NULL)
  173. return FALSE;
  174. BOOL bRet = ::DestroyMenu(m_hMenu);
  175. if(bRet)
  176. m_hMenu = NULL;
  177. return bRet;
  178. }
  179. // Menu Operations
  180. BOOL DeleteMenu(UINT nPosition, UINT nFlags)
  181. {
  182. ATLASSERT(::IsMenu(m_hMenu));
  183. return ::DeleteMenu(m_hMenu, nPosition, nFlags);
  184. }
  185. BOOL TrackPopupMenu(UINT nFlags, int x, int y, HWND hWnd, LPCRECT lpRect = NULL)
  186. {
  187. ATLASSERT(::IsMenu(m_hMenu));
  188. x = _FixTrackMenuPopupX(x, y);
  189. return ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0, hWnd, lpRect);
  190. }
  191. BOOL TrackPopupMenuEx(UINT uFlags, int x, int y, HWND hWnd, LPTPMPARAMS lptpm = NULL)
  192. {
  193. ATLASSERT(::IsMenu(m_hMenu));
  194. x = _FixTrackMenuPopupX(x, y);
  195. return ::TrackPopupMenuEx(m_hMenu, uFlags, x, y, hWnd, lptpm);
  196. }
  197. // helper that fixes popup menu X position when it's off-screen
  198. static int _FixTrackMenuPopupX(int x, int y)
  199. {
  200. POINT pt = { x, y };
  201. HMONITOR hMonitor = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
  202. if(hMonitor == NULL)
  203. {
  204. HMONITOR hMonitorNear = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
  205. if(hMonitorNear != NULL)
  206. {
  207. MONITORINFO mi = { sizeof(MONITORINFO) };
  208. if(::GetMonitorInfo(hMonitorNear, &mi) != FALSE)
  209. {
  210. if(x < mi.rcWork.left)
  211. x = mi.rcWork.left;
  212. else if(x > mi.rcWork.right)
  213. x = mi.rcWork.right;
  214. }
  215. }
  216. }
  217. return x;
  218. }
  219. BOOL GetMenuInfo(LPMENUINFO lpMenuInfo) const
  220. {
  221. ATLASSERT(::IsMenu(m_hMenu));
  222. return ::GetMenuInfo(m_hMenu, lpMenuInfo);
  223. }
  224. BOOL SetMenuInfo(LPCMENUINFO lpMenuInfo)
  225. {
  226. ATLASSERT(::IsMenu(m_hMenu));
  227. return ::SetMenuInfo(m_hMenu, lpMenuInfo);
  228. }
  229. // Menu Item Operations
  230. BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  231. {
  232. ATLASSERT(::IsMenu(m_hMenu));
  233. return ::AppendMenu(m_hMenu, nFlags, nIDNewItem, lpszNewItem);
  234. }
  235. BOOL AppendMenu(UINT nFlags, HMENU hSubMenu, LPCTSTR lpszNewItem)
  236. {
  237. ATLASSERT(::IsMenu(m_hMenu));
  238. ATLASSERT(::IsMenu(hSubMenu));
  239. return ::AppendMenu(m_hMenu, nFlags | MF_POPUP, (UINT_PTR)hSubMenu, lpszNewItem);
  240. }
  241. BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  242. {
  243. ATLASSERT(::IsMenu(m_hMenu));
  244. return ::AppendMenu(m_hMenu, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  245. }
  246. BOOL AppendMenu(UINT nFlags, HMENU hSubMenu, HBITMAP hBmp)
  247. {
  248. ATLASSERT(::IsMenu(m_hMenu));
  249. ATLASSERT(::IsMenu(hSubMenu));
  250. return ::AppendMenu(m_hMenu, nFlags | (MF_BITMAP | MF_POPUP), (UINT_PTR)hSubMenu, (LPCTSTR)hBmp);
  251. }
  252. UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck)
  253. {
  254. ATLASSERT(::IsMenu(m_hMenu));
  255. return (UINT)::CheckMenuItem(m_hMenu, nIDCheckItem, nCheck);
  256. }
  257. UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable)
  258. {
  259. ATLASSERT(::IsMenu(m_hMenu));
  260. return ::EnableMenuItem(m_hMenu, nIDEnableItem, nEnable);
  261. }
  262. BOOL HiliteMenuItem(HWND hWnd, UINT uIDHiliteItem, UINT uHilite)
  263. {
  264. ATLASSERT(::IsMenu(m_hMenu));
  265. return ::HiliteMenuItem(hWnd, m_hMenu, uIDHiliteItem, uHilite);
  266. }
  267. int GetMenuItemCount() const
  268. {
  269. ATLASSERT(::IsMenu(m_hMenu));
  270. return ::GetMenuItemCount(m_hMenu);
  271. }
  272. UINT GetMenuItemID(int nPos) const
  273. {
  274. ATLASSERT(::IsMenu(m_hMenu));
  275. return ::GetMenuItemID(m_hMenu, nPos);
  276. }
  277. UINT GetMenuState(UINT nID, UINT nFlags) const
  278. {
  279. ATLASSERT(::IsMenu(m_hMenu));
  280. return ::GetMenuState(m_hMenu, nID, nFlags);
  281. }
  282. int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount, UINT nFlags) const
  283. {
  284. ATLASSERT(::IsMenu(m_hMenu));
  285. return ::GetMenuString(m_hMenu, nIDItem, lpString, nMaxCount, nFlags);
  286. }
  287. int GetMenuStringLen(UINT nIDItem, UINT nFlags) const
  288. {
  289. ATLASSERT(::IsMenu(m_hMenu));
  290. return ::GetMenuString(m_hMenu, nIDItem, NULL, 0, nFlags);
  291. }
  292. BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
  293. {
  294. USES_CONVERSION;
  295. ATLASSERT(::IsMenu(m_hMenu));
  296. ATLASSERT(bstrText == NULL);
  297. int nLen = GetMenuStringLen(nIDItem, nFlags);
  298. if(nLen == 0)
  299. {
  300. bstrText = ::SysAllocString(OLESTR(""));
  301. return (bstrText != NULL) ? TRUE : FALSE;
  302. }
  303. nLen++; // increment to include terminating NULL char
  304. ATL::CTempBuffer<TCHAR, _WTL_STACK_ALLOC_THRESHOLD> buff;
  305. LPTSTR lpszText = buff.Allocate(nLen);
  306. if(lpszText == NULL)
  307. return FALSE;
  308. if(!GetMenuString(nIDItem, lpszText, nLen, nFlags))
  309. return FALSE;
  310. bstrText = ::SysAllocString(T2OLE(lpszText));
  311. return (bstrText != NULL) ? TRUE : FALSE;
  312. }
  313. #ifdef __ATLSTR_H__
  314. int GetMenuString(UINT nIDItem, ATL::CString& strText, UINT nFlags) const
  315. {
  316. ATLASSERT(::IsMenu(m_hMenu));
  317. int nLen = GetMenuStringLen(nIDItem, nFlags);
  318. if(nLen == 0)
  319. return 0;
  320. nLen++; // increment to include terminating NULL char
  321. LPTSTR lpstr = strText.GetBufferSetLength(nLen);
  322. if(lpstr == NULL)
  323. return 0;
  324. int nRet = GetMenuString(nIDItem, lpstr, nLen, nFlags);
  325. strText.ReleaseBuffer();
  326. return nRet;
  327. }
  328. #endif // __ATLSTR_H__
  329. CMenuHandle GetSubMenu(int nPos) const
  330. {
  331. ATLASSERT(::IsMenu(m_hMenu));
  332. return CMenuHandle(::GetSubMenu(m_hMenu, nPos));
  333. }
  334. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  335. {
  336. ATLASSERT(::IsMenu(m_hMenu));
  337. return ::InsertMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  338. }
  339. BOOL InsertMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, LPCTSTR lpszNewItem)
  340. {
  341. ATLASSERT(::IsMenu(m_hMenu));
  342. ATLASSERT(::IsMenu(hSubMenu));
  343. return ::InsertMenu(m_hMenu, nPosition, nFlags | MF_POPUP, (UINT_PTR)hSubMenu, lpszNewItem);
  344. }
  345. BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  346. {
  347. ATLASSERT(::IsMenu(m_hMenu));
  348. return ::InsertMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  349. }
  350. BOOL InsertMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, HBITMAP hBmp)
  351. {
  352. ATLASSERT(::IsMenu(m_hMenu));
  353. ATLASSERT(::IsMenu(hSubMenu));
  354. return ::InsertMenu(m_hMenu, nPosition, nFlags | (MF_BITMAP | MF_POPUP), (UINT_PTR)hSubMenu, (LPCTSTR)hBmp);
  355. }
  356. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
  357. {
  358. ATLASSERT(::IsMenu(m_hMenu));
  359. return ::ModifyMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
  360. }
  361. BOOL ModifyMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, LPCTSTR lpszNewItem)
  362. {
  363. ATLASSERT(::IsMenu(m_hMenu));
  364. ATLASSERT(::IsMenu(hSubMenu));
  365. return ::ModifyMenu(m_hMenu, nPosition, nFlags | MF_POPUP, (UINT_PTR)hSubMenu, lpszNewItem);
  366. }
  367. BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
  368. {
  369. ATLASSERT(::IsMenu(m_hMenu));
  370. return ::ModifyMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
  371. }
  372. BOOL ModifyMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, HBITMAP hBmp)
  373. {
  374. ATLASSERT(::IsMenu(m_hMenu));
  375. ATLASSERT(::IsMenu(hSubMenu));
  376. return ::ModifyMenu(m_hMenu, nPosition, nFlags | (MF_BITMAP | MF_POPUP), (UINT_PTR)hSubMenu, (LPCTSTR)hBmp);
  377. }
  378. BOOL RemoveMenu(UINT nPosition, UINT nFlags)
  379. {
  380. ATLASSERT(::IsMenu(m_hMenu));
  381. return ::RemoveMenu(m_hMenu, nPosition, nFlags);
  382. }
  383. BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags, HBITMAP hBmpUnchecked, HBITMAP hBmpChecked)
  384. {
  385. ATLASSERT(::IsMenu(m_hMenu));
  386. return ::SetMenuItemBitmaps(m_hMenu, nPosition, nFlags, hBmpUnchecked, hBmpChecked);
  387. }
  388. BOOL CheckMenuRadioItem(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags)
  389. {
  390. ATLASSERT(::IsMenu(m_hMenu));
  391. return ::CheckMenuRadioItem(m_hMenu, nIDFirst, nIDLast, nIDItem, nFlags);
  392. }
  393. BOOL GetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii) const
  394. {
  395. ATLASSERT(::IsMenu(m_hMenu));
  396. return (BOOL)::GetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  397. }
  398. BOOL SetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  399. {
  400. ATLASSERT(::IsMenu(m_hMenu));
  401. return (BOOL)::SetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
  402. }
  403. BOOL InsertMenuItem(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
  404. {
  405. ATLASSERT(::IsMenu(m_hMenu));
  406. return (BOOL)::InsertMenuItem(m_hMenu, uItem, bByPosition, lpmii);
  407. }
  408. UINT GetMenuDefaultItem(BOOL bByPosition = FALSE, UINT uFlags = 0U) const
  409. {
  410. ATLASSERT(::IsMenu(m_hMenu));
  411. return ::GetMenuDefaultItem(m_hMenu, (UINT)bByPosition, uFlags);
  412. }
  413. BOOL SetMenuDefaultItem(UINT uItem = (UINT)-1, BOOL bByPosition = FALSE)
  414. {
  415. ATLASSERT(::IsMenu(m_hMenu));
  416. return ::SetMenuDefaultItem(m_hMenu, uItem, (UINT)bByPosition);
  417. }
  418. BOOL GetMenuItemRect(HWND hWnd, UINT uItem, LPRECT lprcItem) const
  419. {
  420. ATLASSERT(::IsMenu(m_hMenu));
  421. return ::GetMenuItemRect(hWnd, m_hMenu, uItem, lprcItem);
  422. }
  423. int MenuItemFromPoint(HWND hWnd, POINT point) const
  424. {
  425. ATLASSERT(::IsMenu(m_hMenu));
  426. return ::MenuItemFromPoint(hWnd, m_hMenu, point);
  427. }
  428. // Context Help Functions
  429. BOOL SetMenuContextHelpId(DWORD dwContextHelpId)
  430. {
  431. ATLASSERT(::IsMenu(m_hMenu));
  432. return ::SetMenuContextHelpId(m_hMenu, dwContextHelpId);
  433. }
  434. DWORD GetMenuContextHelpId() const
  435. {
  436. ATLASSERT(::IsMenu(m_hMenu));
  437. return ::GetMenuContextHelpId(m_hMenu);
  438. }
  439. };
  440. ///////////////////////////////////////////////////////////////////////////////
  441. // CAccelerator
  442. template <bool t_bManaged>
  443. class CAcceleratorT
  444. {
  445. public:
  446. HACCEL m_hAccel;
  447. // Constructor/destructor/operators
  448. CAcceleratorT(HACCEL hAccel = NULL) : m_hAccel(hAccel)
  449. { }
  450. ~CAcceleratorT()
  451. {
  452. if(t_bManaged && (m_hAccel != NULL))
  453. ::DestroyAcceleratorTable(m_hAccel);
  454. }
  455. CAcceleratorT<t_bManaged>& operator =(HACCEL hAccel)
  456. {
  457. Attach(hAccel);
  458. return *this;
  459. }
  460. void Attach(HACCEL hAccel)
  461. {
  462. if(t_bManaged && (m_hAccel != NULL))
  463. ::DestroyAcceleratorTable(m_hAccel);
  464. m_hAccel = hAccel;
  465. }
  466. HACCEL Detach()
  467. {
  468. HACCEL hAccel = m_hAccel;
  469. m_hAccel = NULL;
  470. return hAccel;
  471. }
  472. operator HACCEL() const { return m_hAccel; }
  473. bool IsNull() const { return m_hAccel == NULL; }
  474. // Create/destroy methods
  475. HACCEL LoadAccelerators(ATL::_U_STRINGorID accel)
  476. {
  477. ATLASSERT(m_hAccel == NULL);
  478. m_hAccel = ::LoadAccelerators(ModuleHelper::GetResourceInstance(), accel.m_lpstr);
  479. return m_hAccel;
  480. }
  481. HACCEL CreateAcceleratorTable(LPACCEL pAccel, int cEntries)
  482. {
  483. ATLASSERT(m_hAccel == NULL);
  484. ATLASSERT(pAccel != NULL);
  485. m_hAccel = ::CreateAcceleratorTable(pAccel, cEntries);
  486. return m_hAccel;
  487. }
  488. void DestroyObject()
  489. {
  490. if(m_hAccel != NULL)
  491. {
  492. ::DestroyAcceleratorTable(m_hAccel);
  493. m_hAccel = NULL;
  494. }
  495. }
  496. // Operations
  497. int CopyAcceleratorTable(LPACCEL lpAccelDst, int cEntries)
  498. {
  499. ATLASSERT(m_hAccel != NULL);
  500. ATLASSERT(lpAccelDst != NULL);
  501. return ::CopyAcceleratorTable(m_hAccel, lpAccelDst, cEntries);
  502. }
  503. int GetEntriesCount() const
  504. {
  505. ATLASSERT(m_hAccel != NULL);
  506. return ::CopyAcceleratorTable(m_hAccel, NULL, 0);
  507. }
  508. BOOL TranslateAccelerator(HWND hWnd, LPMSG pMsg)
  509. {
  510. ATLASSERT(m_hAccel != NULL);
  511. ATLASSERT(::IsWindow(hWnd));
  512. ATLASSERT(pMsg != NULL);
  513. return ::TranslateAccelerator(hWnd, m_hAccel, pMsg);
  514. }
  515. };
  516. typedef CAcceleratorT<false> CAcceleratorHandle;
  517. typedef CAcceleratorT<true> CAccelerator;
  518. ///////////////////////////////////////////////////////////////////////////////
  519. // CIcon
  520. template <bool t_bManaged>
  521. class CIconT
  522. {
  523. public:
  524. HICON m_hIcon;
  525. // Constructor/destructor/operators
  526. CIconT(HICON hIcon = NULL) : m_hIcon(hIcon)
  527. { }
  528. ~CIconT()
  529. {
  530. if(t_bManaged && (m_hIcon != NULL))
  531. ::DestroyIcon(m_hIcon);
  532. }
  533. CIconT<t_bManaged>& operator =(HICON hIcon)
  534. {
  535. Attach(hIcon);
  536. return *this;
  537. }
  538. void Attach(HICON hIcon)
  539. {
  540. if(t_bManaged && (m_hIcon != NULL))
  541. ::DestroyIcon(m_hIcon);
  542. m_hIcon = hIcon;
  543. }
  544. HICON Detach()
  545. {
  546. HICON hIcon = m_hIcon;
  547. m_hIcon = NULL;
  548. return hIcon;
  549. }
  550. operator HICON() const { return m_hIcon; }
  551. bool IsNull() const { return m_hIcon == NULL; }
  552. // Create/destroy methods
  553. HICON LoadIcon(ATL::_U_STRINGorID icon)
  554. {
  555. ATLASSERT(m_hIcon == NULL);
  556. m_hIcon = ::LoadIcon(ModuleHelper::GetResourceInstance(), icon.m_lpstr);
  557. return m_hIcon;
  558. }
  559. HICON LoadIcon(ATL::_U_STRINGorID icon, int cxDesired, int cyDesired, UINT fuLoad = 0)
  560. {
  561. ATLASSERT(m_hIcon == NULL);
  562. m_hIcon = (HICON) ::LoadImage(ModuleHelper::GetResourceInstance(), icon.m_lpstr, IMAGE_ICON, cxDesired, cyDesired, fuLoad);
  563. return m_hIcon;
  564. }
  565. HICON LoadOEMIcon(LPCTSTR lpstrIconName)
  566. {
  567. ATLASSERT(m_hIcon == NULL);
  568. ATLASSERT(IsOEMIcon(lpstrIconName));
  569. m_hIcon = ::LoadIcon(NULL, lpstrIconName);
  570. return m_hIcon;
  571. }
  572. HICON CreateIcon(int nWidth, int nHeight, BYTE cPlanes, BYTE cBitsPixel, CONST BYTE* lpbANDbits, CONST BYTE *lpbXORbits)
  573. {
  574. ATLASSERT(m_hIcon == NULL);
  575. ATLASSERT(lpbANDbits != NULL);
  576. ATLASSERT(lpbXORbits != NULL);
  577. m_hIcon = ::CreateIcon(ModuleHelper::GetResourceInstance(), nWidth, nHeight, cPlanes, cBitsPixel, lpbANDbits, lpbXORbits);
  578. return m_hIcon;
  579. }
  580. HICON CreateIconFromResource(PBYTE pBits, DWORD dwResSize, DWORD dwVersion = 0x00030000)
  581. {
  582. ATLASSERT(m_hIcon == NULL);
  583. ATLASSERT(pBits != NULL);
  584. m_hIcon = ::CreateIconFromResource(pBits, dwResSize, TRUE, dwVersion);
  585. return m_hIcon;
  586. }
  587. HICON CreateIconFromResourceEx(PBYTE pbBits, DWORD cbBits, DWORD dwVersion = 0x00030000, int cxDesired = 0, int cyDesired = 0, UINT uFlags = LR_DEFAULTCOLOR)
  588. {
  589. ATLASSERT(m_hIcon == NULL);
  590. ATLASSERT(pbBits != NULL);
  591. ATLASSERT(cbBits > 0);
  592. m_hIcon = ::CreateIconFromResourceEx(pbBits, cbBits, TRUE, dwVersion, cxDesired, cyDesired, uFlags);
  593. return m_hIcon;
  594. }
  595. HICON CreateIconIndirect(PICONINFO pIconInfo)
  596. {
  597. ATLASSERT(m_hIcon == NULL);
  598. ATLASSERT(pIconInfo != NULL);
  599. m_hIcon = ::CreateIconIndirect(pIconInfo);
  600. return m_hIcon;
  601. }
  602. HICON ExtractIcon(LPCTSTR lpszExeFileName, UINT nIconIndex)
  603. {
  604. ATLASSERT(m_hIcon == NULL);
  605. ATLASSERT(lpszExeFileName != NULL);
  606. m_hIcon = ::ExtractIcon(ModuleHelper::GetModuleInstance(), lpszExeFileName, nIconIndex);
  607. return m_hIcon;
  608. }
  609. HICON ExtractAssociatedIcon(HINSTANCE hInst, LPTSTR lpIconPath, LPWORD lpiIcon)
  610. {
  611. ATLASSERT(m_hIcon == NULL);
  612. ATLASSERT(lpIconPath != NULL);
  613. ATLASSERT(lpiIcon != NULL);
  614. m_hIcon = ::ExtractAssociatedIcon(hInst, lpIconPath, lpiIcon);
  615. return m_hIcon;
  616. }
  617. BOOL DestroyIcon()
  618. {
  619. ATLASSERT(m_hIcon != NULL);
  620. BOOL bRet = ::DestroyIcon(m_hIcon);
  621. if(bRet != FALSE)
  622. m_hIcon = NULL;
  623. return bRet;
  624. }
  625. // Operations
  626. HICON CopyIcon()
  627. {
  628. ATLASSERT(m_hIcon != NULL);
  629. return ::CopyIcon(m_hIcon);
  630. }
  631. HICON DuplicateIcon()
  632. {
  633. ATLASSERT(m_hIcon != NULL);
  634. return ::DuplicateIcon(NULL, m_hIcon);
  635. }
  636. BOOL DrawIcon(HDC hDC, int x, int y)
  637. {
  638. ATLASSERT(m_hIcon != NULL);
  639. return ::DrawIcon(hDC, x, y, m_hIcon);
  640. }
  641. BOOL DrawIcon(HDC hDC, POINT pt)
  642. {
  643. ATLASSERT(m_hIcon != NULL);
  644. return ::DrawIcon(hDC, pt.x, pt.y, m_hIcon);
  645. }
  646. BOOL DrawIconEx(HDC hDC, int x, int y, int cxWidth, int cyWidth, UINT uStepIfAniCur = 0, HBRUSH hbrFlickerFreeDraw = NULL, UINT uFlags = DI_NORMAL)
  647. {
  648. ATLASSERT(m_hIcon != NULL);
  649. return ::DrawIconEx(hDC, x, y, m_hIcon, cxWidth, cyWidth, uStepIfAniCur, hbrFlickerFreeDraw, uFlags);
  650. }
  651. BOOL DrawIconEx(HDC hDC, POINT pt, SIZE size, UINT uStepIfAniCur = 0, HBRUSH hbrFlickerFreeDraw = NULL, UINT uFlags = DI_NORMAL)
  652. {
  653. ATLASSERT(m_hIcon != NULL);
  654. return ::DrawIconEx(hDC, pt.x, pt.y, m_hIcon, size.cx, size.cy, uStepIfAniCur, hbrFlickerFreeDraw, uFlags);
  655. }
  656. BOOL GetIconInfo(PICONINFO pIconInfo) const
  657. {
  658. ATLASSERT(m_hIcon != NULL);
  659. ATLASSERT(pIconInfo != NULL);
  660. return ::GetIconInfo(m_hIcon, pIconInfo);
  661. }
  662. #if (_WIN32_WINNT >= 0x0600)
  663. BOOL GetIconInfoEx(PICONINFOEX pIconInfo) const
  664. {
  665. ATLASSERT(m_hIcon != NULL);
  666. ATLASSERT(pIconInfo != NULL);
  667. return ::GetIconInfoEx(m_hIcon, pIconInfo);
  668. }
  669. #endif // (_WIN32_WINNT >= 0x0600)
  670. #if defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
  671. HRESULT LoadIconMetric(ATL::_U_STRINGorID icon, int lims)
  672. {
  673. ATLASSERT(m_hIcon == NULL);
  674. USES_CONVERSION;
  675. return ::LoadIconMetric(ModuleHelper::GetResourceInstance(), T2CW(icon.m_lpstr), lims, &m_hIcon);
  676. }
  677. HRESULT LoadIconWithScaleDown(ATL::_U_STRINGorID icon, int cx, int cy)
  678. {
  679. ATLASSERT(m_hIcon == NULL);
  680. USES_CONVERSION;
  681. return ::LoadIconWithScaleDown(ModuleHelper::GetResourceInstance(), T2CW(icon.m_lpstr), cx, cy, &m_hIcon);
  682. }
  683. HRESULT LoadOEMIconMetric(LPCTSTR lpstrIconName, int lims)
  684. {
  685. ATLASSERT(m_hIcon == NULL);
  686. ATLASSERT(IsOEMIcon(lpstrIconName));
  687. return ::LoadIconMetric(NULL, (LPCWSTR)lpstrIconName, lims, &m_hIcon);
  688. }
  689. HRESULT LoadOEMIconWithScaleDown(LPCTSTR lpstrIconName, int cx, int cy)
  690. {
  691. ATLASSERT(m_hIcon == NULL);
  692. ATLASSERT(IsOEMIcon(lpstrIconName));
  693. USES_CONVERSION;
  694. return ::LoadIconWithScaleDown(NULL, (LPCWSTR)lpstrIconName, cx, cy, &m_hIcon);
  695. }
  696. #endif // defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
  697. // Helper
  698. static bool IsOEMIcon(LPCTSTR lpstrIconName)
  699. {
  700. #if (WINVER >= 0x0600)
  701. return ((lpstrIconName == IDI_APPLICATION) || (lpstrIconName == IDI_ASTERISK) || (lpstrIconName == IDI_EXCLAMATION) ||
  702. (lpstrIconName == IDI_HAND) || (lpstrIconName == IDI_QUESTION) || (lpstrIconName == IDI_WINLOGO) ||
  703. (lpstrIconName == IDI_SHIELD));
  704. #else // !(WINVER >= 0x0600)
  705. return ((lpstrIconName == IDI_APPLICATION) || (lpstrIconName == IDI_ASTERISK) || (lpstrIconName == IDI_EXCLAMATION) ||
  706. (lpstrIconName == IDI_HAND) || (lpstrIconName == IDI_QUESTION) || (lpstrIconName == IDI_WINLOGO));
  707. #endif // !(WINVER >= 0x0600)
  708. }
  709. };
  710. typedef CIconT<false> CIconHandle;
  711. typedef CIconT<true> CIcon;
  712. ///////////////////////////////////////////////////////////////////////////////
  713. // CCursor
  714. // protect template member from a winuser.h macro
  715. #ifdef CopyCursor
  716. #undef CopyCursor
  717. #endif
  718. template <bool t_bManaged>
  719. class CCursorT
  720. {
  721. public:
  722. HCURSOR m_hCursor;
  723. // Constructor/destructor/operators
  724. CCursorT(HCURSOR hCursor = NULL) : m_hCursor(hCursor)
  725. { }
  726. ~CCursorT()
  727. {
  728. if(t_bManaged && (m_hCursor != NULL))
  729. DestroyCursor();
  730. }
  731. CCursorT<t_bManaged>& operator =(HCURSOR hCursor)
  732. {
  733. Attach(hCursor);
  734. return *this;
  735. }
  736. void Attach(HCURSOR hCursor)
  737. {
  738. if(t_bManaged && (m_hCursor != NULL))
  739. DestroyCursor();
  740. m_hCursor = hCursor;
  741. }
  742. HCURSOR Detach()
  743. {
  744. HCURSOR hCursor = m_hCursor;
  745. m_hCursor = NULL;
  746. return hCursor;
  747. }
  748. operator HCURSOR() const { return m_hCursor; }
  749. bool IsNull() const { return m_hCursor == NULL; }
  750. // Create/destroy methods
  751. HCURSOR LoadCursor(ATL::_U_STRINGorID cursor)
  752. {
  753. ATLASSERT(m_hCursor == NULL);
  754. m_hCursor = ::LoadCursor(ModuleHelper::GetResourceInstance(), cursor.m_lpstr);
  755. return m_hCursor;
  756. }
  757. HCURSOR LoadSysCursor(LPCTSTR lpstrCursorName)
  758. {
  759. ATLASSERT(m_hCursor == NULL);
  760. ATLASSERT((lpstrCursorName == IDC_ARROW) || (lpstrCursorName == IDC_IBEAM) || (lpstrCursorName == IDC_WAIT) ||
  761. (lpstrCursorName == IDC_CROSS) || (lpstrCursorName == IDC_UPARROW) || (lpstrCursorName == IDC_SIZE) ||
  762. (lpstrCursorName == IDC_ICON) || (lpstrCursorName == IDC_SIZENWSE) || (lpstrCursorName == IDC_SIZENESW) ||
  763. (lpstrCursorName == IDC_SIZEWE) || (lpstrCursorName == IDC_SIZENS) || (lpstrCursorName == IDC_SIZEALL) ||
  764. (lpstrCursorName == IDC_NO) || (lpstrCursorName == IDC_APPSTARTING) || (lpstrCursorName == IDC_HELP) ||
  765. (lpstrCursorName == IDC_HAND));
  766. m_hCursor = ::LoadCursor(NULL, lpstrCursorName);
  767. return m_hCursor;
  768. }
  769. // deprecated
  770. HCURSOR LoadOEMCursor(LPCTSTR lpstrCursorName)
  771. {
  772. return LoadSysCursor(lpstrCursorName);
  773. }
  774. HCURSOR LoadCursor(ATL::_U_STRINGorID cursor, int cxDesired, int cyDesired, UINT fuLoad = 0)
  775. {
  776. ATLASSERT(m_hCursor == NULL);
  777. m_hCursor = (HCURSOR) ::LoadImage(ModuleHelper::GetResourceInstance(), cursor.m_lpstr, IMAGE_CURSOR, cxDesired, cyDesired, fuLoad);
  778. return m_hCursor;
  779. }
  780. HCURSOR LoadCursorFromFile(LPCTSTR pstrFilename)
  781. {
  782. ATLASSERT(m_hCursor == NULL);
  783. ATLASSERT(pstrFilename != NULL);
  784. m_hCursor = ::LoadCursorFromFile(pstrFilename);
  785. return m_hCursor;
  786. }
  787. HCURSOR CreateCursor(int xHotSpot, int yHotSpot, int nWidth, int nHeight, CONST VOID *pvANDPlane, CONST VOID *pvXORPlane)
  788. {
  789. ATLASSERT(m_hCursor == NULL);
  790. m_hCursor = ::CreateCursor(ModuleHelper::GetResourceInstance(), xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane);
  791. return m_hCursor;
  792. }
  793. HCURSOR CreateCursorFromResource(PBYTE pBits, DWORD dwResSize, DWORD dwVersion = 0x00030000)
  794. {
  795. ATLASSERT(m_hCursor == NULL);
  796. ATLASSERT(pBits != NULL);
  797. m_hCursor = (HCURSOR)::CreateIconFromResource(pBits, dwResSize, FALSE, dwVersion);
  798. return m_hCursor;
  799. }
  800. HCURSOR CreateCursorFromResourceEx(PBYTE pbBits, DWORD cbBits, DWORD dwVersion = 0x00030000, int cxDesired = 0, int cyDesired = 0, UINT uFlags = LR_DEFAULTCOLOR)
  801. {
  802. ATLASSERT(m_hCursor == NULL);
  803. ATLASSERT(pbBits != NULL);
  804. ATLASSERT(cbBits > 0);
  805. m_hCursor = (HCURSOR)::CreateIconFromResourceEx(pbBits, cbBits, FALSE, dwVersion, cxDesired, cyDesired, uFlags);
  806. return m_hCursor;
  807. }
  808. BOOL DestroyCursor()
  809. {
  810. ATLASSERT(m_hCursor != NULL);
  811. BOOL bRet = ::DestroyCursor(m_hCursor);
  812. if(bRet != FALSE)
  813. m_hCursor = NULL;
  814. return bRet;
  815. }
  816. // Operations
  817. HCURSOR CopyCursor()
  818. {
  819. ATLASSERT(m_hCursor != NULL);
  820. return (HCURSOR)::CopyIcon((HICON)m_hCursor);
  821. }
  822. BOOL GetCursorInfo(LPCURSORINFO pCursorInfo)
  823. {
  824. ATLASSERT(m_hCursor != NULL);
  825. ATLASSERT(pCursorInfo != NULL);
  826. return ::GetCursorInfo(pCursorInfo);
  827. }
  828. };
  829. typedef CCursorT<false> CCursorHandle;
  830. typedef CCursorT<true> CCursor;
  831. ///////////////////////////////////////////////////////////////////////////////
  832. // CResource - Wraps a generic Windows resource.
  833. // Use it with custom resource types other than the
  834. // standard RT_CURSOR, RT_BITMAP, etc.
  835. class CResource
  836. {
  837. public:
  838. HGLOBAL m_hGlobal;
  839. HRSRC m_hResource;
  840. // Constructor/destructor
  841. CResource() : m_hGlobal(NULL), m_hResource(NULL)
  842. { }
  843. ~CResource()
  844. {
  845. Release();
  846. }
  847. // Load methods
  848. bool Load(ATL::_U_STRINGorID Type, ATL::_U_STRINGorID ID)
  849. {
  850. ATLASSERT(m_hResource == NULL);
  851. ATLASSERT(m_hGlobal == NULL);
  852. m_hResource = ::FindResource(ModuleHelper::GetResourceInstance(), ID.m_lpstr, Type.m_lpstr);
  853. if(m_hResource == NULL)
  854. return false;
  855. m_hGlobal = ::LoadResource(ModuleHelper::GetResourceInstance(), m_hResource);
  856. if(m_hGlobal == NULL)
  857. {
  858. m_hResource = NULL;
  859. return false;
  860. }
  861. return true;
  862. }
  863. bool LoadEx(ATL::_U_STRINGorID ID, ATL::_U_STRINGorID Type, WORD wLanguage)
  864. {
  865. ATLASSERT(m_hResource == NULL);
  866. ATLASSERT(m_hGlobal == NULL);
  867. m_hResource = ::FindResourceEx(ModuleHelper::GetResourceInstance(), Type.m_lpstr, ID.m_lpstr, wLanguage);
  868. if(m_hResource == NULL)
  869. return false;
  870. m_hGlobal = ::LoadResource(ModuleHelper::GetResourceInstance(), m_hResource);
  871. if(m_hGlobal == NULL)
  872. {
  873. m_hResource = NULL;
  874. return false;
  875. }
  876. return true;
  877. }
  878. // Misc. operations
  879. DWORD GetSize() const
  880. {
  881. ATLASSERT(m_hResource != NULL);
  882. return ::SizeofResource(ModuleHelper::GetResourceInstance(), m_hResource);
  883. }
  884. LPVOID Lock()
  885. {
  886. ATLASSERT(m_hResource != NULL);
  887. ATLASSERT(m_hGlobal != NULL);
  888. LPVOID pVoid = ::LockResource(m_hGlobal);
  889. ATLASSERT(pVoid != NULL);
  890. return pVoid;
  891. }
  892. void Release()
  893. {
  894. if(m_hGlobal != NULL)
  895. {
  896. FreeResource(m_hGlobal);
  897. m_hGlobal = NULL;
  898. m_hResource = NULL;
  899. }
  900. }
  901. };
  902. ///////////////////////////////////////////////////////////////////////////////
  903. // Toolbar resource descriptor
  904. struct _AtlToolBarData
  905. {
  906. WORD wVersion;
  907. WORD wWidth;
  908. WORD wHeight;
  909. WORD wItemCount;
  910. WORD* items()
  911. { return (WORD*)(this+1); }
  912. };
  913. ///////////////////////////////////////////////////////////////////////////////
  914. // Global functions for loading resources
  915. inline HACCEL AtlLoadAccelerators(ATL::_U_STRINGorID table)
  916. {
  917. return ::LoadAccelerators(ModuleHelper::GetResourceInstance(), table.m_lpstr);
  918. }
  919. inline HMENU AtlLoadMenu(ATL::_U_STRINGorID menu)
  920. {
  921. return ::LoadMenu(ModuleHelper::GetResourceInstance(), menu.m_lpstr);
  922. }
  923. inline HBITMAP AtlLoadBitmap(ATL::_U_STRINGorID bitmap)
  924. {
  925. return ::LoadBitmap(ModuleHelper::GetResourceInstance(), bitmap.m_lpstr);
  926. }
  927. #ifdef OEMRESOURCE
  928. inline HBITMAP AtlLoadSysBitmap(ATL::_U_STRINGorID bitmap)
  929. {
  930. #ifdef _DEBUG
  931. WORD wID = LOWORD(bitmap.m_lpstr);
  932. ATLASSERT((wID >= 32734) && (wID <= 32767));
  933. #endif // _DEBUG
  934. return ::LoadBitmap(NULL, bitmap.m_lpstr);
  935. }
  936. #endif // OEMRESOURCE
  937. inline HCURSOR AtlLoadCursor(ATL::_U_STRINGorID cursor)
  938. {
  939. return ::LoadCursor(ModuleHelper::GetResourceInstance(), cursor.m_lpstr);
  940. }
  941. inline HCURSOR AtlLoadSysCursor(LPCTSTR lpCursorName)
  942. {
  943. ATLASSERT((lpCursorName == IDC_ARROW) || (lpCursorName == IDC_IBEAM) || (lpCursorName == IDC_WAIT) ||
  944. (lpCursorName == IDC_CROSS) || (lpCursorName == IDC_UPARROW) || (lpCursorName == IDC_SIZE) ||
  945. (lpCursorName == IDC_ICON) || (lpCursorName == IDC_SIZENWSE) || (lpCursorName == IDC_SIZENESW) ||
  946. (lpCursorName == IDC_SIZEWE) || (lpCursorName == IDC_SIZENS) || (lpCursorName == IDC_SIZEALL) ||
  947. (lpCursorName == IDC_NO) || (lpCursorName == IDC_APPSTARTING) || (lpCursorName == IDC_HELP) ||
  948. (lpCursorName == IDC_HAND));
  949. return ::LoadCursor(NULL, lpCursorName);
  950. }
  951. inline HICON AtlLoadIcon(ATL::_U_STRINGorID icon)
  952. {
  953. return ::LoadIcon(ModuleHelper::GetResourceInstance(), icon.m_lpstr);
  954. }
  955. inline HICON AtlLoadSysIcon(LPCTSTR lpIconName)
  956. {
  957. #if (WINVER >= 0x0600)
  958. ATLASSERT((lpIconName == IDI_APPLICATION) || (lpIconName == IDI_ASTERISK) || (lpIconName == IDI_EXCLAMATION) ||
  959. (lpIconName == IDI_HAND) || (lpIconName == IDI_QUESTION) || (lpIconName == IDI_WINLOGO) ||
  960. (lpIconName == IDI_SHIELD));
  961. #else // !(WINVER >= 0x0600)
  962. ATLASSERT((lpIconName == IDI_APPLICATION) || (lpIconName == IDI_ASTERISK) || (lpIconName == IDI_EXCLAMATION) ||
  963. (lpIconName == IDI_HAND) || (lpIconName == IDI_QUESTION) || (lpIconName == IDI_WINLOGO));
  964. #endif // !(WINVER >= 0x0600)
  965. return ::LoadIcon(NULL, lpIconName);
  966. }
  967. inline HBITMAP AtlLoadBitmapImage(ATL::_U_STRINGorID bitmap, UINT fuLoad = LR_DEFAULTCOLOR)
  968. {
  969. return (HBITMAP)::LoadImage(ModuleHelper::GetResourceInstance(), bitmap.m_lpstr, IMAGE_BITMAP, 0, 0, fuLoad);
  970. }
  971. inline HCURSOR AtlLoadCursorImage(ATL::_U_STRINGorID cursor, UINT fuLoad = LR_DEFAULTCOLOR | LR_DEFAULTSIZE, int cxDesired = 0, int cyDesired = 0)
  972. {
  973. return (HCURSOR)::LoadImage(ModuleHelper::GetResourceInstance(), cursor.m_lpstr, IMAGE_CURSOR, cxDesired, cyDesired, fuLoad);
  974. }
  975. inline HICON AtlLoadIconImage(ATL::_U_STRINGorID icon, UINT fuLoad = LR_DEFAULTCOLOR | LR_DEFAULTSIZE, int cxDesired = 0, int cyDesired = 0)
  976. {
  977. return (HICON)::LoadImage(ModuleHelper::GetResourceInstance(), icon.m_lpstr, IMAGE_ICON, cxDesired, cyDesired, fuLoad);
  978. }
  979. #ifdef OEMRESOURCE
  980. inline HBITMAP AtlLoadSysBitmapImage(WORD wBitmapID, UINT fuLoad = LR_DEFAULTCOLOR)
  981. {
  982. ATLASSERT((wBitmapID >= 32734) && (wBitmapID <= 32767));
  983. ATLASSERT((fuLoad & LR_LOADFROMFILE) == 0); // this one doesn't load from a file
  984. return (HBITMAP)::LoadImage(NULL, MAKEINTRESOURCE(wBitmapID), IMAGE_BITMAP, 0, 0, fuLoad);
  985. }
  986. #endif // OEMRESOURCE
  987. inline HCURSOR AtlLoadSysCursorImage(ATL::_U_STRINGorID cursor, UINT fuLoad = LR_DEFAULTCOLOR | LR_DEFAULTSIZE, int cxDesired = 0, int cyDesired = 0)
  988. {
  989. #ifdef _DEBUG
  990. WORD wID = LOWORD(cursor.m_lpstr);
  991. ATLASSERT(((wID >= 32512) && (wID <= 32516)) || ((wID >= 32640) && (wID <= 32648)) || (wID == 32650) || (wID == 32651));
  992. ATLASSERT((fuLoad & LR_LOADFROMFILE) == 0); // this one doesn't load from a file
  993. #endif // _DEBUG
  994. return (HCURSOR)::LoadImage(NULL, cursor.m_lpstr, IMAGE_CURSOR, cxDesired, cyDesired, fuLoad);
  995. }
  996. inline HICON AtlLoadSysIconImage(ATL::_U_STRINGorID icon, UINT fuLoad = LR_DEFAULTCOLOR | LR_DEFAULTSIZE, int cxDesired = 0, int cyDesired = 0)
  997. {
  998. #ifdef _DEBUG
  999. WORD wID = LOWORD(icon.m_lpstr);
  1000. ATLASSERT((wID >= 32512) && (wID <= 32517));
  1001. ATLASSERT((fuLoad & LR_LOADFROMFILE) == 0); // this one doesn't load from a file
  1002. #endif // _DEBUG
  1003. return (HICON)::LoadImage(NULL, icon.m_lpstr, IMAGE_ICON, cxDesired, cyDesired, fuLoad);
  1004. }
  1005. inline bool AtlLoadString(UINT uID, BSTR& bstrText)
  1006. {
  1007. USES_CONVERSION;
  1008. ATLASSERT(bstrText == NULL);
  1009. LPTSTR lpstrText = NULL;
  1010. int nRes = 0;
  1011. for(int nLen = 256; ; nLen *= 2)
  1012. {
  1013. ATLTRY(lpstrText = new TCHAR[nLen]);
  1014. if(lpstrText == NULL)
  1015. break;
  1016. nRes = ::LoadString(ModuleHelper::GetResourceInstance(), uID, lpstrText, nLen);
  1017. if(nRes < nLen - 1)
  1018. break;
  1019. delete [] lpstrText;
  1020. lpstrText = NULL;
  1021. }
  1022. if(lpstrText != NULL)
  1023. {
  1024. if(nRes != 0)
  1025. bstrText = ::SysAllocString(T2OLE(lpstrText));
  1026. delete [] lpstrText;
  1027. }
  1028. return (bstrText != NULL) ? true : false;
  1029. }
  1030. } // namespace WTL
  1031. #endif // __ATLUSER_H__