****************************************************************************
**File: PrtScreen.c
**Author: Dong Liu
**Created: July 22th 2009
**Description: 实现截屏,并将得到的图片以bmp格式的文件保存到当前路径
**Function: PrtScreen()
**Called by: MsgScreen()
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <minigui/common.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
typedef struct tagRGBQUAD
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
//BYTE rgbReserved;
} RGBQUAD;
typedef RGBQUAD* PRGBQUAD;
typedef struct
{
DWORD biSize;//4
LONG biWidth;//4
LONG biHeight;//4
WORD biPlanes;//2
WORD biBitCount;//2
DWORD biCompress;//4
DWORD biSizeImage;//4
LONG biXPelsPerMeter;//4
LONG biYPelsPerMeter;//4
DWORD biClrUsed;//4
DWORD biClrImportant;//4
}__attribute__((packed))Infohead;
typedef struct
{
WORD bfType;//2
DWORD bfSize;//4
WORD bfReserved1;//2
WORD bfReserved2;//2
DWORD bfOffBits;//4
}__attribute__((packed))FileHead;
int PrtScreen(color_bit)//here it is 8
{
int i, j;
void * p = NULL;
int fd;
int count = 0;
int size, color;
HDC hdc;
RECT rc;
BITMAP scr_bmp;
HWND hwndfree;
FileHead bmp_head;
Infohead bmp_info;
RGBQUAD rgbquad;
hwndfree = GetActiveWindow();//取得当前活动窗口的句柄
hdc = GetDC(hwndfree);
GetWindowRect(hwndfree, &rc);//获得当前窗口的矩形区域
scr_bmp.bmWidth = RECTW(rc) ;
scr_bmp.bmHeight = RECTH(rc);
scr_bmp.bmBits = NULL;
scr_bmp.bmBits = malloc((scr_bmp.bmWidth ) * (scr_bmp.bmHeight ) * (color_bit / 8));
memset(scr_bmp.bmBits, 0, sizeof(scr_bmp.bmBits));
p = scr_bmp.bmBits;
size = RECTW(rc) * (RECTH(rc)) * (color_bit / 8);
bmp_head.bfType = MAKEWORD ('B', 'M');//生成BM格式的文件
bmp_head.bfSize = size + sizeof(FileHead) + sizeof(Infohead) + sizeof(rgbquad) * 256;
bmp_head.bfReserved1 = 0;
bmp_head.bfReserved2 = 0;
bmp_head.bfOffBits = bmp_head.bfSize - size;
bmp_info.biSize = 40;
bmp_info.biWidth = RECTW(rc);
bmp_info.biHeight = -RECTH(rc);
bmp_info.biPlanes = 1;
bmp_info.biBitCount = color_bit;
bmp_info.biCompress = 0;
bmp_info.biSizeImage = size;
bmp_info.biXPelsPerMeter = 0;
bmp_info.biYPelsPerMeter = 0;
bmp_info.biClrUsed = 256;
bmp_info.biClrImportant = 256;
//打开位图文件
fd = open("./scr.bmp", O_RDWR | O_CREAT | O_TRUNC);
//写入文件头信息和位图文件信息头
write(fd, &bmp_head, sizeof(FileHead));
write(fd, &bmp_info, sizeof(Infohead));
if(color_bit == 8)
{
for (i = 0; i < 256; i++)
{
rgbquad.rgbRed = (i & 0xe0);
rgbquad.rgbGreen = (i & 0x1c) << 3;
rgbquad.rgbBlue = (i & 0x03) << 6;
write(fd, &rgbquad, sizeof(rgbquad));
}
}
for(i = 0; i < scr_bmp.bmHeight; i++)
{
p = scr_bmp.bmBits;
count = 0;
for(j = 0; j < scr_bmp.bmWidth; j++)
{
color = GetPixel(hdc, j, i);
memcpy(p, &color, (color_bit / 8));
p = p + (color_bit / 8);
// 取得的像速必须是4的整数倍,要不然会出现像速移位
count = count + (color_bit / 8);
}
if(count % 4)
count = (4 - count % 4) + count;
write(fd, scr_bmp.bmBits, count);
}
close(fd);
free(scr_bmp.bmBits);
ReleaseDC(hdc);
return 0;
}