Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.7 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 000
  5. *
  6. * File: smarticon.h
  7. *
  8. * Contents: Interface file for CSmartIcon
  9. *
  10. * History: 25-Jul-2000 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #pragma once
  14. #include <windows.h> // for HICON when building uicore.lib, which has no PCH
  15. #include "stddbg.h" // for ASSERT when building uicore.lib, which has no PCH
  16. /*+-------------------------------------------------------------------------*
  17. * class CSmartIcon
  18. *
  19. *
  20. * PURPOSE: A smart wrapper for icons. Destroys the icon when all references
  21. * to the icon are released.
  22. *
  23. *
  24. * USAGE: 1) Create the icon and assign to a smart icon:
  25. * smarticon.Attach(::CreateIcon(...));
  26. *
  27. * NOTE: The Attach method will destroy the icon if the underlying
  28. * CSmartIconData object cannot be created because of insufficient memory.
  29. *
  30. * 2) Smart icons can be treated as icons:
  31. * DrawIcon(..., smarticon, ...)
  32. *
  33. * 3) Smart icons can be assigned to one another just like handles:
  34. * smarticon1 = smarticon2;
  35. *
  36. *+-------------------------------------------------------------------------*/
  37. class CSmartIcon
  38. {
  39. public:
  40. CSmartIcon () : m_pData(NULL) {}
  41. ~CSmartIcon ();
  42. CSmartIcon (const CSmartIcon& other);
  43. CSmartIcon& operator= (const CSmartIcon& rhs);
  44. void Attach (HICON hIcon);
  45. HICON Detach (); // let go without decrementing ref count
  46. void Release (); // let go, decrementing ref count
  47. operator HICON() const
  48. {
  49. return m_pData
  50. ? m_pData->operator HICON()
  51. : NULL;
  52. }
  53. /*
  54. * for comparison to NULL (only)
  55. */
  56. bool operator==(int null) const
  57. {
  58. ASSERT (null == 0);
  59. return (operator HICON() == NULL);
  60. }
  61. bool operator!=(int null) const
  62. {
  63. ASSERT (null == 0);
  64. return (operator HICON() != NULL);
  65. }
  66. private:
  67. class CSmartIconData
  68. {
  69. HICON m_hIcon;
  70. DWORD m_dwRefs;
  71. CSmartIconData(HICON hIcon) : m_hIcon(hIcon), m_dwRefs(1) {}
  72. ~CSmartIconData()
  73. {
  74. if (m_hIcon != NULL)
  75. ::DestroyIcon (m_hIcon);
  76. }
  77. public:
  78. static CSmartIconData* CreateInstance(HICON hIcon) { return new CSmartIconData(hIcon); }
  79. operator HICON() const { return m_hIcon; }
  80. HICON Detach();
  81. void AddRef() {++m_dwRefs;}
  82. void Release()
  83. {
  84. if((--m_dwRefs)==0)
  85. delete this;
  86. }
  87. };
  88. private:
  89. CSmartIconData* m_pData;
  90. };