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