PrintWindow函数是截取在屏幕窗口区域外的函数,即截取“移除到屏幕外的窗口”。比如:mfc中要截取隐藏的一个窗口时,我们可以将这个窗口移除到屏幕外,然后用PrintWindow函数进行截取。
下面这是一个win32控制台程序(给出了PrintWindow函数的用法):
#define _WIN32_WINNT 0x0501 //仅XP或以上系统有效
#include <windows.h>
int main()
{
RECT rc;
HWND hwnd = FindWindow(TEXT("Notepad"), NULL); //注意窗口不能最小化
if (hwnd == NULL)
{
cout << "找不到记事本窗口" << endl;
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, PW_CLIENTONLY);
//PW_CLIENTONLY:Only the client area of the window is copied to hdcBlt.
//By default, the entire window is copied.
//PW_CLIENTONLY表示仅仅拷贝窗口的客户区域,而默认情况下,执行printwindow会拷贝整个窗口
//复制到粘贴板
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hbmp);
CloseClipboard();
//释放
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(NULL, hdcScreen);
cout << "成功把记事本窗口复制到粘贴板,请粘贴到Windows画图工具" << endl;
return 0;
}
执行完上述程序之后,可以打开“附件”-》“画图”->"粘贴",效果图如下:
http://s16/middle/6dd65c6ftb3f674f1917f&690