使用AlphaBlend绘制稍微透明的矩形失败 [英] Using AlphaBlend to draw slightly transparent rectangle fails
问题描述
我试图在Native Win32 C ++中绘制一个稍微透明的蓝色矩形。我使用的功能AlphaBlend(),但它没有绘制任何东西到窗口,没有任何反应。
我的问题:当我运行我的函数来绘制一个稍微透明的矩形,它不会显示在我的窗口。我有一个感觉,我做错了,也许我应该使用HBITMAP?
你能告诉我我需要做什么,在窗口上绘制一个稍微透明的矩形?
还有我知道GDI +,但我想避免它,
$ b
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 ));
}
//用法
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是50%的透明度吗?
EndPaint(hwnd,& ps);
}
break;
这将工作:
<$ c $ p>
bool paintRect(HDC hdc,RECT dim,COLORREF penCol,COLORREF brushCol,unsigned int opacity)
{
HDC tempHdc = CreateCompatibleDC hdc);
BLENDFUNCTION blend = {AC_SRC_OVER,0,127,0};
HBITMAP hbitmap; //位图句柄
BITMAPINFO bmi; // bitmap header
//零位图信息的内存
ZeroMemory(& bmi,sizeof(BITMAPINFO));
//设置位图信息
//将位图宽度和高度设置为三个水平区域的宽度和高度的60%。之后,混合将发生在三个区域中的每一个的中心。
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = dim.right-dim.left;
bmi.bmiHeader.biHeight = dim.bottom-dim.top;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32; //四个8位组件
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage =(dim.right-dim.left)*(dim.bottom-dim.top)* 4;
//创建我们的DIB节,并选择位图到dc
hbitmap = CreateDIBSection(tempHdc,& bmi,DIB_RGB_COLORS,NULL,NULL,0x0);
SelectObject(tempHdc,hbitmap);
SetDCPenColor(tempHdc,RGB(0,0,255));
SetDCBrushColor(tempHdc,RGB(0,0,255));
FillRect(tempHdc,&dim; CreateSolidBrush(RGB(0,0,255)));
return bool(AlphaBlend(hdc,dim.left,dim.top,dim.right,dim.bottom,tempHdc,dim.left,dim.top,dim.right,dim.bottom,blend ));
}
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;
This will work:
bool paintRect(HDC hdc, RECT dim, COLORREF penCol, COLORREF brushCol, unsigned int opacity)
{
HDC tempHdc = CreateCompatibleDC(hdc);
BLENDFUNCTION blend = {AC_SRC_OVER, 0, 127, 0};
HBITMAP hbitmap; // bitmap handle
BITMAPINFO bmi; // bitmap header
// zero the memory for the bitmap info
ZeroMemory(&bmi, sizeof(BITMAPINFO));
// setup bitmap info
// set the bitmap width and height to 60% of the width and height of each of the three horizontal areas. Later on, the blending will occur in the center of each of the three areas.
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = dim.right-dim.left;
bmi.bmiHeader.biHeight = dim.bottom-dim.top;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32; // four 8-bit components
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = (dim.right-dim.left) * (dim.bottom-dim.top) * 4;
// create our DIB section and select the bitmap into the dc
hbitmap = CreateDIBSection(tempHdc, &bmi, DIB_RGB_COLORS, NULL, NULL, 0x0);
SelectObject(tempHdc, hbitmap);
SetDCPenColor(tempHdc, RGB(0,0,255));
SetDCBrushColor(tempHdc, RGB(0,0,255));
FillRect(tempHdc, &dim, CreateSolidBrush(RGB(0,0,255)));
return bool(AlphaBlend(hdc, dim.left, dim.top, dim.right, dim.bottom, tempHdc, dim.left, dim.top, dim.right, dim.bottom, blend));
}
这篇关于使用AlphaBlend绘制稍微透明的矩形失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!