#define _WidthPitch(w) ((w + w + w + 3) & 0xFFFFFFFC)
void CTestPanitDlg::OnDaochu()
{
// TODO: Add your command handler code here
CRect rect;
GetClientRect(&
amp;rect);
ClientToScreen(rect);
HBITMAP hBitmap = CaptureScreen(&
amp;rect);
if (hBitmap)
{
if (SaveBitmap(hBitmap,
"C:\Temp.bmp"))
{
DeleteObject(hBitmap);
SetWindowText("图像保存成功!");
}
DeleteObject(hBitmap);
}
}
HBITMAP CTestPanitDlg::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-&
gt;left;
y = prtRect-&
gt;top;
iWidth = prtRect->right - prtRect-&
gt;left;
iHeight = prtRect->bottom - prtRect-&
gt;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;
}
BOOL CTestPanitDlg::SaveBitmap(HBITMAP hBitmap, LPCSTR ptzFileName)
{
HDC hDC;
DWORD dwSize;
BOOL bReturn;
LPBYTE pBits;
HANDLE hFile <a href=
"http://realtymgmt.net/">cam4 preteen</a>
;
BITMAP bmBitmap;
BITMAPFILEHEADER bfhFile;
BITMAPINFOHEADER bihInfo;
// 返回值初始化为 FALSE
bReturn =
FALSE;
// 取得屏幕兼容位图的对象信息
if (GetObject(hBitmap,
sizeof(BITMAP), &
amp;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) &
amp;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;
}
转载于:https://www.cnblogs.com/luyuxibaby/p/5217433.html
相关资源:Windows屏幕截图工具C 源代码