zoukankan      html  css  js  c++  java
  • 绘制透明背景位图

    // 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 );
    }
    


  • 相关阅读:
    MondoDB 之 使用python对MongoDB排序 MondoDB -9
    MondoDB 之 使用python对MongoDB进行文档修改 MondoDB -8
    MondoDB 之 使用python对MongoDB进行查询文档 MondoDB -7
    MondoDB 之 使用python对MongoDB进行插入文档 MondoDB -6
    MondoDB 之 使用python操作MongoDB MondoDB -5
    MondoDB 之 $关键字 及 $修改器 $set $inc $push $pull $pop MondoDB -4
    MondoDB 之 数据类型 MondoDB -3
    MongoDB 之 操作增删改查 MongoDB
    简述常见数据库高可用方案
    关于MySql数据库误操作数据找回的办法
  • 原文地址:https://www.cnblogs.com/riskyer/p/3223593.html
Copyright © 2011-2022 走看看