标签:des style blog io ar color 使用 sp on
最开始写透明窗口的代码,在百度了之后,找到了SetLayeredWindowAttributes()这一个函数,但是由于网上案列的缺少,使得很多人无法很好的使用这一个方法,我花了几天的时间写了一个实现窗口透明的类,在这里放出来,给大家使用
#define WS_EX_LAYERED 0x00080000 #define LWA_COLORKEY 0x00000001 #define LWA_ALPHA 0x00000002 class CTransparent { public: static BOOL SetTransparentWnd(HWND hWnd, COLORREF crKey, UINT cAlpha, BOOL dwFlags); static BOOL FreeUser32DLL(); static BOOL ChangeTransparency(HWND hWnd,COLORREF crKey, UINT bAlpha, DWORD dwFlags); static BOOL LoadUser32DLL(); static TRANFUNC m_pFunc; static HINSTANCE m_hInstance; static BOOL initialized; }; HINSTANCE CTransparent::m_hInstance = NULL; TRANFUNC CTransparent::m_pFunc = NULL; BOOL CTransparent::initialized = FALSE; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// BOOL CTransparent::LoadUser32DLL() { BOOL bRet = FALSE; if (!initialized) { m_hInstance = LoadLibrary(L"User32.DLL"); if(m_hInstance ) { //取得SetLayeredWindowAttributes函数指针 m_pFunc=(TRANFUNC)GetProcAddress(m_hInstance, "SetLayeredWindowAttributes"); if(m_pFunc != NULL) { initialized = TRUE; bRet = TRUE; } } } return bRet ; } BOOL CTransparent::ChangeTransparency(HWND hWnd, COLORREF crKey, UINT bAlpha, DWORD dwFlags) { //透明度Sp的值是0-255 //其中0为完全透明应该避免出现 BOOL bRet = FALSE; if (m_pFunc == NULL) LoadUser32DLL(); if(m_pFunc != NULL) { m_pFunc(hWnd, crKey, bAlpha, dwFlags); bRet = TRUE; } return bRet; } BOOL CTransparent::FreeUser32DLL() { if(m_hInstance != NULL) { FreeLibrary(m_hInstance); if(m_pFunc != NULL) //置空 m_pFunc = NULL ; if(initialized != NULL) //置空 initialized = NULL ; } return TRUE; } BOOL CTransparent::SetTransparentWnd(HWND hWnd, COLORREF crKey, UINT cAlpha, BOOL dwFlags) { BOOL bRet = TRUE; //要使使窗体拥有透明效果,首先要有WS_EX_LAYERED扩展属性 WS_EX_LAYERED = 0x80000 //SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000); SetWindowLong(hWnd, GWL_EXSTYLE , GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); if(dwFlags) { ChangeTransparency(hWnd, crKey, cAlpha, LWA_ALPHA); //设置cAlpha值0-255 } else { ChangeTransparency(hWnd, crKey, cAlpha, LWA_COLORKEY); //crkey相同色变成透明 } return bRet; }
HWND m_hMyWnd = ::CreateWindow(L"LoginWndDLL", L"TraWnd", WS_POPUP, (int)((xRes - m_WndWidth) / 2), (int)((yRes - m_WndHeight) / 2), m_WndWidth, m_WndHeight, 0, 0, hInst, 0); //在这里注册一个窗口第三个参数表示窗口的类型 CTransparent::SetTransparentWnd((HWND)m_hMyWnd, 0xffffff, 200, 2); //设置窗口透明 ::ShowWindow(m_hMyWnd, SW_SHOW);实际上上面这个类使用了动态调用user32.dll中的SetLayeredWindowAttributes函数,通过这个函数来实现窗口透明方式,这个方法与网上的大多数方法类似,不过是加了一个类进行了封装
标签:des style blog io ar color 使用 sp on
原文地址:http://blog.csdn.net/u011822516/article/details/41946631