绘制透明背景位图
2013-07-29 19:38
363 查看
// when blitting from a monochrome DC to a color DC, // the color black in the source turns into the destination's text color, // and the color white in the source turns into the destination's background color. // If you blit from a color DC to a monochrome DC, // then all pixels in the source that are equal to the background color will turn white, and all other pixels will turn black. // In other words, GDI considers a monochrome bitmap to be black pixels on a white background. static void DrawTransBmp( HDC hDst, HBITMAP hImg, COLORREF clrTran ) { COLORREF colorOldFront; COLORREF colorOldBg; BITMAP bmp; GetObject( hImg, sizeof( bmp ), &bmp ); HDC hForeDC = ::CreateCompatibleDC( hDst ); ::SelectObject ( hForeDC, hImg ); //prepare mask dc HDC hDCMask = CreateCompatibleDC ( hDst ); // MSDN: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. // If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. HBITMAP hBmpMask = CreateCompatibleBitmap( hDCMask, bmp.bmWidth, bmp.bmHeight ); ::SelectObject ( hDCMask, hBmpMask ); ::SetBkColor( hForeDC, clrTran ); //Set clrTran as blackground color ::BitBlt( hDCMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hForeDC, 0, 0, SRCCOPY ); //monochrome, background color is white //mask the foreground ::SetBkColor( hForeDC, RGB(0,0,0) ); ::SetTextColor( hForeDC, RGB(255,255,255) ); ::BitBlt( hForeDC, 0, 0, bmp.bmWidth, bmp.bmHeight, hDCMask, 0, 0, SRCAND ); //mask the background colorOldBg = ::SetBkColor( hDst, RGB(255,255,255) ); colorOldFront = ::SetTextColor( hDst, RGB(0,0,0) ); ::BitBlt( hDst, 0, 0, bmp.bmWidth, bmp.bmHeight, hDCMask, 0, 0, SRCAND ); //or foreground and background ::BitBlt( hDst, 0, 0, bmp.bmWidth, bmp.bmHeight, hForeDC, 0, 0, SRCPAINT ); //recover hDst color of background and foreground ::SetBkColor( hDst, colorOldBg ); ::SetTextColor( hDst, colorOldFront ); //release object ::DeleteDC( hDCMask ); ::DeleteDC( hForeDC ); ::DeleteObject( hBmpMask ); }
相关文章推荐
- 绘制透明背景位图
- 绘制透明背景位图
- 绘制透明背景位图
- MFC 绘制透明背景位图
- VC对话框如何添加WM_ERASEBKGND消息(OnEraseBkgnd函数)及对话框使用位图背景并透明
- 绘制半透明位图以及如何画透明位图
- VC对话框使用OnEraseBkgnd函数位图背景并透明
- 如何在CDC上绘制背景透明的图片
- MFC为子窗口绘制透明背景
- VC透明位图的绘制
- 使CBitmap位图对象背景透明显示
- 绘制透明的背景图
- VC对话框使用OnEraseBkgnd函数位图背景并透明
- VC 位图背景透明显示
- WM_ERASEBKGND消息及位图背景与透明的实现
- 自绘实现半透明水晶按钮(继承CButton,设置BS_OWNERDRAW风格,覆盖DrawItem函数绘制按钮,把父窗口的背景复制到按钮上,实现视觉上的透明,最后通过AlphaBlend实现半透明)
- [C#]桌面模式下GDI绘制(全透明背景)
- 透明背景图像绘制(C#)
- DirextX9之2D编程 通过16位色位图创建背景透明的纹理
- VC对话框如何添加WM_ERASEBKGND消息(OnEraseBkgnd函数)及对话框使用位图背景并透明