sakura/FadeClass

Last-modified: 2008-04-20 (日) 21:08:28

FadeIn/FadeOut Class

フェードイン効果をクラス化してみました。
ちょっとできが悪いです。

diff -uNr orig/CTipWnd.cpp new/CTipWnd.cpp
--- orig/CTipWnd.cpp	Sun Dec 02 06:54:23 2007
+++ new/CTipWnd.cpp	Sun Apr 20 21:06:21 2008
@@ -18,7 +18,7 @@
 #include "stdafx.h"
 #include "CTipWnd.h"
 #include "CShareData.h"
-
+#include "CTransparent.h"
 /* CTipWndクラス デストラクタ */
 CTipWnd::CTipWnd()
@@ -85,6 +85,9 @@
 	}
 	m_hFont = ::CreateFontIndirect( &(CShareData::getInstance()->GetShareData()->m_Common.m_lf_kh) );
+
+	m_cTransparent.SetHwnd( m_hWnd );
+
 	return;
 }
@@ -127,7 +130,13 @@
 	::ReleaseDC( m_hWnd, hdc );
 	::MoveWindow( m_hWnd, nX, nY, rc.right + 8, rc.bottom + 8/*nHeight*/, TRUE );
-	::ShowWindow( m_hWnd, SW_SHOWNA );
+
+	if( m_cTransparent.CanUse() ){
+		m_cTransparent.FadeIn( FALSE, SW_SHOWNA );
+	}else{
+		::ShowWindow( m_hWnd, SW_SHOWNA );
+	}
+
 	return;
 }
@@ -283,8 +292,15 @@
 /* Tipを消す */
 void CTipWnd::Hide( void )
 {
-	::ShowWindow( m_hWnd, SW_HIDE );
+	if( ::IsWindowVisible( m_hWnd ) ){
+		if( m_cTransparent.CanUse() ){
+			m_cTransparent.FadeOut( FALSE );
+		}else{
+			::ShowWindow( m_hWnd, SW_HIDE );
+		}
+	}
 //	::DestroyWindow( m_hWnd );
+
 	return;
 }
@@ -329,5 +345,9 @@
 // 2001/06/19 End
+LRESULT CTipWnd::OnTimer( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+{
+	return m_cTransparent.OnTimer( hWnd, uMsg, wParam, lParam );
+}
 /*[EOF]*/
diff -uNr orig/CTipWnd.h new/CTipWnd.h
--- orig/CTipWnd.h	Sun Dec 02 06:54:23 2007
+++ new/CTipWnd.h	Sun Apr 20 20:43:19 2008
@@ -21,6 +21,8 @@
 #include "CWnd.h"
 #include "CMemory.h"
+#include "CTransparent.h"
+
 /*-----------------------------------------------------------------------
 クラスの宣言
 -----------------------------------------------------------------------*/
@@ -52,6 +54,8 @@
 	char*		m_pszClassName;	/* Mutex作成用・ウィンドウクラス名 */
 	HFONT		m_hFont;
+	CTransparent	m_cTransparent;
+
 public:
 	CMemory		m_cKey;			/* キーの内容データ */
 	BOOL		m_KeyWasHit;	/* キーがヒットしたか */
@@ -73,6 +77,7 @@
 	/* 仮想関数 メッセージ処理 詳しくは実装を参照 */
 	LRESULT OnPaint( HWND, UINT, WPARAM, LPARAM );/* 描画処理 */
+	LRESULT OnTimer( HWND, UINT, WPARAM, LPARAM );
 };
diff -uNr orig/CTransparent.cpp new/CTransparent.cpp
--- orig/CTransparent.cpp	Thu Jan 01 09:00:00 1970
+++ new/CTransparent.cpp	Sun Apr 20 21:02:46 2008
@@ -0,0 +1,180 @@
+/*!	@file
+	@brief 透過
+
+	@author wakura
+	@date 2008.04.20
+*/
+/*
+	Copyright (C) 2008, wakura
+
+	This software is provided 'as-is', without any express or implied
+	warranty. In no event will the authors be held liable for any damages
+	arising from the use of this software.
+
+	Permission is granted to anyone to use this software for any purpose,
+	including commercial applications, and to alter it and redistribute it
+	freely, subject to the following restrictions:
+
+		1. The origin of this software must not be misrepresented;
+		   you must not claim that you wrote the original software.
+		   If you use this software in a product, an acknowledgment
+		   in the product documentation would be appreciated but is
+		   not required.
+
+		2. Altered source versions must be plainly marked as such,
+		   and must not be misrepresented as being the original software.
+
+		3. This notice may not be removed or altered from any source
+		   distribution.
+*/
+
+#include "stdafx.h"
+#include "CTransparent.h"
+
+CTransparent::CTransparent()
+{
+	m_cOpacity = 0;
+	m_bFadeType = FALSE;
+	m_nIDEvent = ID_EVENT;
+	m_nTimerHandle = 0;
+
+	m_hWnd = NULL;
+	m_uElapse = FADE_ELAPSE;
+	m_cFadeInInterval = FADE_IN_INTERVAL;
+	m_cFadeOutInterval = FADE_OUT_INTERVAL;
+
+	m_hUser32 = NULL;
+	m_lpfncSetLayeredWindowAttributes = NULL;
+	m_hUser32 = ::LoadLibrary( _T("USER32.DLL") );
+	if( NULL != m_hUser32 ){
+		m_lpfncSetLayeredWindowAttributes = (FncSetLayeredWindowAttributes)::GetProcAddress( m_hUser32, "SetLayeredWindowAttributes" );
+	}
+}
+
+CTransparent::~CTransparent()
+{
+	if( NULL != m_nTimerHandle ){
+		::KillTimer( m_hWnd, m_nTimerHandle );
+		m_nTimerHandle = 0;
+	}
+	if( NULL != m_hUser32 ){
+		m_lpfncSetLayeredWindowAttributes = NULL;
+		::FreeLibrary( m_hUser32 );
+		m_hUser32 = NULL;
+	}
+}
+
+void CTransparent::SetHwnd( HWND hWnd )
+{
+	m_hWnd = hWnd;
+	::SetWindowLong( m_hWnd, GWL_EXSTYLE, ::GetWindowLong( m_hWnd, GWL_EXSTYLE ) | WS_EX_LAYERED );
+}
+
+BOOL CTransparent::SetTransparent( BYTE cOpacity )
+{
+	m_cOpacity = cOpacity;
+
+	if( ! CanUse() ) return FALSE;
+
+	if( NULL != m_lpfncSetLayeredWindowAttributes ){
+		m_lpfncSetLayeredWindowAttributes( m_hWnd, NULL, m_cOpacity, LWA_ALPHA );
+	}
+
+	return TRUE;
+}
+
+/*!
+	フェードイン
+
+	@param[in] bOption	フェード効果オプション
+
+	@note
+	bOption
+		FALSE	フェード効果は前回の続き
+		TRUE	フェード効果はリセットして最初から実行
+*/
+BOOL CTransparent::FadeIn( BOOL bOption, int nCmdShow )
+{
+	if( ! CanUse() ) return FALSE;
+
+	m_bFadeType = TRUE;
+	FadeStop( bOption );
+	m_nTimerHandle = ::SetTimer( m_hWnd, m_nIDEvent, m_uElapse, NULL );
+	OnTimer( m_hWnd, WM_TIMER, m_nIDEvent, 0 );
+
+	::ShowWindow( m_hWnd, nCmdShow );
+
+	return TRUE;
+}
+
+/*!
+	フェードアウト
+
+	@param[in] bOption	フェード効果オプション
+
+	@note
+	bOption
+		FALSE	フェード効果は前回の続き
+		TRUE	フェード効果はリセットして最初から実行
+*/
+BOOL CTransparent::FadeOut( BOOL bOption, int nCmdShow )
+{
+	if( ! CanUse() ) return FALSE;
+
+	m_bFadeType = FALSE;
+	FadeStop( bOption );
+	m_nTimerHandle = ::SetTimer( m_hWnd, m_nIDEvent, m_uElapse, NULL );
+	OnTimer( m_hWnd, WM_TIMER, m_nIDEvent, 0 );
+
+	return TRUE;
+}
+
+void CTransparent::FadeStop( BOOL bOption )
+{
+	if( ! CanUse() ) return;
+	if( ! IsRunning() ) return;
+
+	::KillTimer( NULL, m_nIDEvent );
+	m_nTimerHandle = 0;
+
+	if( bOption ){
+		if( IsFadeOut() ){
+			SetTransparent( 0 );
+		}else{
+			SetTransparent( 255 );
+		}
+	}
+}
+
+LRESULT CTransparent::OnTimer( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+{
+	if( hWnd   != m_hWnd
+	 || uMsg   != WM_TIMER
+	 || wParam != m_nIDEvent ) return 0L;
+	if( ! IsRunning() ) return 0L;
+
+	if( IsFadeOut() ){
+		if( (int)m_cOpacity - m_cFadeOutInterval <= 0 ){
+			m_cOpacity = 0;
+			::KillTimer( NULL, m_nIDEvent );
+			m_nTimerHandle = 0;
+			SetTransparent( m_cOpacity );
+			::ShowWindow( m_hWnd, SW_HIDE );
+		}else{
+			m_cOpacity -= m_cFadeOutInterval;
+			SetTransparent( m_cOpacity );
+		}
+	}else{
+		if( (int)m_cOpacity + m_cFadeInInterval >= 255 ){
+			m_cOpacity = 255;
+			::KillTimer( NULL, m_nIDEvent );
+			m_nTimerHandle = 0;
+			SetTransparent( m_cOpacity );
+		}else{
+			m_cOpacity += m_cFadeInInterval;
+			SetTransparent( m_cOpacity );
+		}
+	}
+
+	return 0L;
+}
diff -uNr orig/CTransparent.h new/CTransparent.h
--- orig/CTransparent.h	Thu Jan 01 09:00:00 1970
+++ new/CTransparent.h	Sun Apr 20 20:59:53 2008
@@ -0,0 +1,90 @@
+/*!	@file
+	@brief 透過
+
+	@author wakura
+	@date 2008.04.20
+*/
+/*
+	Copyright (C) 2008, wakura
+
+	This software is provided 'as-is', without any express or implied
+	warranty. In no event will the authors be held liable for any damages
+	arising from the use of this software.
+
+	Permission is granted to anyone to use this software for any purpose,
+	including commercial applications, and to alter it and redistribute it
+	freely, subject to the following restrictions:
+
+		1. The origin of this software must not be misrepresented;
+		   you must not claim that you wrote the original software.
+		   If you use this software in a product, an acknowledgment
+		   in the product documentation would be appreciated but is
+		   not required.
+
+		2. Altered source versions must be plainly marked as such,
+		   and must not be misrepresented as being the original software.
+
+		3. This notice may not be removed or altered from any source
+		   distribution.
+*/
+#pragma once
+
+#include "stdafx.h"
+#include "debug.h"
+
+//#if(_WIN32_WINNT >= 0x0500)
+#ifndef WS_EX_LAYERED
+#define WS_EX_LAYERED 0x00080000
+#define LWA_COLORKEY  0x00000001
+#define LWA_ALPHA     0x00000002
+//WINUSERAPI BOOL WINAPI SetLayeredWindowAttributes( HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags );
+#endif	//WS_EX_LAYERED
+//#endif //_WIN32_WINNT >= 0x0500
+
+/*!
+	透過クラス
+*/
+class CTransparent {
+public:
+	static const UINT ID_EVENT = 255;
+	static const BYTE FADE_IN_INTERVAL = 256 / 2;
+	static const BYTE FADE_OUT_INTERVAL = 256 / 16;
+	static const UINT FADE_ELAPSE = 20;
+
+private:
+	BYTE m_cOpacity;
+	BOOL m_bFadeType;
+	UINT m_nIDEvent;
+	UINT_PTR m_nTimerHandle;
+	HMODULE m_hUser32;
+	typedef BOOL (WINAPI *FncSetLayeredWindowAttributes)( HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags );
+	FncSetLayeredWindowAttributes m_lpfncSetLayeredWindowAttributes;
+
+	HWND m_hWnd;
+	UINT m_uElapse;
+	BYTE m_cFadeInInterval;
+	BYTE m_cFadeOutInterval;
+
+public:
+	CTransparent();
+	virtual ~CTransparent();
+
+	void SetHwnd( HWND hWnd );
+	void SetElapse( UINT uElapse ){ m_uElapse = uElapse; }
+	void SetFadeInInterval( BYTE cFadeInInterval ){ m_cFadeInInterval = cFadeInInterval; }
+	void SetFadeOutInterval( BYTE cFadeOutInterval ){ m_cFadeOutInterval = cFadeOutInterval; }
+	BOOL SetTransparent( BYTE cOpacity );
+	BOOL FadeIn( BOOL bOption = FALSE, int nCmdShow = SW_SHOW );
+	BOOL FadeOut( BOOL bOption = FALSE, int nCmdShow = SW_HIDE );
+	void FadeStop( BOOL bOption = FALSE );
+
+	UINT GetElapse( void ){ return m_uElapse; }
+	BYTE GetFadeInInterval( void ){ return m_cFadeInInterval; }
+	BYTE GetFadeOutInterval( void ){ return m_cFadeOutInterval; }
+	BYTE GetOpacity( void ){ return m_cOpacity; }
+	BOOL IsFadeIn( void ){ return m_bFadeType; }
+	BOOL IsFadeOut( void ){ return ! m_bFadeType; }
+	BOOL IsRunning( void ){ return m_nTimerHandle != 0; }
+	BOOL CanUse( void ){ return m_lpfncSetLayeredWindowAttributes != NULL; }
+	LRESULT OnTimer( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
+};