void OnSaveScreen()
{
HDC hScrDC = NULL, hMemDC = NULL;
int width, height;
BYTE *lpBitmapBits = NULL;
//hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
hScrDC = GetDC(NULL);
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
hMemDC = CreateCompatibleDC(hScrDC);
BITMAPINFO bmInfo;
ZeroMemory(&bmInfo, sizeof(bmInfo));
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biWidth = width;
bmInfo.bmiHeader.biHeight = height;
bmInfo.bmiHeader.biBitCount = 24;
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO *)&bmInfo, DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0); //创建一个bitmap数据
HGDIOBJ preObject = SelectObject(hMemDC, directBmp);
BOOL ret = BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
if (!ret)
{
MessageBox(NULL, _T("您的电脑有点与众不同!"), _T("提示:"), MB_OK);
}
BYTE TempBits[100] = {0,0,0,0};
if ( memcmp(lpBitmapBits, TempBits, 100) == 0 )
{
MessageBox(NULL, _T("您的电脑非常与众不同!"), _T("提示:"), MB_OK);
}
//-----------------------------
LOGFONT lf = {0};
lf.lfHeight = 40;
lf.lfWidth = 20;
lf.lfWeight = 600;
lf.lfCharSet = DEFAULT_CHARSET;
wcscpy(lf.lfFaceName, _T("隶书"));
HFONT hFont = CreateFontIndirect(&lf);
HFONT hFontOld = (HFONT)SelectObject(hMemDC, hFont);
SetTextColor(hMemDC, RGB(255, 0, 0));
SetBkMode(hMemDC, TRANSPARENT);
SYSTEMTIME createTime;
GetLocalTime(&createTime);
WORD wYear = createTime.wYear;
WORD wMonth = createTime.wMonth;
WORD wDay = createTime.wDay;
WORD wHour = createTime.wHour;
WORD wMinute = createTime.wMinute;
WORD wSecond = createTime.wSecond;
TCHAR info[_MAX_PATH] = {0};
swprintf(info, _T("摄于%04d年%02d月%02d日%02d时%02d分%02d秒"), wYear, wMonth, wDay, wHour, wMinute, wSecond);
TextOut(hMemDC, int(width/2)+20, height-80, info, wcslen(info));
SelectObject(hMemDC, hFontOld);
DeleteObject(hFont);
//----------------------------
SelectObject(hMemDC, preObject);
BITMAPFILEHEADER bmfh;
ZeroMemory(&bmfh, sizeof(bmfh));
bmfh.bfType = 0x4d42;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfSize = bmfh.bfOffBits + width*height*4;
FILE *mStream = NULL;
TCHAR fileName[_MAX_PATH] = {0};//_T("C:\\Users\\Administrator\\Desktop\\test.bmp");
TCHAR deskPath[_MAX_PATH] = {0};
SHGetSpecialFolderPath(NULL, deskPath, CSIDL_DESKTOPDIRECTORY, FALSE); //获取桌面路径
swprintf(fileName, _T("%s\\%04d%02d%02d%02d%02d%02d.bmp"), deskPath, wYear, wMonth, wDay, wHour, wMinute, wSecond);
if((mStream = _tfopen(fileName, L"wb")))
{
//write bitmap file header
fwrite(&bmfh, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(bmInfo.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data
fwrite(lpBitmapBits, width*height*3, 1, mStream);
//close file
fclose(mStream);
}
DeleteObject(directBmp);
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(preObject);
}
vs2008 Win7环境下的截图程序代码
最新推荐文章于 2021-09-30 00:15:00 发布