MFC背景透明

一:

typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
lpfnSetLayeredWindowAttributes SetLayeredWindowAttributes;   //設置成邊緣透明
COLORREF maskColor = RGB(240, 240, 240);
HMODULE hUser32 = GetModuleHandle(_T("user32.dll")); //加載動態連接庫
SetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");            //取得SetLayeredWindowAttributes函數指針            //爲窗口加入WS_EX_LAYERED擴展屬性
SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE)^WS_EX_LAYERED);             //調用SetLayeredWinowAttributes函數
/*SetLayeredWindowAttributes(this->GetSafeHwnd(), maskColor, 192, LWA_COLORKEY);  */
SetLayeredWindowAttributes(this->GetSafeHwnd(), maskColor, 255, LWA_ALPHA | LWA_COLORKEY);
FreeLibrary(hUser32);   //釋放動態鏈
//LWA_ALPHA時:crKey參數無效,bAlpha參數有效;
//LWA_COLORKEY:窗體中的全部顏色爲crKey的地方將變爲透明,bAlpha參數無效。
//LWA_ALPHA | LWA_COLORKEY:crKey的地方將變爲全透明,而其它地方根據bAlpha參數肯定透明度。

二:

COLORREF m_bgcolor = RGB(0, 0, 0);
BOOL m_bTransparent = ToggleTransparent(m_bgcolor, 100, LWA_ALPHA);
BOOL ToggleTransparent(COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
{
    BOOL bResult;
    LONG dwExStyle;
    dwExStyle = GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE);
    if (dwExStyle & WS_EX_LAYERED)
    {
        dwExStyle &= ~(WS_EX_LAYERED);
        bResult = FALSE;
    }
    else
    {
        dwExStyle |= (WS_EX_LAYERED);
        bResult = TRUE;
    }
    SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, dwExStyle);
    SetLayeredWindowAttributes( crKey, bAlpha, dwFlags);
    return bResult;
}
相關文章
相關標籤/搜索