使用PrintWindow截屏后,WinAPI Window不再更新

时间:2018-09-07 14:33:40

标签: c++ winapi screenshot

我正在使用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;
}

我看不到哪里可以泄漏任何资源。为什么我上面所述的其他想法会发生?

任何帮助表示感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

感谢大家的回答。我现在可以自己解决此问题,而该问题确实与屏幕截图功能无关。相反,我在列表视图中发布了WM_KEYDOWN消息,也忘记了发布WM_KEYUP。这使ListView处于无效状态,因此不再更新。

再次感谢您提供所有答案,尤其感谢您帮助确认没有资源泄漏。