學習繪圖文本,實作文本漸隱效果,想用alphablend來實作漸隱,但是遇到個問題,下面兩種呼叫方式,其中一個方法可行,一個方法不可行。
方式一:
hBitmapDC = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc,w,h);
SelectObject(hBitmapDC,hBitmap);
SetTextColor(hBitmapDC,RGB(255,0,0));
SetBkColor(hBitmapDC,RGB(0,0xaa,0));
TextOut(hBitmapDC,20,50,szText,strlenT(szText));
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 120;
bf.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(hdc,0,100,w,h,hBitmapDC,0,0,w,h,bf);
DeleteObject(hBitmap);
DeleteDC( hBitmapDC );
效果如圖:

方式二:
hBitmapDC = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hBitmapDC,w,h);//不可行
//hBitmapDC = CreateCompatibleDC(NULL);
//hBitmap = CreateCompatibleBitmap(hBitmapDC,w,h);//不可行
SelectObject(hBitmapDC,hBitmap);
SetTextColor(hBitmapDC,RGB(255,0,0));
SetBkMode(hBitmapDC,OPAQUE);
SetBkColor(hBitmapDC,RGB(0,0xaa,0));
TextOut(hBitmapDC,20,50,szText,strlenT(szText));
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 120;
bf.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(hdc,0,100,w,h,hBitmapDC,0,0,w,h,bf);
DeleteObject(hBitmap);
DeleteDC( hBitmapDC );
效果如圖:

這是為什么呢,CreateCompatibleBitmap 不能使用 compatibleDC嗎?
請高手指教

uj5u.com熱心網友回復:
終于找到一個可行方式BITMAPINFOHEADER bih;
memset(&bih, 0, sizeof(BITMAPINFOHEADER));
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biHeight = h;
bih.biWidth = w;
bih.biPlanes = 1;
bih.biBitCount = 32;
bih.biCompression = BI_RGB;
BYTE* m_pBits = NULL;
hBitmapDC = CreateCompatibleDC(NULL);//可行
//hBitmapDC = CreateCompatibleDC(hdc);//可行
hBitmap = ::CreateDIBSection(hBitmapDC, (BITMAPINFO*)&bih,
DIB_RGB_COLORS, (void**)(&m_pBits), NULL, 0);
//hBitmapDC = CreateCompatibleDC(hdc);
//hBitmap = CreateCompatibleBitmap(hBitmapDC,w,h);//不可行
//hBitmapDC = CreateCompatibleDC(NULL);
//hBitmap = CreateCompatibleBitmap(hBitmapDC,w,h);//不可行
SelectObject(hBitmapDC,hBitmap);
SetTextColor(hBitmapDC,RGB(255,0,0));
SetBkMode(hBitmapDC,OPAQUE);
SetBkColor(hBitmapDC,RGB(0,0xaa,0));
TextOut(hBitmapDC,20,50,szText,strlenT(szText));
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 20;
bf.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(hdc,0,100,w,h,hBitmapDC,0,0,w,h,bf);
DeleteObject(hBitmap);
DeleteDC( hBitmapDC );
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/72732.html
上一篇:編程小菜鳥求幫助
