代码是借鉴别人的,还有疑问,比如在画图软件中,鼠标圆圈,这么写好像并没有截图到完整鼠标。
#include <stdio.h>
#include <iostream>
#include <Windows.h>
int SaveBitmapToFile(HBITMAP hBitmap, LPSTR lpFileName)
{ //lpFileName 为位图文件名
HDC hDC;
//设备描述表
int iBits;
//当前显示分辨率下每个像素所占字节数
WORD wBitCount;
//位图中每个像素所占字节数
//定义调色板大小, 位图中像素字节大小 , 位图文件大小 , 写入文件字节数
DWORD dwPaletteSize = 0, dwBmBitsSize, dwDIBSize, dwWritten;
BITMAP Bitmap;
//位图属性结构
BITMAPFILEHEADER bmfHdr;
//位图文件头结构
BITMAPINFOHEADER bi;
//位图信息头结构
LPBITMAPINFOHEADER lpbi;
//指向位图信息头结构
HANDLE fh, hDib, hPal;
HPALETTE hOldPal = NULL;
//定义文件,分配内存句柄,调色板句柄
//计算位图文件每个像素所占字节数
hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) *
GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = 32;
//计算调色板大小
if (wBitCount <= 8)
dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);
//设置位图信息头结构
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwBmBitsSize = ((Bitmap.bmWidth*wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
//为位图内容分配内存
hDib = GlobalAlloc(GHND, dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
// 处理调色板
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
RealizePalette(hDC);
}
// 获取该调色板下新的像素值
GetDIBits(hDC, hBitmap, 0, (UINT)Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize, (BITMAPINFO *)lpbi, DIB_RGB_COLORS);
//恢复调色板
if (hOldPal)
{
SelectPalette(hDC, hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
//创建位图文件
fh = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
return FALSE;
// 设置位图文件头
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
// 写入位图文件头
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// 写入位图文件其余内容
WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize, &dwWritten, NULL);
//清除
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return TRUE;
}
bool SaveBitmapToFile1(HBITMAP hBitmap, char* szfilename)
{
HDC hdc; //设备描述表
int ibits;
WORD wbitcount; //当前显示分辨率下每个像素所占字节数
//位图中每个像素所占字节数,定义调色板大小,位图中像素字节大小,位图文件大小 ,写入文件字节数
DWORD dwpalettesize = 0, dwbmbitssize, dwdibsize, dwwritten;
BITMAP bitmap; //位图属性结构
BITMAPFILEHEADER bmfhdr; //位图文件头结构
BITMAPINFOHEADER bi; //位图信息头结构
LPBITMAPINFOHEADER lpbi; //指向位图信息头结构
//定义文件,分配内存句柄,调色板句柄
HANDLE fh, hdib, hpal, holdpal = NULL;
//计算位图文件每个像素所占字节数
hdc = CreateDC("display", NULL, NULL, NULL);
ibits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
DeleteDC(hdc);
if (ibits <= 1)
wbitcount = 1;
else if (ibits <= 4)
wbitcount = 4;
else if (ibits <= 8)
wbitcount = 8;
else if (ibits <= 16)
wbitcount = 16;
else if (ibits <= 24)
wbitcount = 24;
else
wbitcount = 32;
//计算调色板大小
if (wbitcount <= 8)
dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);
//设置位图信息头结构
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wbitcount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwbmbitssize = ((bitmap.bmWidth * wbitcount + 31) / 32) * 4 * bitmap.bmHeight;
//为位图内容分配内存
hdib = GlobalAlloc(GHND, dwbmbitssize + dwpalettesize + sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib);
*lpbi = bi;
// 处理调色板
hpal = GetStockObject(DEFAULT_PALETTE);
if (hpal)
{
hdc = ::GetDC(NULL);
holdpal = SelectPalette(hdc, (HPALETTE)hpal, false);
RealizePalette(hdc);
}
// 获取该调色板下新的像素值
GetDIBits(hdc, hBitmap, 0, (UINT)bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwpalettesize, (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
//恢复调色板
if (holdpal)
{
SelectPalette(hdc, (HPALETTE)holdpal, true);
RealizePalette(hdc);
::ReleaseDC(NULL, hdc);
}
//创建位图文件
fh = CreateFile(szfilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
return false;
// 设置位图文件头
bmfhdr.bfType = 0x4d42; // "bm"
dwdibsize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
dwpalettesize + dwbmbitssize;
bmfhdr.bfSize = dwdibsize;
bmfhdr.bfReserved1 = 0;
bmfhdr.bfReserved2 = 0;
bmfhdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) +
(DWORD)sizeof(BITMAPINFOHEADER) + dwpalettesize;
//写入位图文件头
WriteFile(fh, (LPSTR)&bmfhdr, sizeof(BITMAPFILEHEADER), &dwwritten, NULL);
//写入位图文件其余内容
WriteFile(fh, (LPSTR)lpbi, dwdibsize, &dwwritten, NULL);
//清除
GlobalUnlock(hdib);
GlobalFree(hdib);
CloseHandle(fh);
}
HBITMAP memoryScreenCaptureBMP()
{
CURSORINFO pci;//定义光标结构体信息
//获取结构体大小,这一步必须要。否则后面就无法获取句柄
pci.cbSize = sizeof(CURSORINFO);
GetCursorInfo(&pci);//获取光标信息
POINT point = pci.ptScreenPos;//光标当前所在坐标位置
ICONINFO iconinfo;
GetIconInfo(pci.hCursor, &iconinfo);//主要用于获取光标对应的位图句柄,后面会在屏幕画出。
HDC hdc = CreateDC(("DISPLAY"), NULL, NULL, NULL);
HDC memdc = CreateCompatibleDC(hdc);//创建兼容屏幕的内存DC
HDC memdc1 = CreateCompatibleDC(hdc);
HDC memdc2 = CreateCompatibleDC(hdc);
SelectObject(memdc, iconinfo.hbmMask);//白底黑鼠标
SelectObject(memdc1, iconinfo.hbmColor);//黑底彩色鼠标
int srcxsize = GetSystemMetrics(SM_CXSCREEN);//获取屏幕设备尺寸信息
int srcysize = GetSystemMetrics(SM_CYSCREEN);
//创建与屏幕DC兼容的DIB位图,初始化尺寸为屏幕尺寸
HBITMAP bitmap = CreateCompatibleBitmap(hdc, srcxsize, srcysize);
SelectObject(memdc2, bitmap);
//将屏幕位图拷贝到兼容内存DC中。
BitBlt(memdc2, 0, 0, srcxsize, srcysize, hdc, 0, 0, SRCCOPY);
//将光标画到屏幕所在位置
BitBlt(memdc2, point.x, point.y, 20, 20, memdc, 0, 0, SRCAND);
BitBlt(memdc2, point.x, point.y, 20, 20, memdc1, 0, 0, SRCPAINT);//将带有鼠标的屏幕位图抓取
return bitmap;
}
int main(int argc, char* argv[])
{
HBITMAP bitmap = memoryScreenCaptureBMP();
SaveBitmapToFile(bitmap, "c:\\备用\\1.bmp");
printf("截图完成!");
getchar();
return 0;
}