一百年前的示例代码,贴出来算权当收藏![Emotion](http://yonsm.reg365.com/img/emot/laugh.gif)
![Emotion](http://yonsm.reg365.com/img/emot/laugh.gif)
// 预处理
#include
// 每行数据对齐起到四个字节,下面的定义仅仅用于 24 位色 DIB
#define _WidthPitch(w) ((w + w + w + 3) & 0xFFFFFFFC)
// 函数声明
HBITMAP WINAPI CaptureScreen(LPCRECT prtRect);
BOOL WINAPI SaveBitmap(HBITMAP hBitmap, LPCSTR ptzFileName);
// 程序入口,使用自定义入口将生成非常小的 PE 文件,仅有 3K
#ifndef _DEBUG
#pragma comment(linker, "/ENTRY:EntryPoint")
VOID WINAPI EntryPoint()
{
ExitProcess(WinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOWNORMAL));
}
#endif // _DEBUG
// 主函数
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pszCmdLine, INT iCmdShow)
{
HBITMAP hBitmap;
hBitmap = CaptureScreen(NULL);
if (hBitmap)
{
if (SaveBitmap(hBitmap, "C://CaptureScreen.bmp"))
{
DeleteObject(hBitmap);
MessageBox(NULL, "屏幕图像保存在 C://CaptureScreen.bmp", "完成", MB_ICONINFORMATION);
return 0;
}
DeleteObject(hBitmap);
}
MessageBox(NULL, "不知道出了什么错误:)……", "错误", MB_ICONSTOP);
return -1;
}
// 捕捉窗口窗口矩形区域。prtRect 为要捕捉的矩形区域,如果为 NULL,则捕捉整个窗口
HBITMAP WINAPI CaptureScreen(LPCRECT prtRect)
{
INT x;
INT y;
HDC hdcSrc;
HDC hdcMem;
INT iWidth;
INT iHeight;
HBITMAP hbmpTemp;
HBITMAP hbmpReturn;
// 创建屏幕设备场景
hdcSrc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
// 设置捕捉矩形
if (prtRect == NULL)
{
x = 0;
y = 0;
iWidth = GetDeviceCaps(hdcSrc, HORZRES);
iHeight = GetDeviceCaps(hdcSrc, VERTRES);
}
else
{
x = prtRect->left;
y = prtRect->top;
iWidth = prtRect->right - prtRect->left;
iHeight = prtRect->bottom - prtRect->top;
}
// 创建内存兼容设备场景
hdcMem = CreateCompatibleDC(hdcSrc);
hbmpReturn = CreateCompatibleBitmap(hdcSrc, iWidth, iHeight);
// 传送位图
hbmpTemp = (HBITMAP) SelectObject(hdcMem, hbmpReturn);
BitBlt(hdcMem, 0, 0, iWidth, iHeight, hdcSrc, x, y, SRCCOPY);
SelectObject(hdcMem, hbmpTemp);
// 删除设备场景
DeleteDC(hdcMem);
DeleteDC(hdcSrc);
return hbmpReturn;
}
// 保存屏幕兼容位图为 24 位色位图文件
BOOL WINAPI SaveBitmap(HBITMAP hBitmap, LPCSTR ptzFileName)
{
HDC hDC;
DWORD dwSize;
BOOL bReturn;
LPBYTE pBits;
HANDLE hFile;
BITMAP bmBitmap;
BITMAPFILEHEADER bfhFile;
BITMAPINFOHEADER bihInfo;
// 返回值初始化为 FALSE
bReturn = FALSE;
// 取得屏幕兼容位图的对象信息
if (GetObject(hBitmap, sizeof(BITMAP), &bmBitmap))
{
// 设置 BITMAPINFOHEADER 结构
bihInfo.biSize = sizeof(BITMAPINFOHEADER);
bihInfo.biWidth = bmBitmap.bmWidth;
bihInfo.biHeight = bmBitmap.bmHeight;
bihInfo.biPlanes = 1;
bihInfo.biBitCount = 24;
bihInfo.biCompression = BI_RGB;
bihInfo.biSizeImage = _WidthPitch(bmBitmap.bmWidth) * bmBitmap.bmHeight;
bihInfo.biXPelsPerMeter = 0;
bihInfo.biYPelsPerMeter = 0;
bihInfo.biClrUsed = 0;
bihInfo.biClrImportant = 0;
// 设置 BITMAPFILEHEADER 结构
bfhFile.bfType = 0x4D42; // "BM"
bfhFile.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bihInfo.biSizeImage;
bfhFile.bfReserved1 = 0;
bfhFile.bfReserved2 = 0;
bfhFile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// 分配内存
pBits = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, bihInfo.biSizeImage);
if (pBits)
{
// 获取屏幕设备场景
hDC = CreateIC("DISPLAY", NULL, NULL, NULL);
// 获取 DIB 数据
if (GetDIBits(hDC, hBitmap, 0, bmBitmap.bmHeight,
pBits, (LPBITMAPINFO) &bihInfo, DIB_RGB_COLORS))
{
// 创建文件
hFile = CreateFile(ptzFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
// 写入 BITMAPFILEHEADER 信息
bReturn = WriteFile(hFile, &bfhFile, sizeof(BITMAPFILEHEADER), &dwSize, 0);
if (bReturn)
{
// 写入 BITMAPINFOHEADER 信息
bReturn = WriteFile(hFile, &bihInfo, sizeof(BITMAPINFOHEADER), &dwSize, 0);
if (bReturn)
{
// 写入 DIB 数据
bReturn = WriteFile(hFile, pBits, bihInfo.biSizeImage, &dwSize, 0);
}
}
// 关闭文件句柄
CloseHandle(hFile);
}
}
// 删除设备场景,释放内存
DeleteDC(hDC);
HeapFree(GetProcessHeap(), 0, (LPVOID) pBits);
}
}
return bReturn;
}