AlphaBlend.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // AlphaBlend.cpp: implementation of the CAlphaBlend class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. // By Mike Ryan ([email protected])
  5. // Copyright (c) 2000
  6. // 07.03.2000
  7. //
  8. // Free usage granted in all applications including commercial.
  9. // Do NOT distribute without permission from me. I can be reached
  10. // at [email protected], http://www.codexia.com
  11. // Please feel free to email me about this class.
  12. //
  13. // NOTE:
  14. //
  15. // You will need the latest Win32 API availble at:
  16. // http://msdn.microsoft.com/downloads/c-frame.htm?007#/downloads/sdks/
  17. // in order to compile this programs.
  18. //
  19. // This will only run under Windows 2000. It is not compatible with
  20. // Windows 9x or Windows NT 4.0.
  21. //
  22. // Also, you must add the following lines to the StdAfx.h at the top
  23. // above the #define VC_EXTRALEAN.
  24. //
  25. // #define _WIN32_WINNT 0x0500
  26. // #define WINVER 0x0500
  27. //
  28. //////////////////////////////////////////////////////////////////////
  29. #include "stdafx.h"
  30. #include "AlphaBlend.h"
  31. #ifdef _DEBUG
  32. #undef THIS_FILE
  33. static char THIS_FILE[]=__FILE__;
  34. #define new DEBUG_NEW
  35. #endif
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39. CAlphaBlend::CAlphaBlend()
  40. {
  41. m_hWnd = NULL;
  42. m_nOpacity = 0;
  43. m_bTransparent = false;
  44. }
  45. CAlphaBlend::~CAlphaBlend()
  46. {
  47. }
  48. CAlphaBlend::CAlphaBlend(HWND hWnd, int nOpactiy)
  49. {
  50. m_hWnd = NULL;
  51. m_nOpacity = 0;
  52. m_bTransparent = false;
  53. SetWindowHandle(hWnd);
  54. SetOpacity(nOpactiy);
  55. }
  56. CAlphaBlend::CAlphaBlend(CWnd *pWnd, int nOpacity)
  57. {
  58. m_hWnd = NULL;
  59. m_nOpacity = 0;
  60. m_bTransparent = false;
  61. SetWindowHandle(pWnd);
  62. SetOpacity(nOpacity);
  63. }
  64. BOOL CAlphaBlend::SetWindowHandle(HWND hWnd)
  65. {
  66. if (::IsWindow(hWnd)) m_hWnd = hWnd;
  67. else return false;
  68. return true;
  69. }
  70. BOOL CAlphaBlend::SetWindowHandle(CWnd *pWnd)
  71. {
  72. if (pWnd && ::IsWindow(pWnd->GetSafeHwnd())) m_hWnd = pWnd->GetSafeHwnd();
  73. else return false;
  74. return true;
  75. }
  76. BOOL CAlphaBlend::SetOpacity(int nOpacity)
  77. {
  78. if (nOpacity >= 0 && nOpacity <= OPACITY_MAX)
  79. {
  80. m_nOpacity = nOpacity;
  81. if (m_bTransparent)
  82. {
  83. // update the transparency
  84. ASSERT(::IsWindow(m_hWnd));
  85. SetLayeredWindowAttributes(m_hWnd, 0, m_nOpacity, LWA_ALPHA);
  86. }
  87. return true;
  88. }
  89. return false;
  90. }
  91. void CAlphaBlend::SetTransparent(BOOL bTransparent)
  92. {
  93. if (bTransparent)
  94. {
  95. // make sure they have set it up properly
  96. ASSERT(m_nOpacity >= 0 && m_nOpacity <= OPACITY_MAX);
  97. ASSERT(m_hWnd && ::IsWindow(m_hWnd));
  98. // make it transparent
  99. long l = GetWindowLong(m_hWnd, GWL_EXSTYLE);
  100. if(!(l & WS_EX_LAYERED))
  101. {
  102. l |= WS_EX_LAYERED;
  103. SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
  104. }
  105. SetLayeredWindowAttributes(m_hWnd, 0, m_nOpacity, LWA_ALPHA);
  106. CRect r;
  107. ::GetWindowRect(m_hWnd, r);
  108. ::InvalidateRect(m_hWnd, r, true);
  109. m_bTransparent = true;
  110. }
  111. else
  112. {
  113. long l = GetWindowLong(m_hWnd, GWL_EXSTYLE);
  114. if(l & WS_EX_LAYERED)
  115. {
  116. l ^= WS_EX_LAYERED;
  117. SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
  118. CRect r;
  119. ::GetWindowRect(m_hWnd, r);
  120. ::InvalidateRect(m_hWnd, r, true);
  121. }
  122. m_bTransparent = false;
  123. }
  124. }
  125. BOOL CAlphaBlend::SetTransparent(HWND hWnd, int nOpacity, BOOL bTransparent)
  126. {
  127. // set members
  128. if (!SetWindowHandle(hWnd)) return false;
  129. if (!SetOpacity(nOpacity)) return false;
  130. SetTransparent(bTransparent);
  131. return true;
  132. }