1 // Windows Template Library - WTL version 8.0
2 // Copyright (C) Microsoft Corporation. All rights reserved.
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.
15 #error ATL requires C++ compilation (use a .cpp suffix)
19 #error atluser.h requires atlapp.h to be included first
23 ///////////////////////////////////////////////////////////////////////////////
24 // Classes in this file:
28 // CAcceleratorT<t_bManaged>
30 // CCursorT<t_bManaged>
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
)
58 int nRes
= ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(message
.m_lpstr
), lpstrMessage
, nLen
);
61 delete [] lpstrMessage
;
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
)
79 int nRes
= ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(title
.m_lpstr
), lpstrTitle
, nLen
);
86 title
.m_lpstr
= lpstrTitle
;
89 int nRet
= ::MessageBox(hWndOwner
, message
.m_lpstr
, title
.m_lpstr
, uType
);
91 delete [] lpstrMessage
;
98 ///////////////////////////////////////////////////////////////////////////////
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))
105 #define MENUITEMINFO_SIZE_VERSION_400A MII_SIZEOF_STRUCT(MENUITEMINFOA, cch)
106 #define MENUITEMINFO_SIZE_VERSION_400W MII_SIZEOF_STRUCT(MENUITEMINFOW, cch)
108 #define MENUITEMINFO_SIZE_VERSION_400 MENUITEMINFO_SIZE_VERSION_400W
110 #define MENUITEMINFO_SIZE_VERSION_400 MENUITEMINFO_SIZE_VERSION_400A
112 #endif // (WINVER >= 0x0500)
114 class CMenuItemInfo
: public MENUITEMINFO
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
>
146 // Constructor/destructor/operators
147 CMenuT(HMENU hMenu
= NULL
) : m_hMenu(hMenu
)
152 if(t_bManaged
&& m_hMenu
!= NULL
)
156 CMenuT
<t_bManaged
>& operator =(HMENU hMenu
)
162 void Attach(HMENU hMenuNew
)
164 ATLASSERT(::IsMenu(hMenuNew
));
165 if(t_bManaged
&& m_hMenu
!= NULL
&& m_hMenu
!= hMenuNew
)
166 ::DestroyMenu(m_hMenu
);
172 HMENU hMenu
= m_hMenu
;
177 operator HMENU() const { return m_hMenu
; }
179 bool IsNull() const { return (m_hMenu
== NULL
); }
183 return ::IsMenu(m_hMenu
);
186 // Create/destroy methods
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
;
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
221 BOOL bRet
= ::DestroyMenu(m_hMenu
);
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
));
238 #if (WINVER >= 0x0500)
239 x
= _FixTrackMenuPopupX(x
, y
);
240 #endif // !(WINVER >= 0x0500)
241 return ::TrackPopupMenu(m_hMenu
, nFlags
, x
, y
, 0, hWnd
, lpRect
);
244 return ::TrackPopupMenuEx(m_hMenu
, nFlags
, x
, y
, hWnd
, NULL
);
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
)
262 HMONITOR hMonitor
= ::MonitorFromPoint(pt
, MONITOR_DEFAULTTONULL
);
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
)
274 else if(x
> mi
.rcWork
.right
)
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
);
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
);
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
);
375 BOOL
GetMenuString(UINT nIDItem
, BSTR
& bstrText
, UINT nFlags
) const
378 ATLASSERT(::IsMenu(m_hMenu
));
379 ATLASSERT(bstrText
== NULL
);
381 int nLen
= GetMenuStringLen(nIDItem
, nFlags
);
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
);
394 if(!GetMenuString(nIDItem
, lpszText
, nLen
, nFlags
))
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
);
412 nLen
++; // increment to include terminating NULL char
413 LPTSTR lpstr
= strText
.GetBufferSetLength(nLen
);
416 int nRet
= GetMenuString(nIDItem
, lpstr
, nLen
, nFlags
);
417 strText
.ReleaseBuffer();
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
);
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
);
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
);
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 ///////////////////////////////////////////////////////////////////////////////
564 template <bool t_bManaged
>
570 // Constructor/destructor/operators
571 CAcceleratorT(HACCEL hAccel
= NULL
) : m_hAccel(hAccel
)
576 if(t_bManaged
&& m_hAccel
!= NULL
)
577 ::DestroyAcceleratorTable(m_hAccel
);
580 CAcceleratorT
<t_bManaged
>& operator =(HACCEL hAccel
)
586 void Attach(HACCEL hAccel
)
588 if(t_bManaged
&& m_hAccel
!= NULL
)
589 ::DestroyAcceleratorTable(m_hAccel
);
595 HACCEL hAccel
= m_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
);
612 HACCEL
CreateAcceleratorTable(LPACCEL pAccel
, int cEntries
)
614 ATLASSERT(m_hAccel
== NULL
);
615 ATLASSERT(pAccel
!= NULL
);
616 m_hAccel
= ::CreateAcceleratorTable(pAccel
, cEntries
);
624 ::DestroyAcceleratorTable(m_hAccel
);
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 ///////////////////////////////////////////////////////////////////////////////
661 template <bool t_bManaged
>
667 // Constructor/destructor/operators
668 CIconT(HICON hIcon
= NULL
) : m_hIcon(hIcon
)
673 if(t_bManaged
&& m_hIcon
!= NULL
)
674 ::DestroyIcon(m_hIcon
);
677 CIconT
<t_bManaged
>& operator =(HICON hIcon
)
683 void Attach(HICON hIcon
)
685 if(t_bManaged
&& m_hIcon
!= NULL
)
686 ::DestroyIcon(m_hIcon
);
692 HICON hIcon
= m_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
);
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
);
717 HICON
LoadOEMIcon(LPCTSTR lpstrIconName
)
719 ATLASSERT(m_hIcon
== NULL
);
720 ATLASSERT(IsOEMIcon(lpstrIconName
));
721 m_hIcon
= ::LoadIcon(NULL
, lpstrIconName
);
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
);
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
);
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
);
750 #endif // !_WIN32_WCE
752 HICON
CreateIconIndirect(PICONINFO pIconInfo
)
754 ATLASSERT(m_hIcon
== NULL
);
755 ATLASSERT(pIconInfo
!= NULL
);
756 m_hIcon
= ::CreateIconIndirect(pIconInfo
);
761 HICON
ExtractIcon(LPCTSTR lpszExeFileName
, UINT nIconIndex
)
763 ATLASSERT(m_hIcon
== NULL
);
764 ATLASSERT(lpszExeFileName
!= NULL
);
765 m_hIcon
= ::ExtractIcon(ModuleHelper::GetModuleInstance(), lpszExeFileName
, nIconIndex
);
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
);
777 #endif // !_WIN32_WCE
781 ATLASSERT(m_hIcon
!= NULL
);
782 BOOL bRet
= ::DestroyIcon(m_hIcon
);
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
);
807 return ::DrawIcon(hDC
, x
, y
, m_hIcon
);
809 return ::DrawIconEx(hDC
, x
, y
, m_hIcon
, 0, 0, 0, NULL
, DI_NORMAL
);
813 BOOL
DrawIcon(HDC hDC
, POINT pt
)
815 ATLASSERT(m_hIcon
!= NULL
);
817 return ::DrawIcon(hDC
, pt
.x
, pt
.y
, m_hIcon
);
819 return ::DrawIconEx(hDC
, pt
.x
, pt
.y
, m_hIcon
, 0, 0, 0, NULL
, DI_NORMAL
);
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
);
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
);
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
);
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
));
879 return ::LoadIconWithScaleDown(NULL
, (LPCWSTR
)lpstrIconName
, cx
, cy
, &m_hIcon
);
881 #endif // defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
882 #endif // !_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 ///////////////////////////////////////////////////////////////////////////////
907 // protect template member from a winuser.h macro
912 template <bool t_bManaged
>
918 // Constructor/destructor/operators
919 CCursorT(HCURSOR hCursor
= NULL
) : m_hCursor(hCursor
)
924 if(t_bManaged
&& m_hCursor
!= NULL
)
928 CCursorT
<t_bManaged
>& operator =(HCURSOR hCursor
)
934 void Attach(HCURSOR hCursor
)
936 if(t_bManaged
&& m_hCursor
!= NULL
)
943 HCURSOR hCursor
= m_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
);
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
);
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
);
995 HCURSOR
LoadCursorFromFile(LPCTSTR pstrFilename
)
997 ATLASSERT(m_hCursor
== NULL
);
998 ATLASSERT(pstrFilename
!= NULL
);
999 m_hCursor
= ::LoadCursorFromFile(pstrFilename
);
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
);
1011 #endif // !defined(_WIN32_WCE) || ((_WIN32_WCE >= 0x400) && !(defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)))
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
);
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
);
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
);
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"));
1043 #endif // !(!defined(_WIN32_WCE) || ((_WIN32_WCE >= 0x400) && !(defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP))))
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.
1080 // Constructor/destructor
1081 CResource() : m_hGlobal(NULL
), m_hResource(NULL
)
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
)
1099 m_hGlobal
= ::LoadResource(ModuleHelper::GetResourceInstance(), m_hResource
);
1100 if(m_hGlobal
== NULL
)
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
)
1119 m_hGlobal
= ::LoadResource(ModuleHelper::GetResourceInstance(), m_hResource
);
1120 if(m_hGlobal
== NULL
)
1128 #endif // !_WIN32_WCE
1131 DWORD
GetSize() const
1133 ATLASSERT(m_hResource
!= NULL
);
1134 return ::SizeofResource(ModuleHelper::GetResourceInstance(), m_hResource
);
1139 ATLASSERT(m_hResource
!= NULL
);
1140 ATLASSERT(m_hGlobal
!= NULL
);
1141 LPVOID pVoid
= ::LockResource(m_hGlobal
);
1142 ATLASSERT(pVoid
!= NULL
);
1148 if(m_hGlobal
!= NULL
)
1150 FreeResource(m_hGlobal
);
1159 #endif // __ATLUSER_H__