作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图在 native Win32 C++ 中绘制一个稍微透明的蓝色矩形。我正在使用函数 AlphaBlend() 但它没有在窗口上绘制任何东西,没有任何反应。
我的问题:当我运行我的函数来绘制一个稍微透明的矩形时,它没有显示在我的窗口中。我觉得我做错了,也许我应该使用 HBITMAP?
你能告诉我我需要做什么才能让我的函数在窗口上绘制一个稍微透明的矩形吗?
我也知道 GDI+,但我现在想避免使用它,因为我在使用该库时遇到很多编译/包含错误,而且我想在没有帮助的情况下尽可能低/原生为我做一切的图书馆。
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;
最佳答案
这会起作用:
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));
}
关于c++ - 使用 AlphaBlend 绘制略透明的矩形失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10975180/
直接问题:对于一个类对象的三个(或更多)几乎相同的拷贝,我怎样才能最好(或最有效)地存储它们之间的差异? 背景:我有一个需要一组参数的算法: struct params { std::strin
我是一名优秀的程序员,十分优秀!