c++ - 只截图窗口的特定区域

标签 c++ winapi screenshot

我目前正在使用以下代码行截取窗口:

UpdateWindow(hwnd);
HDC window_dc = GetDC(hwnd);
HDC res = CreateCompatibleDC(window_dc);
RECT r;
GetClientRect(hwnd, &r);
HBITMAP bmp = CreateCompatibleBitmap(window_dc, r.right - r.left, r.bottom - r.top);
SelectObject(res, bmp);
PrintWindow(hwnd, res, PW_CLIENTONLY);
DeleteObject(bmp);
ReleaseDC(hwnd, window_dc);

现在有没有办法只截取我的 HWND 的特定区域?我意识到,在某些应用程序中,当窗口稍微大一点时,屏幕截图会花费更长的时间。所以我想如果我能够只截取重要的帧,我真的可以提高我的速度。

编辑:它需要为处于后台的窗口工作,例如。被其他窗口重叠。

最佳答案

你可以这样试试:

bool ScreenShot(HWND hwnd, int x, int y, int w, int h, LPCSTR file){
    HDC source = GetDC(hwnd);
    HDC memory = CreateCompatibleDC(source);

    HBITMAP bitmap = CreateCompatibleBitmap(source, w, h);
    HBITMAP bitmapOld = (HBITMAP)SelectObject(memory, hBitmap);

    BitBlt(memory, 0, 0, w, h, source, x, y, SRCCOPY);
    hBitmap = (HBITMAP)SelectObject(memory, bitmapOld);

    DeleteDC(source);
    DeleteDC(memory);

    HPALETTE pal = NULL;
    if(saveBitmap(file, bitmap, pal)) return true;
    return false;
}

关于c++ - 只截图窗口的特定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27837364/

相关文章:

c++ - 检查字符串中的小写字符或空格。 bool 值不起作用

c++ - 将 Catkin 与 CMake 接口(interface)库结合使用

swift - swift 从屏幕的一部分截取屏幕截图

python - 如何在 python 中将 COLORREF 从 GetPixel() 转换为 RGB?

excel - 显示带有超时值的消息框

java - 如何在 Java 中快速截取屏幕截图?

ios - 如何截取 UIView 的屏幕截图并使用它具有模糊背景?

c++ - 使用 MSVC 2015 构建时出现链接器错误(其他 CC 都可以)

c++ - 为什么我看不到 "Application Error"对话框?

创建一个简单的单缓冲区应用程序 (IDirectDraw)