Add policy_templates target for GN.
[chromium-blink-merge.git] / third_party / wtl / include / atluser.h
blob2df3b4d302010704687b52425c9f23cb0c8d8017
1 // Windows Template Library - WTL version 8.0
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 // Microsoft Permissive License (Ms-PL) which can be found in the file
7 // Ms-PL.txt at the root of this distribution.
9 #ifndef __ATLUSER_H__
10 #define __ATLUSER_H__
12 #pragma once
14 #ifndef __cplusplus
15 #error ATL requires C++ compilation (use a .cpp suffix)
16 #endif
18 #ifndef __ATLAPP_H__
19 #error atluser.h requires atlapp.h to be included first
20 #endif
23 ///////////////////////////////////////////////////////////////////////////////
24 // Classes in this file:
26 // CMenuItemInfo
27 // CMenuT<t_bManaged>
28 // CAcceleratorT<t_bManaged>
29 // CIconT<t_bManaged>
30 // CCursorT<t_bManaged>
31 // CResource
33 // Global functions:
34 // AtlMessageBox()
37 namespace WTL
40 ///////////////////////////////////////////////////////////////////////////////
41 // AtlMessageBox - accepts both memory and resource based strings
43 inline int AtlMessageBox(HWND hWndOwner, ATL::_U_STRINGorID message, ATL::_U_STRINGorID title = (LPCTSTR)NULL, UINT uType = MB_OK | MB_ICONINFORMATION)
45 ATLASSERT(hWndOwner == NULL || ::IsWindow(hWndOwner));
47 LPTSTR lpstrMessage = NULL;
48 if(IS_INTRESOURCE(message.m_lpstr))
50 for(int nLen = 256; ; nLen *= 2)
52 ATLTRY(lpstrMessage = new TCHAR[nLen]);
53 if(lpstrMessage == NULL)
55 ATLASSERT(FALSE);
56 return 0;
58 int nRes = ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(message.m_lpstr), lpstrMessage, nLen);
59 if(nRes < nLen - 1)
60 break;
61 delete [] lpstrMessage;
62 lpstrMessage = NULL;
65 message.m_lpstr = lpstrMessage;
68 LPTSTR lpstrTitle = NULL;
69 if(IS_INTRESOURCE(title.m_lpstr) && LOWORD(title.m_lpstr) != 0)
71 for(int nLen = 256; ; nLen *= 2)
73 ATLTRY(lpstrTitle = new TCHAR[nLen]);
74 if(lpstrTitle == NULL)
76 ATLASSERT(FALSE);
77 return 0;
79 int nRes = ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(title.m_lpstr), lpstrTitle, nLen);
80 if(nRes < nLen - 1)
81 break;
82 delete [] lpstrTitle;
83 lpstrTitle = NULL;
86 title.m_lpstr = lpstrTitle;
89 int nRet = ::MessageBox(hWndOwner, message.m_lpstr, title.m_lpstr, uType);
91 delete [] lpstrMessage;
92 delete [] lpstrTitle;
94 return nRet;
98 ///////////////////////////////////////////////////////////////////////////////
99 // CMenu
101 #if (WINVER >= 0x0500)
102 #ifndef MII_SIZEOF_STRUCT
103 #define MII_SIZEOF_STRUCT(structname, member) (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
104 #endif
105 #define MENUITEMINFO_SIZE_VERSION_400A MII_SIZEOF_STRUCT(MENUITEMINFOA, cch)
106 #define MENUITEMINFO_SIZE_VERSION_400W MII_SIZEOF_STRUCT(MENUITEMINFOW, cch)
107 #ifdef UNICODE
108 #define MENUITEMINFO_SIZE_VERSION_400 MENUITEMINFO_SIZE_VERSION_400W
109 #else
110 #define MENUITEMINFO_SIZE_VERSION_400 MENUITEMINFO_SIZE_VERSION_400A
111 #endif // !UNICODE
112 #endif // (WINVER >= 0x0500)
114 class CMenuItemInfo : public MENUITEMINFO
116 public:
117 CMenuItemInfo()
119 memset(this, 0, sizeof(MENUITEMINFO));
120 cbSize = sizeof(MENUITEMINFO);
121 #if (WINVER >= 0x0500)
122 // adjust struct size if running on older version of Windows
123 if(AtlIsOldWindows())
125 ATLASSERT(cbSize > MENUITEMINFO_SIZE_VERSION_400); // must be
126 cbSize = MENUITEMINFO_SIZE_VERSION_400;
128 #endif // (WINVER >= 0x0500)
133 // forward declarations
134 template <bool t_bManaged> class CMenuT;
135 typedef CMenuT<false> CMenuHandle;
136 typedef CMenuT<true> CMenu;
139 template <bool t_bManaged>
140 class CMenuT
142 public:
143 // Data members
144 HMENU m_hMenu;
146 // Constructor/destructor/operators
147 CMenuT(HMENU hMenu = NULL) : m_hMenu(hMenu)
150 ~CMenuT()
152 if(t_bManaged && m_hMenu != NULL)
153 DestroyMenu();
156 CMenuT<t_bManaged>& operator =(HMENU hMenu)
158 Attach(hMenu);
159 return *this;
162 void Attach(HMENU hMenuNew)
164 ATLASSERT(::IsMenu(hMenuNew));
165 if(t_bManaged && m_hMenu != NULL && m_hMenu != hMenuNew)
166 ::DestroyMenu(m_hMenu);
167 m_hMenu = hMenuNew;
170 HMENU Detach()
172 HMENU hMenu = m_hMenu;
173 m_hMenu = NULL;
174 return hMenu;
177 operator HMENU() const { return m_hMenu; }
179 bool IsNull() const { return (m_hMenu == NULL); }
181 BOOL IsMenu() const
183 return ::IsMenu(m_hMenu);
186 // Create/destroy methods
187 BOOL CreateMenu()
189 ATLASSERT(m_hMenu == NULL);
190 m_hMenu = ::CreateMenu();
191 return (m_hMenu != NULL) ? TRUE : FALSE;
194 BOOL CreatePopupMenu()
196 ATLASSERT(m_hMenu == NULL);
197 m_hMenu = ::CreatePopupMenu();
198 return (m_hMenu != NULL) ? TRUE : FALSE;
201 BOOL LoadMenu(ATL::_U_STRINGorID menu)
203 ATLASSERT(m_hMenu == NULL);
204 m_hMenu = ::LoadMenu(ModuleHelper::GetResourceInstance(), menu.m_lpstr);
205 return (m_hMenu != NULL) ? TRUE : FALSE;
208 #ifndef _WIN32_WCE
209 BOOL LoadMenuIndirect(const void* lpMenuTemplate)
211 ATLASSERT(m_hMenu == NULL);
212 m_hMenu = ::LoadMenuIndirect(lpMenuTemplate);
213 return (m_hMenu != NULL) ? TRUE : FALSE;
215 #endif // !_WIN32_WCE
217 BOOL DestroyMenu()
219 if (m_hMenu == NULL)
220 return FALSE;
221 BOOL bRet = ::DestroyMenu(m_hMenu);
222 if(bRet)
223 m_hMenu = NULL;
224 return bRet;
227 // Menu Operations
228 BOOL DeleteMenu(UINT nPosition, UINT nFlags)
230 ATLASSERT(::IsMenu(m_hMenu));
231 return ::DeleteMenu(m_hMenu, nPosition, nFlags);
234 BOOL TrackPopupMenu(UINT nFlags, int x, int y, HWND hWnd, LPCRECT lpRect = NULL)
236 ATLASSERT(::IsMenu(m_hMenu));
237 #ifndef _WIN32_WCE
238 #if (WINVER >= 0x0500)
239 x = _FixTrackMenuPopupX(x, y);
240 #endif // !(WINVER >= 0x0500)
241 return ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0, hWnd, lpRect);
242 #else // CE specific
243 lpRect;
244 return ::TrackPopupMenuEx(m_hMenu, nFlags, x, y, hWnd, NULL);
245 #endif // _WIN32_WCE
248 BOOL TrackPopupMenuEx(UINT uFlags, int x, int y, HWND hWnd, LPTPMPARAMS lptpm = NULL)
250 ATLASSERT(::IsMenu(m_hMenu));
251 #if (WINVER >= 0x0500) && !defined(_WIN32_WCE)
252 x = _FixTrackMenuPopupX(x, y);
253 #endif // (WINVER >= 0x0500) && !defined(_WIN32_WCE)
254 return ::TrackPopupMenuEx(m_hMenu, uFlags, x, y, hWnd, lptpm);
257 #if (WINVER >= 0x0500) && !defined(_WIN32_WCE)
258 // helper that fixes popup menu X position when it's off-screen
259 static int _FixTrackMenuPopupX(int x, int y)
261 POINT pt = { x, y };
262 HMONITOR hMonitor = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
263 if(hMonitor == NULL)
265 HMONITOR hMonitorNear = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
266 if(hMonitorNear != NULL)
268 MONITORINFO mi = { 0 };
269 mi.cbSize = sizeof(MONITORINFO);
270 if(::GetMonitorInfo(hMonitorNear, &mi) != FALSE)
272 if(x < mi.rcWork.left)
273 x = mi.rcWork.left;
274 else if(x > mi.rcWork.right)
275 x = mi.rcWork.right;
280 return x;
283 BOOL GetMenuInfo(LPMENUINFO lpMenuInfo) const
285 ATLASSERT(::IsMenu(m_hMenu));
286 return ::GetMenuInfo(m_hMenu, lpMenuInfo);
289 BOOL SetMenuInfo(LPCMENUINFO lpMenuInfo)
291 ATLASSERT(::IsMenu(m_hMenu));
292 return ::SetMenuInfo(m_hMenu, lpMenuInfo);
294 #endif // (WINVER >= 0x0500) && !defined(_WIN32_WCE)
296 // Menu Item Operations
297 BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
299 ATLASSERT(::IsMenu(m_hMenu));
300 return ::AppendMenu(m_hMenu, nFlags, nIDNewItem, lpszNewItem);
303 BOOL AppendMenu(UINT nFlags, HMENU hSubMenu, LPCTSTR lpszNewItem)
305 ATLASSERT(::IsMenu(m_hMenu));
306 ATLASSERT(::IsMenu(hSubMenu));
307 return ::AppendMenu(m_hMenu, nFlags | MF_POPUP, (UINT_PTR)hSubMenu, lpszNewItem);
310 #ifndef _WIN32_WCE
311 BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
313 ATLASSERT(::IsMenu(m_hMenu));
314 return ::AppendMenu(m_hMenu, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
317 BOOL AppendMenu(UINT nFlags, HMENU hSubMenu, HBITMAP hBmp)
319 ATLASSERT(::IsMenu(m_hMenu));
320 ATLASSERT(::IsMenu(hSubMenu));
321 return ::AppendMenu(m_hMenu, nFlags | (MF_BITMAP | MF_POPUP), (UINT_PTR)hSubMenu, (LPCTSTR)hBmp);
323 #endif // !_WIN32_WCE
325 UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck)
327 ATLASSERT(::IsMenu(m_hMenu));
328 return (UINT)::CheckMenuItem(m_hMenu, nIDCheckItem, nCheck);
331 UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable)
333 ATLASSERT(::IsMenu(m_hMenu));
334 return ::EnableMenuItem(m_hMenu, nIDEnableItem, nEnable);
337 #ifndef _WIN32_WCE
338 BOOL HiliteMenuItem(HWND hWnd, UINT uIDHiliteItem, UINT uHilite)
340 ATLASSERT(::IsMenu(m_hMenu));
341 return ::HiliteMenuItem(hWnd, m_hMenu, uIDHiliteItem, uHilite);
344 int GetMenuItemCount() const
346 ATLASSERT(::IsMenu(m_hMenu));
347 return ::GetMenuItemCount(m_hMenu);
350 UINT GetMenuItemID(int nPos) const
352 ATLASSERT(::IsMenu(m_hMenu));
353 return ::GetMenuItemID(m_hMenu, nPos);
356 UINT GetMenuState(UINT nID, UINT nFlags) const
358 ATLASSERT(::IsMenu(m_hMenu));
359 return ::GetMenuState(m_hMenu, nID, nFlags);
362 int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount, UINT nFlags) const
364 ATLASSERT(::IsMenu(m_hMenu));
365 return ::GetMenuString(m_hMenu, nIDItem, lpString, nMaxCount, nFlags);
368 int GetMenuStringLen(UINT nIDItem, UINT nFlags) const
370 ATLASSERT(::IsMenu(m_hMenu));
371 return ::GetMenuString(m_hMenu, nIDItem, NULL, 0, nFlags);
374 #ifndef _ATL_NO_COM
375 BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
377 USES_CONVERSION;
378 ATLASSERT(::IsMenu(m_hMenu));
379 ATLASSERT(bstrText == NULL);
381 int nLen = GetMenuStringLen(nIDItem, nFlags);
382 if(nLen == 0)
384 bstrText = ::SysAllocString(OLESTR(""));
385 return (bstrText != NULL) ? TRUE : FALSE;
388 nLen++; // increment to include terminating NULL char
389 CTempBuffer<TCHAR, _WTL_STACK_ALLOC_THRESHOLD> buff;
390 LPTSTR lpszText = buff.Allocate(nLen);
391 if(lpszText == NULL)
392 return FALSE;
394 if(!GetMenuString(nIDItem, lpszText, nLen, nFlags))
395 return FALSE;
397 bstrText = ::SysAllocString(T2OLE(lpszText));
398 return (bstrText != NULL) ? TRUE : FALSE;
400 #endif // !_ATL_NO_COM
401 #endif // !_WIN32_WCE
403 #if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
404 int GetMenuString(UINT nIDItem, _CSTRING_NS::CString& strText, UINT nFlags) const
406 ATLASSERT(::IsMenu(m_hMenu));
408 int nLen = GetMenuStringLen(nIDItem, nFlags);
409 if(nLen == 0)
410 return 0;
412 nLen++; // increment to include terminating NULL char
413 LPTSTR lpstr = strText.GetBufferSetLength(nLen);
414 if(lpstr == NULL)
415 return 0;
416 int nRet = GetMenuString(nIDItem, lpstr, nLen, nFlags);
417 strText.ReleaseBuffer();
418 return nRet;
420 #endif // defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
422 CMenuHandle GetSubMenu(int nPos) const
424 ATLASSERT(::IsMenu(m_hMenu));
425 return CMenuHandle(::GetSubMenu(m_hMenu, nPos));
428 BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
430 ATLASSERT(::IsMenu(m_hMenu));
431 return ::InsertMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
434 BOOL InsertMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, LPCTSTR lpszNewItem)
436 ATLASSERT(::IsMenu(m_hMenu));
437 ATLASSERT(::IsMenu(hSubMenu));
438 return ::InsertMenu(m_hMenu, nPosition, nFlags | MF_POPUP, (UINT_PTR)hSubMenu, lpszNewItem);
441 #ifndef _WIN32_WCE
442 BOOL InsertMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
444 ATLASSERT(::IsMenu(m_hMenu));
445 return ::InsertMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
448 BOOL InsertMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, HBITMAP hBmp)
450 ATLASSERT(::IsMenu(m_hMenu));
451 ATLASSERT(::IsMenu(hSubMenu));
452 return ::InsertMenu(m_hMenu, nPosition, nFlags | (MF_BITMAP | MF_POPUP), (UINT_PTR)hSubMenu, (LPCTSTR)hBmp);
455 BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
457 ATLASSERT(::IsMenu(m_hMenu));
458 return ::ModifyMenu(m_hMenu, nPosition, nFlags, nIDNewItem, lpszNewItem);
461 BOOL ModifyMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, LPCTSTR lpszNewItem)
463 ATLASSERT(::IsMenu(m_hMenu));
464 ATLASSERT(::IsMenu(hSubMenu));
465 return ::ModifyMenu(m_hMenu, nPosition, nFlags | MF_POPUP, (UINT_PTR)hSubMenu, lpszNewItem);
468 BOOL ModifyMenu(UINT nPosition, UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
470 ATLASSERT(::IsMenu(m_hMenu));
471 return ::ModifyMenu(m_hMenu, nPosition, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
474 BOOL ModifyMenu(UINT nPosition, UINT nFlags, HMENU hSubMenu, HBITMAP hBmp)
476 ATLASSERT(::IsMenu(m_hMenu));
477 ATLASSERT(::IsMenu(hSubMenu));
478 return ::ModifyMenu(m_hMenu, nPosition, nFlags | (MF_BITMAP | MF_POPUP), (UINT_PTR)hSubMenu, (LPCTSTR)hBmp);
480 #endif // !_WIN32_WCE
482 BOOL RemoveMenu(UINT nPosition, UINT nFlags)
484 ATLASSERT(::IsMenu(m_hMenu));
485 return ::RemoveMenu(m_hMenu, nPosition, nFlags);
488 #ifndef _WIN32_WCE
489 BOOL SetMenuItemBitmaps(UINT nPosition, UINT nFlags, HBITMAP hBmpUnchecked, HBITMAP hBmpChecked)
491 ATLASSERT(::IsMenu(m_hMenu));
492 return ::SetMenuItemBitmaps(m_hMenu, nPosition, nFlags, hBmpUnchecked, hBmpChecked);
494 #endif // !_WIN32_WCE
496 BOOL CheckMenuRadioItem(UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags)
498 ATLASSERT(::IsMenu(m_hMenu));
499 return ::CheckMenuRadioItem(m_hMenu, nIDFirst, nIDLast, nIDItem, nFlags);
502 BOOL GetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii) const
504 ATLASSERT(::IsMenu(m_hMenu));
505 return (BOOL)::GetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
508 BOOL SetMenuItemInfo(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
510 ATLASSERT(::IsMenu(m_hMenu));
511 return (BOOL)::SetMenuItemInfo(m_hMenu, uItem, bByPosition, lpmii);
514 #ifndef _WIN32_WCE
515 BOOL InsertMenuItem(UINT uItem, BOOL bByPosition, LPMENUITEMINFO lpmii)
517 ATLASSERT(::IsMenu(m_hMenu));
518 return (BOOL)::InsertMenuItem(m_hMenu, uItem, bByPosition, lpmii);
521 UINT GetMenuDefaultItem(BOOL bByPosition = FALSE, UINT uFlags = 0U) const
523 ATLASSERT(::IsMenu(m_hMenu));
524 return ::GetMenuDefaultItem(m_hMenu, (UINT)bByPosition, uFlags);
527 BOOL SetMenuDefaultItem(UINT uItem = (UINT)-1, BOOL bByPosition = FALSE)
529 ATLASSERT(::IsMenu(m_hMenu));
530 return ::SetMenuDefaultItem(m_hMenu, uItem, (UINT)bByPosition);
533 BOOL GetMenuItemRect(HWND hWnd, UINT uItem, LPRECT lprcItem) const
535 ATLASSERT(::IsMenu(m_hMenu));
536 return ::GetMenuItemRect(hWnd, m_hMenu, uItem, lprcItem);
539 int MenuItemFromPoint(HWND hWnd, POINT point) const
541 ATLASSERT(::IsMenu(m_hMenu));
542 return ::MenuItemFromPoint(hWnd, m_hMenu, point);
545 // Context Help Functions
546 BOOL SetMenuContextHelpId(DWORD dwContextHelpId)
548 ATLASSERT(::IsMenu(m_hMenu));
549 return ::SetMenuContextHelpId(m_hMenu, dwContextHelpId);
552 DWORD GetMenuContextHelpId() const
554 ATLASSERT(::IsMenu(m_hMenu));
555 return ::GetMenuContextHelpId(m_hMenu);
557 #endif // !_WIN32_WCE
561 ///////////////////////////////////////////////////////////////////////////////
562 // CAccelerator
564 template <bool t_bManaged>
565 class CAcceleratorT
567 public:
568 HACCEL m_hAccel;
570 // Constructor/destructor/operators
571 CAcceleratorT(HACCEL hAccel = NULL) : m_hAccel(hAccel)
574 ~CAcceleratorT()
576 if(t_bManaged && m_hAccel != NULL)
577 ::DestroyAcceleratorTable(m_hAccel);
580 CAcceleratorT<t_bManaged>& operator =(HACCEL hAccel)
582 Attach(hAccel);
583 return *this;
586 void Attach(HACCEL hAccel)
588 if(t_bManaged && m_hAccel != NULL)
589 ::DestroyAcceleratorTable(m_hAccel);
590 m_hAccel = hAccel;
593 HACCEL Detach()
595 HACCEL hAccel = m_hAccel;
596 m_hAccel = NULL;
597 return hAccel;
600 operator HACCEL() const { return m_hAccel; }
602 bool IsNull() const { return m_hAccel == NULL; }
604 // Create/destroy methods
605 HACCEL LoadAccelerators(ATL::_U_STRINGorID accel)
607 ATLASSERT(m_hAccel == NULL);
608 m_hAccel = ::LoadAccelerators(ModuleHelper::GetResourceInstance(), accel.m_lpstr);
609 return m_hAccel;
612 HACCEL CreateAcceleratorTable(LPACCEL pAccel, int cEntries)
614 ATLASSERT(m_hAccel == NULL);
615 ATLASSERT(pAccel != NULL);
616 m_hAccel = ::CreateAcceleratorTable(pAccel, cEntries);
617 return m_hAccel;
620 void DestroyObject()
622 if(m_hAccel != NULL)
624 ::DestroyAcceleratorTable(m_hAccel);
625 m_hAccel = NULL;
629 // Operations
630 #ifndef _WIN32_WCE
631 int CopyAcceleratorTable(LPACCEL lpAccelDst, int cEntries)
633 ATLASSERT(m_hAccel != NULL);
634 ATLASSERT(lpAccelDst != NULL);
635 return ::CopyAcceleratorTable(m_hAccel, lpAccelDst, cEntries);
638 int GetEntriesCount() const
640 ATLASSERT(m_hAccel != NULL);
641 return ::CopyAcceleratorTable(m_hAccel, NULL, 0);
643 #endif // !_WIN32_WCE
645 BOOL TranslateAccelerator(HWND hWnd, LPMSG pMsg)
647 ATLASSERT(m_hAccel != NULL);
648 ATLASSERT(::IsWindow(hWnd));
649 ATLASSERT(pMsg != NULL);
650 return ::TranslateAccelerator(hWnd, m_hAccel, pMsg);
654 typedef CAcceleratorT<false> CAcceleratorHandle;
655 typedef CAcceleratorT<true> CAccelerator;
658 ///////////////////////////////////////////////////////////////////////////////
659 // CIcon
661 template <bool t_bManaged>
662 class CIconT
664 public:
665 HICON m_hIcon;
667 // Constructor/destructor/operators
668 CIconT(HICON hIcon = NULL) : m_hIcon(hIcon)
671 ~CIconT()
673 if(t_bManaged && m_hIcon != NULL)
674 ::DestroyIcon(m_hIcon);
677 CIconT<t_bManaged>& operator =(HICON hIcon)
679 Attach(hIcon);
680 return *this;
683 void Attach(HICON hIcon)
685 if(t_bManaged && m_hIcon != NULL)
686 ::DestroyIcon(m_hIcon);
687 m_hIcon = hIcon;
690 HICON Detach()
692 HICON hIcon = m_hIcon;
693 m_hIcon = NULL;
694 return hIcon;
697 operator HICON() const { return m_hIcon; }
699 bool IsNull() const { return m_hIcon == NULL; }
701 // Create/destroy methods
702 HICON LoadIcon(ATL::_U_STRINGorID icon)
704 ATLASSERT(m_hIcon == NULL);
705 m_hIcon = ::LoadIcon(ModuleHelper::GetResourceInstance(), icon.m_lpstr);
706 return m_hIcon;
709 HICON LoadIcon(ATL::_U_STRINGorID icon, int cxDesired, int cyDesired, UINT fuLoad = 0)
711 ATLASSERT(m_hIcon == NULL);
712 m_hIcon = (HICON) ::LoadImage(ModuleHelper::GetResourceInstance(), icon.m_lpstr, IMAGE_ICON, cxDesired, cyDesired, fuLoad);
713 return m_hIcon;
716 #ifndef _WIN32_WCE
717 HICON LoadOEMIcon(LPCTSTR lpstrIconName)
719 ATLASSERT(m_hIcon == NULL);
720 ATLASSERT(IsOEMIcon(lpstrIconName));
721 m_hIcon = ::LoadIcon(NULL, lpstrIconName);
722 return m_hIcon;
725 HICON CreateIcon(int nWidth, int nHeight, BYTE cPlanes, BYTE cBitsPixel, CONST BYTE* lpbANDbits, CONST BYTE *lpbXORbits)
727 ATLASSERT(m_hIcon == NULL);
728 ATLASSERT(lpbANDbits != NULL);
729 ATLASSERT(lpbXORbits != NULL);
730 m_hIcon = ::CreateIcon(ModuleHelper::GetResourceInstance(), nWidth, nHeight, cPlanes, cBitsPixel, lpbANDbits, lpbXORbits);
731 return m_hIcon;
734 HICON CreateIconFromResource(PBYTE pBits, DWORD dwResSize, DWORD dwVersion = 0x00030000)
736 ATLASSERT(m_hIcon == NULL);
737 ATLASSERT(pBits != NULL);
738 m_hIcon = ::CreateIconFromResource(pBits, dwResSize, TRUE, dwVersion);
739 return m_hIcon;
742 HICON CreateIconFromResourceEx(PBYTE pbBits, DWORD cbBits, DWORD dwVersion = 0x00030000, int cxDesired = 0, int cyDesired = 0, UINT uFlags = LR_DEFAULTCOLOR)
744 ATLASSERT(m_hIcon == NULL);
745 ATLASSERT(pbBits != NULL);
746 ATLASSERT(cbBits > 0);
747 m_hIcon = ::CreateIconFromResourceEx(pbBits, cbBits, TRUE, dwVersion, cxDesired, cyDesired, uFlags);
748 return m_hIcon;
750 #endif // !_WIN32_WCE
752 HICON CreateIconIndirect(PICONINFO pIconInfo)
754 ATLASSERT(m_hIcon == NULL);
755 ATLASSERT(pIconInfo != NULL);
756 m_hIcon = ::CreateIconIndirect(pIconInfo);
757 return m_hIcon;
760 #ifndef _WIN32_WCE
761 HICON ExtractIcon(LPCTSTR lpszExeFileName, UINT nIconIndex)
763 ATLASSERT(m_hIcon == NULL);
764 ATLASSERT(lpszExeFileName != NULL);
765 m_hIcon = ::ExtractIcon(ModuleHelper::GetModuleInstance(), lpszExeFileName, nIconIndex);
766 return m_hIcon;
769 HICON ExtractAssociatedIcon(HINSTANCE hInst, LPTSTR lpIconPath, LPWORD lpiIcon)
771 ATLASSERT(m_hIcon == NULL);
772 ATLASSERT(lpIconPath != NULL);
773 ATLASSERT(lpiIcon != NULL);
774 m_hIcon = ::ExtractAssociatedIcon(hInst, lpIconPath, lpiIcon);
775 return m_hIcon;
777 #endif // !_WIN32_WCE
779 BOOL DestroyIcon()
781 ATLASSERT(m_hIcon != NULL);
782 BOOL bRet = ::DestroyIcon(m_hIcon);
783 if(bRet != FALSE)
784 m_hIcon = NULL;
785 return bRet;
788 // Operations
789 #ifndef _WIN32_WCE
790 HICON CopyIcon()
792 ATLASSERT(m_hIcon != NULL);
793 return ::CopyIcon(m_hIcon);
796 HICON DuplicateIcon()
798 ATLASSERT(m_hIcon != NULL);
799 return ::DuplicateIcon(NULL, m_hIcon);
801 #endif // !_WIN32_WCE
803 BOOL DrawIcon(HDC hDC, int x, int y)
805 ATLASSERT(m_hIcon != NULL);
806 #ifndef _WIN32_WCE
807 return ::DrawIcon(hDC, x, y, m_hIcon);
808 #else // CE specific
809 return ::DrawIconEx(hDC, x, y, m_hIcon, 0, 0, 0, NULL, DI_NORMAL);
810 #endif // _WIN32_WCE
813 BOOL DrawIcon(HDC hDC, POINT pt)
815 ATLASSERT(m_hIcon != NULL);
816 #ifndef _WIN32_WCE
817 return ::DrawIcon(hDC, pt.x, pt.y, m_hIcon);
818 #else // CE specific
819 return ::DrawIconEx(hDC, pt.x, pt.y, m_hIcon, 0, 0, 0, NULL, DI_NORMAL);
820 #endif // _WIN32_WCE
823 BOOL DrawIconEx(HDC hDC, int x, int y, int cxWidth, int cyWidth, UINT uStepIfAniCur = 0, HBRUSH hbrFlickerFreeDraw = NULL, UINT uFlags = DI_NORMAL)
825 ATLASSERT(m_hIcon != NULL);
826 return ::DrawIconEx(hDC, x, y, m_hIcon, cxWidth, cyWidth, uStepIfAniCur, hbrFlickerFreeDraw, uFlags);
829 BOOL DrawIconEx(HDC hDC, POINT pt, SIZE size, UINT uStepIfAniCur = 0, HBRUSH hbrFlickerFreeDraw = NULL, UINT uFlags = DI_NORMAL)
831 ATLASSERT(m_hIcon != NULL);
832 return ::DrawIconEx(hDC, pt.x, pt.y, m_hIcon, size.cx, size.cy, uStepIfAniCur, hbrFlickerFreeDraw, uFlags);
835 #ifndef _WIN32_WCE
836 BOOL GetIconInfo(PICONINFO pIconInfo) const
838 ATLASSERT(m_hIcon != NULL);
839 ATLASSERT(pIconInfo != NULL);
840 return ::GetIconInfo(m_hIcon, pIconInfo);
843 #if (_WIN32_WINNT >= 0x0600)
844 BOOL GetIconInfoEx(PICONINFOEX pIconInfo) const
846 ATLASSERT(m_hIcon != NULL);
847 ATLASSERT(pIconInfo != NULL);
848 return ::GetIconInfoEx(m_hIcon, pIconInfo);
850 #endif // (_WIN32_WINNT >= 0x0600)
852 #if defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
853 HRESULT LoadIconMetric(ATL::_U_STRINGorID icon, int lims)
855 ATLASSERT(m_hIcon == NULL);
856 USES_CONVERSION;
857 return ::LoadIconMetric(ModuleHelper::GetResourceInstance(), T2CW(icon.m_lpstr), lims, &m_hIcon);
860 HRESULT LoadIconWithScaleDown(ATL::_U_STRINGorID icon, int cx, int cy)
862 ATLASSERT(m_hIcon == NULL);
863 USES_CONVERSION;
864 return ::LoadIconWithScaleDown(ModuleHelper::GetResourceInstance(), T2CW(icon.m_lpstr), cx, cy, &m_hIcon);
867 HRESULT LoadOEMIconMetric(LPCTSTR lpstrIconName, int lims)
869 ATLASSERT(m_hIcon == NULL);
870 ATLASSERT(IsOEMIcon(lpstrIconName));
871 return ::LoadIconMetric(NULL, (LPCWSTR)lpstrIconName, lims, &m_hIcon);
874 HRESULT LoadOEMIconWithScaleDown(LPCTSTR lpstrIconName, int cx, int cy)
876 ATLASSERT(m_hIcon == NULL);
877 ATLASSERT(IsOEMIcon(lpstrIconName));
878 USES_CONVERSION;
879 return ::LoadIconWithScaleDown(NULL, (LPCWSTR)lpstrIconName, cx, cy, &m_hIcon);
881 #endif // defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
882 #endif // !_WIN32_WCE
884 // Helper
885 #ifndef _WIN32_WCE
886 static bool IsOEMIcon(LPCTSTR lpstrIconName)
888 #if (WINVER >= 0x0600)
889 return (lpstrIconName == IDI_APPLICATION || lpstrIconName == IDI_ASTERISK || lpstrIconName == IDI_EXCLAMATION ||
890 lpstrIconName == IDI_HAND || lpstrIconName == IDI_QUESTION || lpstrIconName == IDI_WINLOGO ||
891 lpstrIconName == IDI_SHIELD);
892 #else // !(WINVER >= 0x0600)
893 return (lpstrIconName == IDI_APPLICATION || lpstrIconName == IDI_ASTERISK || lpstrIconName == IDI_EXCLAMATION ||
894 lpstrIconName == IDI_HAND || lpstrIconName == IDI_QUESTION || lpstrIconName == IDI_WINLOGO);
895 #endif // !(WINVER >= 0x0600)
897 #endif // !_WIN32_WCE
900 typedef CIconT<false> CIconHandle;
901 typedef CIconT<true> CIcon;
904 ///////////////////////////////////////////////////////////////////////////////
905 // CCursor
907 // protect template member from a winuser.h macro
908 #ifdef CopyCursor
909 #undef CopyCursor
910 #endif
912 template <bool t_bManaged>
913 class CCursorT
915 public:
916 HCURSOR m_hCursor;
918 // Constructor/destructor/operators
919 CCursorT(HCURSOR hCursor = NULL) : m_hCursor(hCursor)
922 ~CCursorT()
924 if(t_bManaged && m_hCursor != NULL)
925 DestroyCursor();
928 CCursorT<t_bManaged>& operator =(HCURSOR hCursor)
930 Attach(hCursor);
931 return *this;
934 void Attach(HCURSOR hCursor)
936 if(t_bManaged && m_hCursor != NULL)
937 DestroyCursor();
938 m_hCursor = hCursor;
941 HCURSOR Detach()
943 HCURSOR hCursor = m_hCursor;
944 m_hCursor = NULL;
945 return hCursor;
948 operator HCURSOR() const { return m_hCursor; }
950 bool IsNull() const { return m_hCursor == NULL; }
952 // Create/destroy methods
953 HCURSOR LoadCursor(ATL::_U_STRINGorID cursor)
955 ATLASSERT(m_hCursor == NULL);
956 m_hCursor = ::LoadCursor(ModuleHelper::GetResourceInstance(), cursor.m_lpstr);
957 return m_hCursor;
960 HCURSOR LoadSysCursor(LPCTSTR lpstrCursorName)
962 ATLASSERT(m_hCursor == NULL);
963 #if (WINVER >= 0x0500)
964 ATLASSERT(lpstrCursorName == IDC_ARROW || lpstrCursorName == IDC_IBEAM || lpstrCursorName == IDC_WAIT ||
965 lpstrCursorName == IDC_CROSS || lpstrCursorName == IDC_UPARROW || lpstrCursorName == IDC_SIZE ||
966 lpstrCursorName == IDC_ICON || lpstrCursorName == IDC_SIZENWSE || lpstrCursorName == IDC_SIZENESW ||
967 lpstrCursorName == IDC_SIZEWE || lpstrCursorName == IDC_SIZENS || lpstrCursorName == IDC_SIZEALL ||
968 lpstrCursorName == IDC_NO || lpstrCursorName == IDC_APPSTARTING || lpstrCursorName == IDC_HELP ||
969 lpstrCursorName == IDC_HAND);
970 #else // !(WINVER >= 0x0500)
971 ATLASSERT(lpstrCursorName == IDC_ARROW || lpstrCursorName == IDC_IBEAM || lpstrCursorName == IDC_WAIT ||
972 lpstrCursorName == IDC_CROSS || lpstrCursorName == IDC_UPARROW || lpstrCursorName == IDC_SIZE ||
973 lpstrCursorName == IDC_ICON || lpstrCursorName == IDC_SIZENWSE || lpstrCursorName == IDC_SIZENESW ||
974 lpstrCursorName == IDC_SIZEWE || lpstrCursorName == IDC_SIZENS || lpstrCursorName == IDC_SIZEALL ||
975 lpstrCursorName == IDC_NO || lpstrCursorName == IDC_APPSTARTING || lpstrCursorName == IDC_HELP);
976 #endif // !(WINVER >= 0x0500)
977 m_hCursor = ::LoadCursor(NULL, lpstrCursorName);
978 return m_hCursor;
981 // deprecated
982 HCURSOR LoadOEMCursor(LPCTSTR lpstrCursorName)
984 return LoadSysCursor(lpstrCursorName);
987 HCURSOR LoadCursor(ATL::_U_STRINGorID cursor, int cxDesired, int cyDesired, UINT fuLoad = 0)
989 ATLASSERT(m_hCursor == NULL);
990 m_hCursor = (HCURSOR) ::LoadImage(ModuleHelper::GetResourceInstance(), cursor.m_lpstr, IMAGE_CURSOR, cxDesired, cyDesired, fuLoad);
991 return m_hCursor;
994 #ifndef _WIN32_WCE
995 HCURSOR LoadCursorFromFile(LPCTSTR pstrFilename)
997 ATLASSERT(m_hCursor == NULL);
998 ATLASSERT(pstrFilename != NULL);
999 m_hCursor = ::LoadCursorFromFile(pstrFilename);
1000 return m_hCursor;
1002 #endif // !_WIN32_WCE
1004 #if !defined(_WIN32_WCE) || ((_WIN32_WCE >= 0x400) && !(defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)))
1005 HCURSOR CreateCursor(int xHotSpot, int yHotSpot, int nWidth, int nHeight, CONST VOID *pvANDPlane, CONST VOID *pvXORPlane)
1007 ATLASSERT(m_hCursor == NULL);
1008 m_hCursor = ::CreateCursor(ModuleHelper::GetResourceInstance(), xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane);
1009 return m_hCursor;
1011 #endif // !defined(_WIN32_WCE) || ((_WIN32_WCE >= 0x400) && !(defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)))
1013 #ifndef _WIN32_WCE
1014 HCURSOR CreateCursorFromResource(PBYTE pBits, DWORD dwResSize, DWORD dwVersion = 0x00030000)
1016 ATLASSERT(m_hCursor == NULL);
1017 ATLASSERT(pBits != NULL);
1018 m_hCursor = (HCURSOR)::CreateIconFromResource(pBits, dwResSize, FALSE, dwVersion);
1019 return m_hCursor;
1022 HCURSOR CreateCursorFromResourceEx(PBYTE pbBits, DWORD cbBits, DWORD dwVersion = 0x00030000, int cxDesired = 0, int cyDesired = 0, UINT uFlags = LR_DEFAULTCOLOR)
1024 ATLASSERT(m_hCursor == NULL);
1025 ATLASSERT(pbBits != NULL);
1026 ATLASSERT(cbBits > 0);
1027 m_hCursor = (HCURSOR)::CreateIconFromResourceEx(pbBits, cbBits, FALSE, dwVersion, cxDesired, cyDesired, uFlags);
1028 return m_hCursor;
1030 #endif // !_WIN32_WCE
1032 BOOL DestroyCursor()
1034 ATLASSERT(m_hCursor != NULL);
1035 #if !defined(_WIN32_WCE) || ((_WIN32_WCE >= 0x400) && !(defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)))
1036 BOOL bRet = ::DestroyCursor(m_hCursor);
1037 if(bRet != FALSE)
1038 m_hCursor = NULL;
1039 return bRet;
1040 #else // !(!defined(_WIN32_WCE) || ((_WIN32_WCE >= 0x400) && !(defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP))))
1041 ATLTRACE2(atlTraceUI, 0, _T("Warning: This version of Windows CE does not have ::DestroyCursor()\n"));
1042 return FALSE;
1043 #endif // !(!defined(_WIN32_WCE) || ((_WIN32_WCE >= 0x400) && !(defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP))))
1046 // Operations
1047 #ifndef _WIN32_WCE
1048 HCURSOR CopyCursor()
1050 ATLASSERT(m_hCursor != NULL);
1051 return (HCURSOR)::CopyIcon((HICON)m_hCursor);
1053 #endif // !_WIN32_WCE
1055 #if (WINVER >= 0x0500) && !defined(_WIN32_WCE)
1056 BOOL GetCursorInfo(LPCURSORINFO pCursorInfo)
1058 ATLASSERT(m_hCursor != NULL);
1059 ATLASSERT(pCursorInfo != NULL);
1060 return ::GetCursorInfo(pCursorInfo);
1062 #endif // (WINVER >= 0x0500) && !defined(_WIN32_WCE)
1065 typedef CCursorT<false> CCursorHandle;
1066 typedef CCursorT<true> CCursor;
1069 ///////////////////////////////////////////////////////////////////////////////
1070 // CResource - Wraps a generic Windows resource.
1071 // Use it with custom resource types other than the
1072 // standard RT_CURSOR, RT_BITMAP, etc.
1074 class CResource
1076 public:
1077 HGLOBAL m_hGlobal;
1078 HRSRC m_hResource;
1080 // Constructor/destructor
1081 CResource() : m_hGlobal(NULL), m_hResource(NULL)
1084 ~CResource()
1086 Release();
1089 // Load methods
1090 bool Load(ATL::_U_STRINGorID Type, ATL::_U_STRINGorID ID)
1092 ATLASSERT(m_hResource == NULL);
1093 ATLASSERT(m_hGlobal == NULL);
1095 m_hResource = ::FindResource(ModuleHelper::GetResourceInstance(), ID.m_lpstr, Type.m_lpstr);
1096 if(m_hResource == NULL)
1097 return false;
1099 m_hGlobal = ::LoadResource(ModuleHelper::GetResourceInstance(), m_hResource);
1100 if(m_hGlobal == NULL)
1102 m_hResource = NULL;
1103 return false;
1106 return true;
1109 #ifndef _WIN32_WCE
1110 bool LoadEx(ATL::_U_STRINGorID Type, ATL::_U_STRINGorID ID, WORD wLanguage)
1112 ATLASSERT(m_hResource == NULL);
1113 ATLASSERT(m_hGlobal == NULL);
1115 m_hResource = ::FindResourceEx(ModuleHelper::GetResourceInstance(), ID.m_lpstr, Type.m_lpstr, wLanguage);
1116 if(m_hResource == NULL)
1117 return false;
1119 m_hGlobal = ::LoadResource(ModuleHelper::GetResourceInstance(), m_hResource);
1120 if(m_hGlobal == NULL)
1122 m_hResource = NULL;
1123 return false;
1126 return true;
1128 #endif // !_WIN32_WCE
1130 // Misc. operations
1131 DWORD GetSize() const
1133 ATLASSERT(m_hResource != NULL);
1134 return ::SizeofResource(ModuleHelper::GetResourceInstance(), m_hResource);
1137 LPVOID Lock()
1139 ATLASSERT(m_hResource != NULL);
1140 ATLASSERT(m_hGlobal != NULL);
1141 LPVOID pVoid = ::LockResource(m_hGlobal);
1142 ATLASSERT(pVoid != NULL);
1143 return pVoid;
1146 void Release()
1148 if(m_hGlobal != NULL)
1150 FreeResource(m_hGlobal);
1151 m_hGlobal = NULL;
1152 m_hResource = NULL;
1157 }; // namespace WTL
1159 #endif // __ATLUSER_H__