Windows 使用缓冲区的 AlphaBlend
Windows AlphaBlend with buffer usage
我想在给定的 window 内绘制一个缓冲区(带有 alpha 信息!)。绘图是在 WM_PAINT 之外完成的(它是在从 Chromium-Embedded-Framework 调用的 CefRenderHandler::OnPaint 方法中完成的)。
我遇到的问题是:
- 未清除 window 的旧内容(如果更改缓冲区,我会绘制旧内容和绘制新内容)。
- alpha 通道被错误地解释 - 我认为,即使像素具有 alpha 信息,它也被绘制为没有 alpha 信息
这是我目前拥有的:
OnPaint(...):
HDC screen_dc = GetDC(windowHandle);
RECT rcWin;
GetClientRect(windowHandle, &rcWin);
BITMAPINFO info;
ZeroMemory(&info, sizeof(BITMAPINFO));
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biWidth = width;
info.bmiHeader.biHeight = -height;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biSizeImage = width*height * 4;
info.bmiHeader.biCompression = BI_RGB;
void *buf;
HBITMAP hDib = CreateDIBSection(screen_dc, &info, DIB_RGB_COLORS, (void **)&buf, 0, 0);
memcpy(buf, buffer, width * height * 4); //buffer contains bitmap to draw
HDC hDibDC = CreateCompatibleDC(screen_dc);
HGDIOBJ hOldObj = SelectObject(hDibDC, hDib);
BLENDFUNCTION blendFunction_;
blendFunction.BlendOp = AC_SRC_OVER;
blendFunction.BlendFlags = 0;
blendFunction.SourceConstantAlpha = 255;
blendFunction.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(screen_dc, 0, 0, width, height, hDibDC, 0, 0, width, height, blendFunction);
SelectObject(hDibDC, hOldObj);
ReleaseDC(windowHandle, screen_dc);
DeleteObject(hDib);
DeleteDC(hDibDC);
Window创作:
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = BrowserWindowWndProc;
wcex.hInstance = hinstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = WHITE_BRUSH;
wcex.lpszClassName = BROWSER_WINDOW_CLASS;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
RegisterClassEx(&wcex);
DWORD exStyle{0};
exStyle |= WS_EX_TOOLWINDOW;
exStyle |= WS_EX_LAYERED;
DWORD style {0};
style |= WS_SYSMENU;
style |= WS_VISIBLE;
HWND hWnd = CreateWindowEx(
exStyle,
BROWSER_WINDOW_CLASS,
BROWSER_WINDOW_CLASS,
style,
100,
100,
300,
300,
nullptr,
nullptr,
hinstance,
nullptr
);
...
SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), 255, LWA_COLORKEY);
你能帮我解决这些问题吗?
提前致谢。
没有必要同时使用AlphaBlend
和分层windows。仅使用分层 windows:
void OnPaint(HDC hdc, int width, int height, HBITMAP hbitmap)
{
HDC memdc = CreateCompatibleDC(hdc);
auto oldbmp = SelectObject(memdc, hbitmap);
BITMAP bm;
GetObject(hbitmap, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memdc, 0, 0, SRCCOPY);
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
}
其中 hbitmap
是先前创建的位图的句柄。使用 SetLayeredWindowAttributes(hwnd, RGB(255,255,255), 255, LWA_COLORKEY);
时,位图的白色区域应显示为透明
或使用 LWA_COLORKEY | LWA_ALPHA
调整透明度和 alpha 级别。
假设 OnPaint
是对 WM_PAINT
的回应,请使用 BeginPaint/EndPaint
而不是 GetDC/ReleaseDC
注意 WHITE_BRUSH
为零,因此 wcex.hbrBackground = WHITE_BRUSH;
将背景画笔设置为零。改为指定画笔手柄。
或者您可以在同一个 window 中使用 TransparentBlt
:
HDC memdc = CreateCompatibleDC(hdc);
auto oldbmp = SelectObject(memdc, hbitmap);
BITMAP bm;
GetObject(hbitmap, sizeof(bm), &bm);
TransparentBlt(hdc, 0, 0, width, height,
memdc, 0, 0, bm.bmWidth, bm.bmHeight, RGB(255, 255, 255));
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
我想在给定的 window 内绘制一个缓冲区(带有 alpha 信息!)。绘图是在 WM_PAINT 之外完成的(它是在从 Chromium-Embedded-Framework 调用的 CefRenderHandler::OnPaint 方法中完成的)。
我遇到的问题是:
- 未清除 window 的旧内容(如果更改缓冲区,我会绘制旧内容和绘制新内容)。
- alpha 通道被错误地解释 - 我认为,即使像素具有 alpha 信息,它也被绘制为没有 alpha 信息
这是我目前拥有的:
OnPaint(...):
HDC screen_dc = GetDC(windowHandle);
RECT rcWin;
GetClientRect(windowHandle, &rcWin);
BITMAPINFO info;
ZeroMemory(&info, sizeof(BITMAPINFO));
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biWidth = width;
info.bmiHeader.biHeight = -height;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biSizeImage = width*height * 4;
info.bmiHeader.biCompression = BI_RGB;
void *buf;
HBITMAP hDib = CreateDIBSection(screen_dc, &info, DIB_RGB_COLORS, (void **)&buf, 0, 0);
memcpy(buf, buffer, width * height * 4); //buffer contains bitmap to draw
HDC hDibDC = CreateCompatibleDC(screen_dc);
HGDIOBJ hOldObj = SelectObject(hDibDC, hDib);
BLENDFUNCTION blendFunction_;
blendFunction.BlendOp = AC_SRC_OVER;
blendFunction.BlendFlags = 0;
blendFunction.SourceConstantAlpha = 255;
blendFunction.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(screen_dc, 0, 0, width, height, hDibDC, 0, 0, width, height, blendFunction);
SelectObject(hDibDC, hOldObj);
ReleaseDC(windowHandle, screen_dc);
DeleteObject(hDib);
DeleteDC(hDibDC);
Window创作:
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = BrowserWindowWndProc;
wcex.hInstance = hinstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = WHITE_BRUSH;
wcex.lpszClassName = BROWSER_WINDOW_CLASS;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
RegisterClassEx(&wcex);
DWORD exStyle{0};
exStyle |= WS_EX_TOOLWINDOW;
exStyle |= WS_EX_LAYERED;
DWORD style {0};
style |= WS_SYSMENU;
style |= WS_VISIBLE;
HWND hWnd = CreateWindowEx(
exStyle,
BROWSER_WINDOW_CLASS,
BROWSER_WINDOW_CLASS,
style,
100,
100,
300,
300,
nullptr,
nullptr,
hinstance,
nullptr
);
...
SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), 255, LWA_COLORKEY);
你能帮我解决这些问题吗?
提前致谢。
没有必要同时使用AlphaBlend
和分层windows。仅使用分层 windows:
void OnPaint(HDC hdc, int width, int height, HBITMAP hbitmap)
{
HDC memdc = CreateCompatibleDC(hdc);
auto oldbmp = SelectObject(memdc, hbitmap);
BITMAP bm;
GetObject(hbitmap, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memdc, 0, 0, SRCCOPY);
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
}
其中 hbitmap
是先前创建的位图的句柄。使用 SetLayeredWindowAttributes(hwnd, RGB(255,255,255), 255, LWA_COLORKEY);
或使用 LWA_COLORKEY | LWA_ALPHA
调整透明度和 alpha 级别。
假设 OnPaint
是对 WM_PAINT
的回应,请使用 BeginPaint/EndPaint
而不是 GetDC/ReleaseDC
注意 WHITE_BRUSH
为零,因此 wcex.hbrBackground = WHITE_BRUSH;
将背景画笔设置为零。改为指定画笔手柄。
或者您可以在同一个 window 中使用 TransparentBlt
:
HDC memdc = CreateCompatibleDC(hdc);
auto oldbmp = SelectObject(memdc, hbitmap);
BITMAP bm;
GetObject(hbitmap, sizeof(bm), &bm);
TransparentBlt(hdc, 0, 0, width, height,
memdc, 0, 0, bm.bmWidth, bm.bmHeight, RGB(255, 255, 255));
SelectObject(memdc, oldbmp);
DeleteDC(memdc);