我有一个C++程序来捕获特定窗口的屏幕截图,并使用以下代码保存它

 int main()
 {
   GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR gdiplusToken;
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

   RECT rc;
   HWND hwnd = FindWindow(NULL,TEXT("Window Title Here"));
   if(hwnd == NULL)
   {
      cout<<"Can't Find Window";
      return 0;
   }

    GetClientRect(hwnd,&rc);

    HDC hdcscreen = GetDC(NULL);
    HDC hdc = CreateCompatibleDC(hdcscreen);

    HBITMAP hbmp = CreateCompatibleBitmap(hdcscreen,rc.right - rc.left,rc.bottom - rc.top);

    SelectObject(hdc,hbmp);

    PrintWindow(hwnd,hdc,NULL);

    BitmapToJpg(hbmp,rc.right - rc.left,rc.bottom-rc.top); //Function to convert hbmp bitmap to jpg

    DeleteDC(hdc);
    DeleteObject(hbmp);
    ReleaseDC(NULL,hdcscreen);
 }

此代码适用于许多窗口,但对于某些窗口,输出是具有正确宽度和高度的黑色图像。通过搜索,我找到了使用BitBlt()的解决方案。但是我不知道如何用PrintWindow()替换BitBlt()并输出到HBITMAP。帮助需要

最佳答案

首先,将hdcscreen替换为hdcwnd,您可以使用GetDC(hwnd)代替GetDC(NULL)。它可能什么都不会改变,但是即使使用PrintWindow(),它也足够了。
然后,只需替换:

PrintWindow(hwnd,hdc,NULL);

经过 :
BitBlt( hdc, 0, 0, rc.right - rc.left,rc.bottom-rc.top, hdcwnd, 0, 0, SRCCOPY );

关于c++ - 将PrintWindow转换为BitBlt以捕获特定窗口的屏幕截图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31532878/

10-11 23:14
查看更多