图片操作(一)---半透明效果 AlphaBlend

需求, 显示一个图片, 然后再图片上面蒙上一个半透明的图.

BOOL bRet; // Return value HBITMAP hBitmap; // HBITMAP HBITMAP hOldBitmap; // Old HBITMAP BITMAP bitmap; // The bitmap INT iPicWidth; // The width of the DC INT iPicHeight; // The height of the DC HDC hdcMem; // Temp DC // Initial data here bRet = FALSE; hBitmap = NULL; hOldBitmap = NULL; hdcMem = NULL; iPicWidth = 0; iPicHeight = 0; SecureZeroMemory(&bitmap, sizeof(bitmap)); // Load bitmap hBitmap = SHLoadImageFile(wszPicPath); if(NULL == hBitmap) { GetLastError(); goto Err; } // Get the attributes of the bitmap if(!GetObject(hBitmap, sizeof (BITMAP), &bitmap)) { goto Err; } iPicWidth = bitmap.bmWidth; iPicHeight = bitmap.bmHeight; // Create Compatible DC hdcMem = CreateCompatibleDC(NULL); if(!hdcMem) { goto Err; } // Select Object hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap); if(!hOldBitmap) { goto Err; } if (NULL == m_Flicker.m_hDC) { goto Err; } // Display the bitmap if(!StretchBlt(m_Flicker.m_hDC, 0, 0, WVGA_WIDTH, WVGA_HIGHT, hdcMem, 0, 0, iPicWidth, iPicHeight, SRCCOPY)) { goto Err; } HDC hDcMemFrame = NULL; hDcMemFrame = CreateCompatibleDC(NULL); if (NULL == hDcMemFrame) { goto Err; } HDC hdcDev = GetDC(NULL); if (NULL == hdcDev) { goto Err; } HBITMAP hBitmapTran = NULL; hBitmapTran = CreateCompatibleBitmap(hdcDev, VGA_WIDTH, 655); if (NULL == hBitmapTran) { goto Err; } HBITMAP hBitmapTranOld = NULL; hBitmapTranOld = (HBITMAP)SelectObject(hDcMemFrame, hBitmapTran); if (NULL == hBitmapTranOld) { goto Err; } //这是重点 BLENDFUNCTION blendTemp; SecureZeroMemory(&blendTemp, sizeof(BLENDFUNCTION)); blendTemp.BlendOp = AC_SRC_OVER; blendTemp.SourceConstantAlpha = 188; blendTemp.BlendFlags = 0; if(!AlphaBlend(m_Flicker.m_hDC, 0, 60, WVGA_WIDTH, 655, hDcMemFrame, 0, 0, WVGA_WIDTH, 655, blendTemp)) { goto Err; } if(!StretchBlt(hDcMemFrame, 0, 60, WVGA_WIDTH, 655, hdcMem, 0, 0, iPicWidth, iPicHeight, SRCCOPY)) { goto Err; } if(!StretchBlt(m_Flicker.m_hDC, 40, 90, 400, 595, hDcMemFrame, 40, 90, 400, 595, SRCCOPY)) { goto Err; } // Here means we are successful. bRet = TRUE; Err: if(hOldBitmap) { if(!SelectObject(hdcMem, hOldBitmap)) { } } if(hBitmap) { if(!DeleteObject(hBitmap)) { } } if(hdcMem) { if(!DeleteDC(hdcMem)) { } } if(hBitmapTranOld) { if(!SelectObject(hDcMemFrame, hBitmapTranOld)) { } } if(hBitmapTran) { if(!DeleteObject(hBitmapTran)) { } } if(hDcMemFrame) { if(!DeleteDC(hDcMemFrame)) { } } if(hdcDev) { if(!ReleaseDC(NULL, hdcDev)) { } }

你可能感兴趣的:(object,null,attributes)