#include<stdio.h>
#include<conio.h>//因为要使用到getch()函数
#include<windows.h>
int main()
{
//定义局部变量
HWND hdesktopwnd,hconsolewnd;//桌面窗口的句柄和控制台窗口的句柄
HDC hscreendc,hconsoledc,hmemdc;//DC描述符,画板
HBITMAP hbmp;//位图的句柄
RECT rect,conrect;//矩形
char title[1024];
printf("按任意键开始捕获WINDOWS桌面\n");
getch();
//获取桌面窗口的句柄
hdesktopwnd=GetDesktopWindow();
GetWindowRect(hdesktopwnd,&rect);//获取桌面窗口的矩形大小
//获取控制台窗口的句柄
GetConsoleTitle(title,1024);//获取控制台窗口的标题
hconsolewnd=FindWindow(NULL,title);
GetWindowRect(hconsolewnd,&conrect);//获取控制台窗口的矩形大小
//获取桌面窗口的DC
hscreendc=GetDC(hdesktopwnd);
//获取控制台窗口的DC
hconsoledc=GetDC(hconsolewnd);
//创建一个和桌面DC兼容的内存DC
hmemdc=CreateCompatibleDC(hscreendc);
//把桌面窗口捕获到然后拷贝到兼容DC里
hbmp=CreateCompatibleBitmap(hscreendc,rect.right,rect.bottom);
SelectObject(hmemdc,hbmp);
BitBlt(hmemdc,0,0,rect.right,rect.bottom,hscreendc,0,0,SRCCOPY);
//把兼容DC里的位图画到控制台DC上
StretchBlt(hconsoledc,0,0,conrect.right-conrect.left,conrect.bottom-conrect.top,hmemdc,0,0,1366,768,SRCCOPY);
//释放资源
DeleteObject(hbmp);
DeleteObject(hmemdc);
ReleaseDC(hconsolewnd,hconsoledc);
ReleaseDC(hdesktopwnd,hscreendc);
getch();//让程序暂停
system("pause");
return 0;
}