AlphaBlend.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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()))
  73. m_hWnd = pWnd->GetSafeHwnd();
  74. else
  75. return false;
  76. return true;
  77. }
  78. BOOL CAlphaBlend::SetOpacity(int nOpacity)
  79. {
  80. if (nOpacity >= 0 && nOpacity <= OPACITY_MAX)
  81. {
  82. m_nOpacity = nOpacity;
  83. if (m_bTransparent)
  84. {
  85. // update the transparency
  86. ASSERT(::IsWindow(m_hWnd));
  87. SetLayeredWindowAttributesEx(m_hWnd, 0, m_nOpacity, LWA_ALPHA);
  88. }
  89. return true;
  90. }
  91. return false;
  92. }
  93. BOOL CAlphaBlend::SetLayeredWindowAttributesEx(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
  94. {
  95. BOOL bRet = FALSE;
  96. typedef BOOL (CALLBACK* fnc)(HWND, COLORREF, BYTE, DWORD);
  97. HINSTANCE DLL;
  98. fnc setLayeredWindowAttributes;
  99. DLL = LoadLibrary(_T("user32.dll"));
  100. if(DLL != NULL)
  101. {
  102. setLayeredWindowAttributes = (fnc)GetProcAddress(DLL,"SetLayeredWindowAttributes");
  103. if(setLayeredWindowAttributes)
  104. {
  105. bRet = setLayeredWindowAttributes(hwnd, crKey, bAlpha, dwFlags);
  106. }
  107. FreeLibrary(DLL);
  108. }
  109. return bRet;
  110. }
  111. void CAlphaBlend::SetTransparent(BOOL bTransparent)
  112. {
  113. if (bTransparent)
  114. {
  115. // make sure they have set it up properly
  116. ASSERT(m_nOpacity >= 0 && m_nOpacity <= OPACITY_MAX);
  117. ASSERT(m_hWnd && ::IsWindow(m_hWnd));
  118. // make it transparent
  119. long l = GetWindowLong(m_hWnd, GWL_EXSTYLE);
  120. if(!(l & WS_EX_LAYERED))
  121. {
  122. l |= WS_EX_LAYERED;
  123. SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
  124. }
  125. SetLayeredWindowAttributesEx(m_hWnd, 0, m_nOpacity, LWA_ALPHA);
  126. CRect r;
  127. ::GetWindowRect(m_hWnd, r);
  128. ::InvalidateRect(m_hWnd, r, true);
  129. m_bTransparent = true;
  130. }
  131. else
  132. {
  133. long l = GetWindowLong(m_hWnd, GWL_EXSTYLE);
  134. if(l & WS_EX_LAYERED)
  135. {
  136. l ^= WS_EX_LAYERED;
  137. SetWindowLong(m_hWnd, GWL_EXSTYLE, l);
  138. CRect r;
  139. ::GetWindowRect(m_hWnd, r);
  140. ::InvalidateRect(m_hWnd, r, true);
  141. }
  142. m_bTransparent = false;
  143. }
  144. }
  145. BOOL CAlphaBlend::SetTransparent(HWND hWnd, int nOpacity, BOOL bTransparent)
  146. {
  147. // set members
  148. if (!SetWindowHandle(hWnd))
  149. return false;
  150. if (!SetOpacity(nOpacity))
  151. return false;
  152. SetTransparent(bTransparent);
  153. return true;
  154. }