利用BMP通道和UpdateLayeredWindow制作边缘透明不规则窗口
1.新建 颜色模式RGB颜色 背景内容为透明
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_001.png)
2.填充黑色 因为Alpha通道黑色+前景色黑色时会完全透明
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_002.png)
3.用滤镜做如下效果
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_003.png)
4.选择全部区域并复制 增加Alpha通道
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_004.png)
5.通道中只选Alpha通道 并粘贴选择区域
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_005.png)
6.存盘为BMP格式 这是Alpha通道自动选择
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_006.png)
7.BMP选项为Windows 32位
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_007.png)
8.效果图 边沿透明不规则
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_008.png)
说明:UpdateLayeredWindow函数做透明处理时有两个参数可以调整透明度
一个是BMP中的Alpha通道的参数,还有一个是BLENDFUNCTION结构中的SourceConstantAlpha参数
下载源码
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_001.png)
2.填充黑色 因为Alpha通道黑色+前景色黑色时会完全透明
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_002.png)
3.用滤镜做如下效果
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_003.png)
4.选择全部区域并复制 增加Alpha通道
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_004.png)
5.通道中只选Alpha通道 并粘贴选择区域
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_005.png)
6.存盘为BMP格式 这是Alpha通道自动选择
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_006.png)
7.BMP选项为Windows 32位
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_007.png)
8.效果图 边沿透明不规则
![](https://images.cnblogs.com/cnblogs_com/nlsoft/458525/o_008.png)
说明:UpdateLayeredWindow函数做透明处理时有两个参数可以调整透明度
一个是BMP中的Alpha通道的参数,还有一个是BLENDFUNCTION结构中的SourceConstantAlpha参数
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); }
下载源码