I am attempting to draw a slightly transparent blue rectangle in Native Win32 C++. I am using the function AlphaBlend() but its not drawing anything onto the window, nothing happens.
My Problem: When I run my function to draw a slightly transparent rectangle, it doesn't get shown on my window. I have a feeling I am doing this wrong, maybe I should be using a HBITMAP?
Can you tell me what I need to do to get my function to draw a slightly transparent rectangle on the window?
Also I'm aware of GDI+ but I want to avoid it for now because I am getting alot of compile/include errors when I use that library plus I want to go as low/native as possible without the help of libraries that do everything for me.
bool paintRect(HDC hdc, RECT dim, COLORREF penCol, COLORREF brushCol, unsigned int opacity)
{
HDC tempHdc = CreateCompatibleDC(hdc);
BLENDFUNCTION blend = {AC_SRC_OVER, 0, 127, AC_SRC_ALPHA};
SetDCPenColor(tempHdc, RGB(255,255,0));
SetDCBrushColor(tempHdc, RGB(255,255,0));
Rectangle(tempHdc, dim.left, dim.top, dim.right, dim.bottom);
return bool(AlphaBlend(hdc, dim.left, dim.top, dim.right, dim.bottom, tempHdc, dim.left, dim.top, dim.right, dim.bottom, blend));
}
// Usage
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
RECT a = {0,0,100,100};
paintRect(hdc, a, RGB(255,255,0), RGB(255,255,0), 127); // 127 is 50% transparency right?
EndPaint(hwnd, &ps);
}
break;
Best Answer
This will work: