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