文章原始出處: http://www.codeguru.com
譯者:阿鬼 [[email protected]]
環境: VC6, win2000, winXP
轉載請與作者聯系
技術准備:
很簡單,就是使用 Windows API函數
SetLayeredWindowAttributes(HWND, COLORREF, BYTE,DWORD)
SetLayeredWindowAttributes函數在USER32.DLL中,你需要裝載該DLL並使用它。
第一步:
你必須改變窗口的樣式,將窗口變成具有圖層樣式,使用Windows API函數SetWindowLong。
在對話框窗體OnInitDialog()函數或者在文檔/視類型的窗體OnCreate()函數如下使用:
SetWindowLong(m_hWnd,
GWL_EXTYLE,
::GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
然後調用函數:
SetTransparent( m_hWnd, 0, 255 , LWA_ALPHA );
讓bAlpha的值為255,該函數如下:
// This function sets the transparency layered window
// by calling SetLayeredWindowAttributes API function.
BOOL SetTransparent(HWND hWnd, COLORREF crKey,
BYTE bAlpha, DWORD dwFlags)
{
BOOL bRet = TRUE;
typedef BOOL (WINAPI* lpfnSetTransparent)(HWND hWnd,
COLORREF crKey,
BYTE bAlpha,
DWORD dwFlags);
// Check that "USER32.dll" library has been
// loaded successfully...
if ( m_hUserDll )
{
lpfnSetTransparent pFnSetTransparent = NULL;
pFnSetTransparent =
(lpfnSetTransparent)GetProcAddress(m_hUserDll,
"SetLayeredWindowAttributes");
if (pFnSetTransparent )
bRet = pFnSetTransparent(hWnd, crKey, bAlpha, dwFlags);
else
bRet = FALSE;
} // if( m_hUserDll )
return bRet;
} // End of SetTransparent function
第二步:
在窗體OnClose()中調用CloseSmoothly()函數,該函數如下:
void CloseSmoothly()
{
// Increase transparency one percent each time...
for(int nPercent=100; nPercent >= 0 ;nPercent--)
SetTransparent( m_hWnd, 0, 255 * nPercent/100, LWA_ALPHA);
}