WinAPI 窗口在使用 PrintWindow 截屏后不再更新



我正在使用PrintWindow((截取应用程序的屏幕截图。应用程序包含一个列表视图,一段时间后,列表不再更新。仅当我在列表中选择一个条目时,它才会更新该条目的名称。我的假设是 ListView 的窗口不会以某种方式失效,但这只是一个猜测。截屏后,我尝试调用InvalidateRect((,但这也无济于事。

我认为造成这种情况的原因一定是资源泄漏,但我将所有必需的资源封装在一个类中,该类会自动处理释放它们。

struct DrawingSurface
{
DrawingSurface(const DrawingSurface&) = delete;
DrawingSurface& operator=(const DrawingSurface&) = delete;
// Window is a custom class, but it's not really important here.
DrawingSurface(const Window& window)
: hwnd(window.handle()), pixels(0), windowDC(0), memoryDC(0), bitmap(0), previous(0)
{
// Get window size.
Rect clientRect = window.getClientRect();
width = clientRect.width();
height = clientRect.height();
// Create DCs.
windowDC = ::GetDC(window.handle());
if(windowDC == NULL)
return;
memoryDC = ::CreateCompatibleDC(windowDC);
if(memoryDC == NULL)
return;
// Create bitmap.
ZeroMemory(&bitmapInfo, sizeof(BITMAPINFO));
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = width;
bitmapInfo.bmiHeader.biHeight = -height;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = width * height * 4;
bitmap = ::CreateDIBSection(windowDC, &bitmapInfo, DIB_RGB_COLORS, (void**)&pixels, 0, 0);
if(bitmap == NULL)
return;
previous = ::SelectObject(memoryDC, bitmap);
}
~DrawingSurface()
{
if(windowDC != NULL)
::ReleaseDC(hwnd, windowDC);
if(previous != NULL && previous != HGDI_ERROR && memoryDC != NULL)
::SelectObject(memoryDC, previous);
if(memoryDC != NULL)
::DeleteDC(memoryDC);
if(bitmap != NULL)
::DeleteObject(bitmap);
}
bool valid() const
{
return width * height > 0
&& previous != NULL
&& previous != HGDI_ERROR
&& windowDC != NULL
&& memoryDC != NULL
&& bitmap != NULL;
}
int width, height;
HWND hwnd;
HDC windowDC;
HDC memoryDC;
HBITMAP bitmap;
RGBQUAD* pixels;
BITMAPINFO bitmapInfo;
private:
HGDIOBJ previous;
};

然后,我使用此绘图表面使用此功能截取屏幕截图:

bool Screenshot::take(const Window& window)
{
m_width = 0; m_height = 0;
DrawingSurface surface(window);
if(!surface.valid())
return false;
if(PrintWindow(surface.hwnd, surface.memoryDC, PW_CLIENTONLY) == 0)
return false;
if(GdiFlush() == 0)
return false;
// Set attributes.
m_hwnd = surface.hwnd;
m_width = surface.width;
m_height = surface.height;
// Copy pixels.
m_pixels.resize(surface.width * surface.height, { 0, 0, 0, 0 });
memcpy(&m_pixels[0], surface.pixels, surface.bitmapInfo.bmiHeader.biSizeImage);
return true;
}

我看不出我可以在这里泄漏任何资源的地方。还有其他想法为什么我上面描述的事情可能会发生吗?

任何帮助表示赞赏,谢谢。

谢谢大家的回答。我现在能够自己解决这个问题,问题真的与屏幕截图功能无关。相反,我在ListView上发布了一条WM_KEYDOWN消息,也忘记了发布WM_KEYUP。这使列表视图处于无效状态,因此它不再更新。

再次感谢所有答案,特别是帮助验证没有泄漏资源。

最新更新