我正在努力找出在 WM_PAINT 期间将纯 RGBA 值数组转储到 Win32 窗口的客户区的正确方法。我有以下代码,但它看起来已经很复杂,我什至还没有完成:
case WM_ERASEBKGND:
return 1;
case WM_PAINT:
{
PAINTSTRUCT paintInfo{};
HDC device = BeginPaint(window, &paintInfo);
if (device == nullptr)
throw runtime_error(RG_LOCATION());
ScopeExit endPaint([&] { EndPaint(window, &paintInfo); });
HDC offscreenDevice = CreateCompatibleDC(device);
ScopeExit deleteOffscreenDevice([&] { DeleteDC(offscreenDevice); });
HBITMAP offscreenBitmap = CreateCompatibleBitmap(device, Distance(paintInfo.rcPaint.left, paintInfo.rcPaint.right),
Distance(paintInfo.rcPaint.top, paintInfo.rcPaint.bottom));
ScopeExit deleteOffscreenBitmap([&] { DeleteObject(offscreenBitmap); });
HBITMAP previousBitmap = reinterpret_cast<HBITMAP>(SelectObject(offscreenDevice, offscreenBitmap));
// now I need to blit the available pixel data...
vector<array<uint8_t, 4>> mypixels;
// ...onto the client area of the window.
// What do I do next?
// CreateDIBSection ?
// BitBlt ?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于源“图像”内存格式,我有一些回旋余地,因此我可以使其匹配目标所需的任何内容。
我这样做正确吗?有没有更好的办法?
PS:显然,每次 WM_PAINT 出现时,我都会存储而不是重新创建大多数对象。这只是一个示例/WIP。
编辑:添加了 WM_ERASEBKGND 的处理。
编辑2:好的,看来我需要更具体一些。我并不是在寻找我发布的代码的实际问题。这只是我迄今为止在工作流程方面所拥有的一个示例。这意味着我有窗口 HDC、屏幕外 HDC、屏幕外 HBITMAP 和指向像素的指针,这些像素位于假设的 R8G8B8A8 内存布局中。我该如何处理这些物体?我是否通过 CreateDIBSection 创建另一个 HBITMAP 并将我的像素写入其中?之后我该怎么办?
编辑 3:请参阅 Barmak Shemirani 的答案以获得正确的解决方案(我的示例代码有问题)。另请查看 Paul Sanders 的回答,了解一些现代 WinAPI 技巧。
谢谢大家!
要打印mypixels矢量,请使用SetDIBitsToDevice绘制到设备上下文。或者用来SetDIBits创建一个新HBITMAP对象。
为简单起见,此示例直接绘制到HDC。但您可以用于CreateCompatibleDC缓冲,或使用其他答案中显示的缓冲方法。
case WM_PAINT:
{
//int w = width of source bitmap
//int h = height of source bitmap
//optional: make sure width and height are correct
assert(mypixels.size() == w * h);
PAINTSTRUCT ps;
auto hdc = BeginPaint(hwnd, &ps);
BITMAPINFOHEADER bi{ sizeof(bi) };
bi.biWidth = w;
bi.biHeight = h;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
SetDIBitsToDevice(hdc, 0, 0, w, h, 0, 0, 0, h, &mypixels[0],
(BITMAPINFO*)&bi, DIB_RGB_COLORS);
EndPaint(hwnd, &ps);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用内存直流:
case WM_PAINT:
{
RECT rc;
GetClientRect(hwnd, &rc);
int canvas_width = rc.right;
int canvas_height = rc.bottom;
PAINTSTRUCT ps;
auto hdc = BeginPaint(hwnd, &ps);
//create memory dc:
auto memdc = CreateCompatibleDC(hdc);
auto hbmp = CreateCompatibleBitmap(hdc, canvas_width, canvas_height);
auto oldbmp = SelectObject(memdc, hbmp); //<- memdc is ready
//draw on memory dc:
BITMAPINFOHEADER bi{ sizeof(bi), w, h, 1, 32, BI_RGB };
SetDIBitsToDevice(memdc, 0, 0, w, h, 0, 0, 0, h, mypixels.data(),
(BITMAPINFO*)&bi, DIB_RGB_COLORS);
//draw on actual dc:
BitBlt(hdc, 0, 0, canvas_width, canvas_height, memdc, 0, 0, SRCCOPY);
//clean up:
SelectObject(memdc, oldbmp);
DeleteObject(hbmp);
DeleteDC(memdc);
EndPaint(hwnd, &ps);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
关于绘图无闪烁,Vista 及更高版本在 Win32 API 中内置了双缓冲支持。我改编了本文中的以下代码。更多信息请访问MSDN。巴尔马克的答案向您展示了如何绘制像素。
初始化(每个线程):
BufferedPaintInit();
Run Code Online (Sandbox Code Playgroud)
终止(每个线程):
BufferedPaintUnInit();
Run Code Online (Sandbox Code Playgroud)
在你的 WndProc 中:
case WM_PAINT:
{
// Set things up in the usual way
PAINTSTRUCT ps;
HDC hDC = BeginPaint (hWnd, &ps);
RECT rc;
GetClientRect (hWnd, &rc);
// Try to use buffered painting (may fail, so they say)
HDC hBufferedDC;
HPAINTBUFFER hBufferedPaint = BeginBufferedPaint (hDC, &rc, BPBF_COMPATIBLEBITMAP, NULL, &hBufferedDC);
if (hBufferedPaint)
hDC = hBufferedDC;
// Draw stuff into hDC
// Clean up
if (hBufferedPaint)
EndBufferedPaint (hBufferedPaint, TRUE);
// Finished
EndPaint (hWnd, &ps);
break;
}
Run Code Online (Sandbox Code Playgroud)
真的没什么。
| 归档时间: |
|
| 查看次数: |
624 次 |
| 最近记录: |