title: How to take a screenshot for frameless transparent qt window?
body: <p>The Qt Window set window flag and attribute:</p>
  
  <pre><code>setWindowFlag(Qt::FramelessWindowHint);
  setAttribute(Qt::WA_TranslucentBackground);
  </code></pre>
  
  <p>Screenshot codes:</p>
  
  <pre><code>HDC hdc = GetDC(hwnd);
  if (!hdc)
  {
      LOGE &lt;&lt; "Get window dc failed. Code - " &lt;&lt; GetLastError();
      return "";
  }
  SCOPE_GUARD([hwnd, hdc] {
      ReleaseDC(hwnd, hdc);
  });
  
  //get window size
  RECT clientRect;
  if (hwnd)
  {
      GetClientRect(hwnd, &amp;clientRect);
  }
  else
  {
      clientRect.top = 0;
      clientRect.left = 0;
      clientRect.right = GetSystemMetrics(SM_CXSCREEN);
      clientRect.bottom = GetSystemMetrics(SM_CYSCREEN);
  }
  LONG rectWidth = clientRect.right - clientRect.left;
  LONG rectHeith = clientRect.bottom - clientRect.top;
  
  //create target dc
  HDC dcMemory = CreateCompatibleDC(hdc);
  if (!dcMemory)
  {
      LOGE &lt;&lt; "Create compatible dc failed. Code - " &lt;&lt; GetLastError();
      return "";
  }
  SCOPE_GUARD([dcMemory] {
      DeleteObject(dcMemory);
  });
  
  HBITMAP hBitmap = CreateCompatibleBitmap(hdc, rectWidth, rectHeith);
  if (!hBitmap)
  {
      LOGE &lt;&lt; "Create compatible bitmap failed. Code - " &lt;&lt; GetLastError();
      return "";
  }
  SCOPE_GUARD([hBitmap] {
      DeleteObject(hBitmap);
  });
  
  //select and print
  {
      HGDIOBJ hOldBitmap = SelectObject(dcMemory, hBitmap);
      SCOPE_GUARD([dcMemory, hOldBitmap] {
          SelectObject(dcMemory, hOldBitmap);
      });
  
      if (!BitBlt(dcMemory, 0, 0, rectWidth, rectHeith, hdc, 0, 0, SRCCOPY))
      {
          LOGE &lt;&lt; "Copy data from DC failed. Code - " &lt;&lt; GetLastError();
          return "";
      }
  }
  </code></pre>
  
  <p>Finally, I got a black image.</p>
  
  <p>I found <a href="https://code.google.com/archive/p/aeroshot/" rel="nofollow noreferrer">Aeroshot</a> do this. It put target window top and make a black window back. And take screenshot for Display in target window's range. </p>
  
  <p>I don't want to affect the target program. Are there other options?</p>