原文链接地址:http://blog.csdn.net/VisualEleven/article/details/6093484
// 屏幕截图程序,可运行PC,WinCE,Windows Mobile平台上 void SaveScreenToFile(LPCTSTR szFileName) { HDC hScrDC = ::GetDC(NULL); HDC hMemDC = NULL; BYTE *lpBitmapBits = NULL; int nWidth = GetSystemMetrics(SM_CXSCREEN); int nHeight = GetSystemMetrics(SM_CYSCREEN); hMemDC = ::CreateCompatibleDC(hScrDC); BITMAPINFO bi; ZeroMemory(&bi, sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth = nWidth; bi.bmiHeader.biHeight = nHeight; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 24; HCURSOR hCursor = GetCursor(); POINT ptCursor; GetCursorPos(&ptCursor); ICONINFO IconInfo = {0}; if(GetIconInfo(hCursor, &IconInfo)) { ptCursor.x -= IconInfo.xHotspot; ptCursor.y -= IconInfo.yHotspot; if(NULL != IconInfo.hbmMask) DeleteObject(IconInfo.hbmMask); if(NULL != IconInfo.hbmColor) DeleteObject(IconInfo.hbmColor); } HBITMAP bitmap = ::CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0); HGDIOBJ oldbmp = ::SelectObject(hMemDC, bitmap); ::BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY); DrawIconEx(hMemDC, ptCursor.x, ptCursor.y, hCursor, 0, 0, 0, NULL, DI_NORMAL | DI_COMPAT); BITMAPFILEHEADER bh; ZeroMemory(&bh, sizeof(BITMAPFILEHEADER)); bh.bfType = 0x4d42; //bitmap bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bh.bfSize = bh.bfOffBits + ((nWidth*nHeight)*3); CFile file; if(file.Open(szFileName, CFile::modeCreate | CFile::modeWrite)) { file.Write(&bh, sizeof(BITMAPFILEHEADER)); file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER)); file.Write(lpBitmapBits, 3 * nWidth * nHeight); file.Close(); } ::SelectObject(hMemDC, oldbmp); ::DeleteObject(bitmap); ::DeleteObject(hMemDC); ::ReleaseDC(NULL, hScrDC); }
BOOL DrawIconEx( HDC hdc, int xLeft, int yTop, HICON hIcon, int cxWidth, int cyWidth, UINT istepIfAniCur, HBRUSH hbrFlickerFreeDraw, UINT diFlags ); //The DrawIconEx function draws an icon or cursor into the specified device context, performing the specified raster operations, and stretching or compressing the icon or cursor as specified.