如何在 C++ 中将窗口的屏幕截图作为位图对象获取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7292757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:44:48  来源:igfitidea点击:

How to get screenshot of a window as bitmap object in C++?

c++windowsvisual-c++bitmapwindow

提问by jondinham

How to get screenshot of a window as bitmap object in C++? Supposed that I already have the window handle. And I want to know also whether it's possible to get the screenshot of a window when it's in minimized state?

如何在 C++ 中将窗口的屏幕截图作为位图对象获取?假设我已经有了窗口句柄。而且我还想知道是否可以在窗口处于最小化状态时获得它的屏幕截图

C++ here means VC++ with all the libraries associated with Windows XP+ (win32).

C++ 在这里是指 VC++ 以及与 Windows XP+ (win32) 相关的所有库。

回答by Davide Piras

you should call the PrintWindow API:

您应该调用 PrintWindow API:

void CScreenShotDlg::OnPaint()
{
    // device context for painting
    CPaintDC dc(this);

    // Get the window handle of calculator application.
    HWND hWnd = ::FindWindow( 0, _T( "Calculator" ));

    // Take screenshot.
    PrintWindow( hWnd,
                 dc.GetSafeHdc(),
                 0 );
}

see this question: getting window screenshot windows API

看到这个问题:获取窗口截图 windows API

if you are not using MFC, here the pure PrintWindow signature:

如果您不使用 MFC,这里是纯 PrintWindow 签名:

BOOL PrintWindow(
    HWND hwnd,
    HDC hdcBlt,
    UINT nFlags
);

see MSDN for more details: http://msdn.microsoft.com/en-us/library/dd162869(v=vs.85).aspx

有关更多详细信息,请参阅 MSDN:http: //msdn.microsoft.com/en-us/library/dd162869(v=vs.85).aspx

about how to save it as bitmap asMatteo said depends on the actual framework you are using...

关于如何将其保存为位图,正如 Matteo 所说,取决于您使用的实际框架......

EDIT:

编辑:

here full example in raw C++

这里是原始 C++ 中的完整示例

#define _WIN32_WINNT    0x0501        //xp
#include <windows.h>

int main()
{ 
    RECT rc;
    HWND hwnd = FindWindow(TEXT("Notepad"), NULL);    //the window can't be min
    if (hwnd == NULL)
    {
        cout << "it can't find any 'note' window" << endl;
        return 0;
    }
    GetClientRect(hwnd, &rc);

    //create
    HDC hdcScreen = GetDC(NULL);
    HDC hdc = CreateCompatibleDC(hdcScreen);
    HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, 
        rc.right - rc.left, rc.bottom - rc.top);
    SelectObject(hdc, hbmp);

    //Print to memory hdc
    PrintWindow(hwnd, hdc, PW_CLIENTONLY);

    //copy to clipboard
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hbmp);
    CloseClipboard();

    //release
    DeleteDC(hdc);
    DeleteObject(hbmp);
    ReleaseDC(NULL, hdcScreen);

    cout << "success copy to clipboard, please paste it to the 'mspaint'" << endl;

    return 0;
}

回答by Ond?ej Vykouk

If anybody is interested in getting PrintWindow picture of minimized window, here you can get the idea, how to get the thing done: http://www.codeproject.com/Articles/20651/Capturing-Minimized-Window-A-Kid-s-Trick

如果有人有兴趣获得最小化窗口的 PrintWindow 图片,您可以在这里了解如何完成这项工作:http: //www.codeproject.com/Articles/20651/Capturing-Minimized-Window-A-Kid-特技

Happy coding;)

快乐编码;)

回答by vik_78

Looks like PrintWindow is working with frontbuffer. I tried to take an IE screenshot. Open new a link, and try to get the picture. It will show the picture from the previous link.

看起来 PrintWindow 正在使用前端缓冲区。我尝试截取 IE 屏幕截图。打开新链接,并尝试获取图片。它将显示上一个链接中的图片。