How to clip a rectangle when calling AlphaBlend?

1 day ago 3
ARTICLE AD BOX

I call AlphaBlend for blitting an image with alpha channel from a memory DC to a window DC:

HBITMAP CreateBitmapForMemoryDC(HDC hdc, int width, int height, int bitCount) { BITMAPINFO bi; ZeroMemory(&bi, sizeof(bi)); bi.bmiHeader.biSize = sizeof(bi.bmiHeader); bi.bmiHeader.biWidth = width; bi.bmiHeader.biHeight = height; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = bitCount; bi.bmiHeader.biCompression = BI_RGB; return CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, 0, 0, 0); } … g_hBitmap = CreateBitmapForMemoryDC(NULL, width, height, 32); g_hMemDC = CreateCompatibleDC(NULL); SelectObject(g_hMemDC, g_hBitmap); … // Painting stuff … // On render: BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; AlphaBlend(hDC, 0, 0, width, height, g_hMemDC, 0, 0, width, height, bf);

How do I clip a rectangle?

I can call AlphaBlend twice (for a corner) or multiple times (for an inner hole).

Is there a better approach?

Read Entire Article