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