如何在c++中以位图对象的形式获取窗口的截图?

时间:2022-09-02 00:26:45

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)相关的库。

3 个解决方案

#1


22  

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...

关于如何保存位图,asMatteo说的取决于你使用的实际框架……

EDIT:

编辑:

here full example in raw C++

这里是raw 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;
}

#2


6  

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-minimzed - windows - a - kid - trick

Happy coding;)

快乐的编码。

#3


2  

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正在使用frontbuffer。我试着拍了个IE截图。打开一个新的链接,并尝试获得图片。它将显示来自前一个链接的图片。

#1


22  

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...

关于如何保存位图,asMatteo说的取决于你使用的实际框架……

EDIT:

编辑:

here full example in raw C++

这里是raw 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;
}

#2


6  

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-minimzed - windows - a - kid - trick

Happy coding;)

快乐的编码。

#3


2  

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正在使用frontbuffer。我试着拍了个IE截图。打开一个新的链接,并尝试获得图片。它将显示来自前一个链接的图片。