#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之类的开源图形库来实现。

最新文章

  1. [展示]手把手教你如何diy门户幻灯片
  2. Redis集群(三):主从配置一
  3. CSS实现多个Div等高,类似表格布局
  4. Verilog学习笔记简单功能实现(八)...............异步FIFO
  5. Spring 一二事(4) - 单例
  6. [tools]tcp/udp连通性测试
  7. 自定义PopupWindow弹出框(带有动画)
  8. Android开发_Gson解析
  9. python操作memcache
  10. 解决Webservice内存溢出
  11. 24点游戏&amp;&amp;速算24点(dfs)
  12. Liunx 环境下vsftpd的三种实现方法(超详细参数)
  13. Java常见算法整理
  14. Percona XtraBackup的部分备份与恢复/单库备份/单表备份/指定库备份/指定表备份
  15. Spring Boot 整合 rabbitmq
  16. 清除Linux日志文件命令
  17. Go语言中的map
  18. PHP 标准规范,PSR-1,PSR-2,PSR-3,PSR-4,PSR-5,PSR-6,PSR-7及其他标准
  19. Java发送QQ邮件
  20. jsp页面继承

热门文章

  1. DS博客大作业--树(李天明组)
  2. AndroidStudio下载安装教程(图文教程)
  3. 关于Android的资源id
  4. sysbench的简单安装
  5. java中package包
  6. 在linux下进行数据备份
  7. 第7章:LeetCode--算法:递归问题
  8. 【第一季】CH09_FPGA多路分频器设计
  9. WPF入门(2)——数据绑定与INotifyPropertyChanged(INPC)
  10. 怎样创建一个canvas画布环境