捕捉电脑桌面并打印
#include <stdio.h> #include <conio.h>//getch函数要定义此头文件 #include <windows.h> int main() { HWND hdesktopwnd,hconsolewnd;//桌面窗口的句柄,控制平台窗口句柄 RECT rect,conrect;//矩形 char title[1024]; HDC hscreendc,hconsoledc,hmemdc;//DC 设备描述符,画板 HBITMAP hbmp;//位图句柄 printf("按下任意键开始捕获系统桌面\n"); getch();//等待输入 hdesktopwnd = GetDesktopWindow();//1.获取桌面窗口的句柄 GetWindowRect(hdesktopwnd,&rect);//2.获取桌面窗口的矩形大小 GetConsoleTitle(title,1024);//3.获取控制平台窗口的标题 hconsolewnd = FindWindow(NULL,title);//4.根据标题获取控制平台窗口句柄 GetWindowRect(hconsolewnd,&conrect);//5.获取控制平台窗口的矩形大小 hscreendc = GetDC(hdesktopwnd);//6.获取桌面窗口的DC hconsoledc = GetDC(hconsolewnd);//7.获取控制台窗口的DC hmemdc = CreateCompatibleDC(hscreendc);//8.创建一个和桌面DC兼容的内存DC //9.把桌面窗口捕获到然后拷贝到兼容DC里 hbmp = CreateCompatibleBitmap(hscreendc,rect.right,rect.bottom); SelectObject(hmemdc,hbmp); BitBlt(hmemdc,0,0,rect.right,rect.bottom,hscreendc,0,0,SRCCOPY); //10.把兼容DC里的位图画到控制台DC上 StretchBlt(hconsoledc,0,0,conrect.right-conrect.left, conrect.bottom-conrect.top,hmemdc,0,0,1920,1280,SRCCOPY); //11.释放资源 DeleteObject(hbmp); DeleteObject(hmemdc); ReleaseDC(hconsolewnd,hconsoledc); ReleaseDC(hdesktopwnd,hscreendc); getch();//让程序暂停 return 0; }