#include
#include
int main()
{
HWND hwin;
HDC dc,memdc;
HBITMAP hbitm,hold;
char *pchar,*mloc;
RECT rect;
int cx,cy,recnum;
hwin=GetDesktopWindow();//获取桌面窗口句柄
dc=GetWindowDC(hwin);//获取桌面窗口DC
GetWindowRect(hwin,&rect);//获取桌面窗口大小
cx=rect.right-rect.left;
cy=rect.bottom-rect.top;
memdc=CreateCompatibleDC(dc);//创建与桌面窗口DC相适应的内存DC
hbitm=CreateCompatibleBitmap(dc,cx,cy);//创建相适应的位图
SelectObject(memdc, hbitm);//将新建的位图选入内存DC中
BitBlt(memdc,0,0,cx,cy,dc,0,0,SRCCOPY);
////////////////////////////////////////////////////////////////////////////////////////////////
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount =24;// bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = cy;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = cx*cy*3;//图像数据大小
bih.biWidth = cx;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + cx*cy*3;//文件总的大小
bfh.bfType = (WORD)0x4d42;
mloc=new char[cx*cy*3];//申请空间来存放图像数据
recnum=GetDIBits(memdc,hbitm,cx,cy,mloc,(BITMAPINFO*)&bfh, DIB_RGB_COLORS);//获取位图数据
FILE *stream;
if ((stream = fopen("截屏.bmp", "wb")) == NULL) /* open file TEST.$$$ */
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
fwrite(&bfh, sizeof(BITMAPFILEHEADER), 1, stream);
fwrite(&bih, sizeof(BITMAPINFOHEADER), 1, stream);
fwrite(mloc, cx*cy*3, 1, stream);
fclose(stream); /*关闭文件*/
return 0;
}