/jEditLauncher/trunk/extern/wtl/atluser.h

# · C++ Header · 1391 lines · 1100 code · 210 blank · 81 comment · 336 complexity · 06d2730911f9002fcbd06e6097950c19 MD5 · raw file

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