sakura/FadeTip

Last-modified: 2008-04-21 (月) 18:24:36

ツールチップのフェードイン・フェードアウト機能

最新版はFadeIn/FadeOut Classです。

CTipWndの差分ファイルです。
SetLayeredWindowAttributesが必要ですのでW2K以降となります。

diff -uNr orig/CTipWnd.cpp new/CTipWnd.cpp
--- orig/CTipWnd.cpp	Sun Dec 02 06:54:23 2007
+++ new/CTipWnd.cpp	Sat Apr 19 12:22:22 2008
@@ -19,6 +19,15 @@
 #include "CTipWnd.h"
 #include "CShareData.h"
+#define CTIPWND_EVENT_ID          1
+#define CTIPWND_FADE_TIMER        50
+#define CTIPWND_FADE_IN_INTERVAL  64
+#define CTIPWND_FADE_OUT_INTERVAL 16
+#ifndef WS_EX_LAYERED
+#define WS_EX_LAYERED           0x00080000
+#define LWA_COLORKEY            0x00000001
+#define LWA_ALPHA               0x00000002
+#endif	//WS_EX_LAYERED
 /* CTipWndクラス デストラクタ */
 CTipWnd::CTipWnd()
@@ -26,6 +35,15 @@
 	strcat( m_szClassInheritances, "::CTipWnd" );
 	m_pszClassName = "CTipWnd";
 	m_KeyWasHit = FALSE;	/* キーがヒットしたか */
+	m_uFadeTimerId = 0;
+	m_nOpacity = 0;
+	m_bNowFadeType = FALSE;
+
+	m_lpfncSetLayeredWindowAttributes = NULL;
+	HMODULE m_hUser32 = ::GetModuleHandle( _T("USER32.DLL") );
+	m_lpfncSetLayeredWindowAttributes = (FncSetLayeredWindowAttributes)::GetProcAddress( m_hUser32,
+               "SetLayeredWindowAttributes");
+
 	return;
 }
@@ -38,6 +56,12 @@
 		::DeleteObject( m_hFont );
 		m_hFont = NULL;
 	}
+
+	if( 0 != m_uFadeTimerId ){
+		::KillTimer( m_hWnd, m_uFadeTimerId );
+		m_uFadeTimerId = 0;
+	}
+
 	return;
 }
@@ -85,6 +109,7 @@
 	}
 	m_hFont = ::CreateFontIndirect( &(CShareData::getInstance()->GetShareData()->m_Common.m_lf_kh) );
+
 	return;
 }
@@ -127,7 +152,14 @@
 	::ReleaseDC( m_hWnd, hdc );
 	::MoveWindow( m_hWnd, nX, nY, rc.right + 8, rc.bottom + 8/*nHeight*/, TRUE );
+
+	::SetWindowLong( m_hWnd, GWL_EXSTYLE, ::GetWindowLong( m_hWnd, GWL_EXSTYLE ) | WS_EX_LAYERED );
+	m_bNowFadeType = TRUE;
+	m_uFadeTimerId = ::SetTimer( m_hWnd, CTIPWND_EVENT_ID, CTIPWND_FADE_TIMER, NULL );
+	OnTimer( m_hWnd, WM_TIMER, CTIPWND_EVENT_ID, 0 );
+
 	::ShowWindow( m_hWnd, SW_SHOWNA );
+
 	return;
 }
@@ -283,8 +315,14 @@
 /* Tipを消す */
 void CTipWnd::Hide( void )
 {
-	::ShowWindow( m_hWnd, SW_HIDE );
+//	::ShowWindow( m_hWnd, SW_HIDE );
 //	::DestroyWindow( m_hWnd );
+
+	::SetWindowLong( m_hWnd, GWL_EXSTYLE, ::GetWindowLong( m_hWnd, GWL_EXSTYLE ) | WS_EX_LAYERED );
+	m_bNowFadeType = FALSE;
+	m_uFadeTimerId = ::SetTimer( m_hWnd, CTIPWND_EVENT_ID, CTIPWND_FADE_TIMER, NULL );
+	OnTimer( m_hWnd, WM_TIMER, CTIPWND_EVENT_ID, 0 );
+
 	return;
 }
@@ -329,5 +367,35 @@
 // 2001/06/19 End
+LRESULT CTipWnd::OnTimer( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+{
+	switch( wParam ){
+	case CTIPWND_EVENT_ID:
+		if( m_bNowFadeType ){
+			if( (int)m_nOpacity + CTIPWND_FADE_IN_INTERVAL >= 255 ){
+				m_nOpacity = 255;
+				::KillTimer( m_hWnd, m_uFadeTimerId );
+				m_uFadeTimerId = 0;
+			}else{
+				m_nOpacity += CTIPWND_FADE_IN_INTERVAL;
+			}
+		}else{
+			if( (int)m_nOpacity - CTIPWND_FADE_OUT_INTERVAL <= 0 ){
+				m_nOpacity = 0;
+				::KillTimer( m_hWnd, m_uFadeTimerId );
+				m_uFadeTimerId = 0;
+				::ShowWindow( m_hWnd, SW_HIDE );
+			}else{
+				m_nOpacity -= CTIPWND_FADE_OUT_INTERVAL;
+			}
+		}
+
+		if( NULL != m_lpfncSetLayeredWindowAttributes ){
+			m_lpfncSetLayeredWindowAttributes( m_hWnd, NULL, m_nOpacity, LWA_ALPHA );
+		}
+		break;
+	}
+	return 0L;
+}
 /*[EOF]*/
diff -uNr orig/CTipWnd.h new/CTipWnd.h
--- orig/CTipWnd.h	Sun Dec 02 06:54:23 2007
+++ new/CTipWnd.h	Sat Apr 19 12:18:28 2008
@@ -52,6 +52,13 @@
 	char*		m_pszClassName;	/* Mutex作成用・ウィンドウクラス名 */
 	HFONT		m_hFont;
+	HMODULE m_hUser32;
+	typedef BOOL (WINAPI *FncSetLayeredWindowAttributes)( HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags );
+	FncSetLayeredWindowAttributes m_lpfncSetLayeredWindowAttributes;
+	UINT_PTR	m_uFadeTimerId;
+	BYTE		m_nOpacity;
+	BOOL		m_bNowFadeType;
+
 public:
 	CMemory		m_cKey;			/* キーの内容データ */
 	BOOL		m_KeyWasHit;	/* キーがヒットしたか */
@@ -73,6 +80,7 @@
 	/* 仮想関数 メッセージ処理 詳しくは実装を参照 */
 	LRESULT OnPaint( HWND, UINT, WPARAM, LPARAM );/* 描画処理 */
+	LRESULT OnTimer( HWND, UINT, WPARAM, LPARAM );
 };