利用BMP通道和UpdateLayeredWindow制作边缘透明不规则窗口
2.填充黑色 因为Alpha通道黑色+前景色黑色时会完全透明
3.用滤镜做如下效果
4.选择全部区域并复制 增加Alpha通道
5.通道中只选Alpha通道 并粘贴选择区域
6.存盘为BMP格式 这是Alpha通道自动选择
7.BMP选项为Windows 32位
8.效果图 边沿透明不规则
说明:UpdateLayeredWindow函数做透明处理时有两个参数可以调整透明度
一个是BMP中的Alpha通道的参数,还有一个是BLENDFUNCTION结构中的SourceConstantAlpha参数
pre { color: rgba(32, 48, 64, 1); background-color: rgba(235, 243, 252, 1); border: 1px dashed rgba(47, 111, 171, 1); font-family: 宋体; font-size: 9pt; line-height: 1.2em; overflow: auto; padding: 0.2em; white-space: pre }
void CUpdateLayeredWindow_testDlg::Test() { char bmpFileName[] = {"./back.bmp"}; HWND hWnd = this->GetSafeHwnd(); typedef BOOL (WINAPI *pfnUpdateLayeredWindow)(HWND hWnd, HDC hdcDst, POINT *pptDst, SIZE *psize, HDC hdcSrc, POINT *pptSrc, COLORREF crKey, BLENDFUNCTION *pblend, DWORD dwFlags); pfnUpdateLayeredWindow s_UpdateLayeredWindow; HMODULE hSysDll = LoadLibrary(_T("USER32.DLL")); if(!hSysDll) return; s_UpdateLayeredWindow = (pfnUpdateLayeredWindow)GetProcAddress(hSysDll, "UpdateLayeredWindow"); if(!s_UpdateLayeredWindow) return; HBITMAP hbitmap = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),bmpFileName, IMAGE_BITMAP,0,0,LR_LOADFROMFILE); if(!hbitmap) return; DWORD dwExStyle = ::GetWindowLong(hWnd, GWL_EXSTYLE); if ((dwExStyle & 0x80000) != 0x80000) ::SetWindowLong(hWnd, GWL_EXSTYLE, dwExStyle ^ 0x80000); HDC hMemDC = CreateCompatibleDC(NULL); HBITMAP hOriBmp = (HBITMAP)SelectObject(hMemDC, hbitmap); BITMAP bi; CBitmap bm; bm.Attach(hbitmap); bm.GetBitmap(&bi); bm.Detach(); SetWindowPos(NULL,0,0,bi.bmWidth,bi.bmHeight,0); RECT WndRect; ::GetWindowRect(hWnd, &WndRect); POINT ptDst = {WndRect.left, WndRect.top}; POINT ptSrc = {0, 0}; SIZE WndSize = {WndRect.right-WndRect.left,WndRect.bottom-WndRect.top}; #define AC_SRC_ALPHA 0x01 #define ULW_ALPHA 0x00000002 BLENDFUNCTION blendPixelFunction= { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; BOOL bRet= s_UpdateLayeredWindow(m_hWnd, NULL, &ptDst, &WndSize, hMemDC, &ptSrc, 0, &blendPixelFunction, 2); SelectObject(hMemDC, hOriBmp); DeleteObject(hbitmap); DeleteDC(hMemDC); }