[英]How to get screenshot of a window as bitmap object in C++?
How to get screenshot of a window as bitmap object in C++? 如何在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++ here means VC++ with all the libraries associated with Windows XP+ (win32). 这里的C ++意味着VC ++包含与Windows XP +(win32)相关的所有库。
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++ 这里是原始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;
}
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- S-绝招
Happy coding;) 快乐的编码;)
Looks like PrintWindow is working with frontbuffer. 看起来像PrintWindow正在使用前缓冲器。 I tried to take an IE screenshot. 我试图采用IE截图。 Open new a link, and try to get the picture. 打开一个新链接,并尝试获取图片。 It will show the picture from the previous link. 它将显示上一个链接的图片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.