原作者:Mohammad A. Salah. [Mohd_a_salah@hotmail.com] 文章原始出处: http://www.codeguru.com 译者:阿鬼 [mornlee@21cn.com] 环境: 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); } |