屏幕截图(带光标)

来源:互联网 发布:手机学打字软件 编辑:程序博客网 时间:2024/05/16 01:28
    // 屏幕截图程序,可运行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);      }