GDI根据位图和透明度创建蒙版
2024-09-05 08:06:18
#include <windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); HBITMAP g_bmp;
HBITMAP g_bmpMask; HBITMAP createImageMask(HBITMAP bitmapHandle, const COLORREF transparencyColor) {
// For getting information about the bitmap's height and width in this context
BITMAP bitmap; // Create the device contexts for the bitmap and its mask
HDC bitmapGraphicsDeviceContext = CreateCompatibleDC(NULL);
HDC bitmapMaskGraphicsDeviceContext = CreateCompatibleDC(NULL); // The actual mask
HBITMAP bitmapMaskHandle; // 1. Generate the mask.
GetObject(bitmapHandle, sizeof(BITMAP), &bitmap);
bitmapMaskHandle = CreateBitmap(bitmap.bmWidth, bitmap.bmHeight, , , NULL); // 2. Setup the device context for the mask (and the bitmap).
SelectObject(bitmapGraphicsDeviceContext, bitmapHandle);
SelectObject(bitmapMaskGraphicsDeviceContext, bitmapMaskHandle); // 3. Set the background color of the mask.
SetBkColor(bitmapGraphicsDeviceContext, transparencyColor); // 4. Copy the bitmap to the mask and invert it so it blends with the background color.
BitBlt(bitmapMaskGraphicsDeviceContext, , , bitmap.bmWidth, bitmap.bmHeight, bitmapGraphicsDeviceContext, , , SRCCOPY);
BitBlt(bitmapGraphicsDeviceContext, , , bitmap.bmWidth, bitmap.bmHeight, bitmapMaskGraphicsDeviceContext, , , SRCINVERT); // Clean-up
DeleteDC(bitmapGraphicsDeviceContext);
DeleteDC(bitmapMaskGraphicsDeviceContext); // Voila!
return bitmapMaskHandle;
} int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ MSG msg = { };
WNDCLASS wc = { };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"minwindowsapp"; g_bmp = (HBITMAP)LoadImage(hInstance, L"C:\\Users\\h2fM6d9.bmp", IMAGE_BITMAP, , , LR_LOADFROMFILE);
g_bmpMask = createImageMask(g_bmp, RGB(, , )); if (!RegisterClass(&wc))
return ; if (!CreateWindow(wc.lpszClassName,
L"Minimal Windows Application",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
, , , , , , hInstance, NULL))
return ; while (GetMessage(&msg, NULL, , ) > ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return ;
} LRESULT HandleWmPaint(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps; HDC hdcScr = GetDC(NULL);
HDC hdcBmp = CreateCompatibleDC(hdcScr);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcBmp, g_bmp); HDC hdcMask = CreateCompatibleDC(hdcScr);
HBITMAP hbmOldMask = (HBITMAP)SelectObject(hdcMask, g_bmpMask); HDC hdc = BeginPaint(hWnd, &ps);
BitBlt(hdc, , , , , hdcMask, , , SRCCOPY);
BitBlt(hdc, , , , , hdcBmp, , , SRCCOPY);
EndPaint(hWnd, &ps); SelectObject(hdcMask, hbmOldMask);
DeleteDC(hdcMask); SelectObject(hdcBmp, hbmOld);
DeleteDC(hdcBmp);
ReleaseDC(NULL, hdcScr); return ;
} LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ switch (message)
{
case WM_CLOSE:
PostQuitMessage();
break; case WM_PAINT:
return HandleWmPaint(hWnd, wParam, lParam); default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
}
顺便说一句,这项技术是一种非常古老的实现方法,在GDI中引入的MaskBlt 可以取代它,它可以在一个调用中完成您想要的操作。但更进一步,MaskBlt在这一点上已经过时了。如果是想为游戏或类似游戏的游戏绘制精灵。实际上可能想要的是使用每个像素的Alpha加载PNG,并使用Alpha合成对其进行绘制。也可以使用GDI +或诸如FreeImage之类的开源图形库来实现。
最新文章
- [展示]手把手教你如何diy门户幻灯片
- Redis集群(三):主从配置一
- CSS实现多个Div等高,类似表格布局
- Verilog学习笔记简单功能实现(八)...............异步FIFO
- Spring 一二事(4) - 单例
- [tools]tcp/udp连通性测试
- 自定义PopupWindow弹出框(带有动画)
- Android开发_Gson解析
- python操作memcache
- 解决Webservice内存溢出
- 24点游戏&;&;速算24点(dfs)
- Liunx 环境下vsftpd的三种实现方法(超详细参数)
- Java常见算法整理
- Percona XtraBackup的部分备份与恢复/单库备份/单表备份/指定库备份/指定表备份
- Spring Boot 整合 rabbitmq
- 清除Linux日志文件命令
- Go语言中的map
- PHP 标准规范,PSR-1,PSR-2,PSR-3,PSR-4,PSR-5,PSR-6,PSR-7及其他标准
- Java发送QQ邮件
- jsp页面继承