返回> 网站首页 

屏幕截图(带光标)

yoours2011-03-23 13:30:40 阅读 1249

简介一边听听音乐,一边写写文章。

  1. // 屏幕截图程序,可运行PC,WinCE,Windows Mobile平台上  
  2.   
  3. void SaveScreenToFile(LPCTSTR szFileName)  
  4. {  
  5.     HDC hScrDC = ::GetDC(NULL);  
  6.     HDC hMemDC = NULL;  
  7.       
  8.     BYTE *lpBitmapBits = NULL;   
  9.       
  10.     int nWidth = GetSystemMetrics(SM_CXSCREEN);  
  11.     int nHeight = GetSystemMetrics(SM_CYSCREEN);   
  12.       
  13.     hMemDC = ::CreateCompatibleDC(hScrDC);   
  14.       
  15.     BITMAPINFO bi;   
  16.     ZeroMemory(&bi, sizeof(BITMAPINFO));  
  17.     bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
  18.     bi.bmiHeader.biWidth = nWidth;  
  19.     bi.bmiHeader.biHeight = nHeight;  
  20.     bi.bmiHeader.biPlanes = 1;  
  21.     bi.bmiHeader.biBitCount = 24;  
  22.   
  23.     HCURSOR hCursor = GetCursor();  
  24.     POINT ptCursor;  
  25.     GetCursorPos(&ptCursor);  
  26.     ICONINFO IconInfo = {0};  
  27.     if(GetIconInfo(hCursor, &IconInfo))  
  28.     {  
  29.         ptCursor.x -= IconInfo.xHotspot;  
  30.         ptCursor.y -= IconInfo.yHotspot;  
  31.         if(NULL != IconInfo.hbmMask)  
  32.             DeleteObject(IconInfo.hbmMask);  
  33.         if(NULL != IconInfo.hbmColor)  
  34.             DeleteObject(IconInfo.hbmColor);  
  35.     }  
  36.       
  37.       
  38.     HBITMAP bitmap = ::CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0);  
  39.     HGDIOBJ oldbmp = ::SelectObject(hMemDC, bitmap);   
  40.       
  41.     ::BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);  
  42.     DrawIconEx(hMemDC, ptCursor.x, ptCursor.y, hCursor, 0, 0, 0, NULL, DI_NORMAL | DI_COMPAT);  
  43.   
  44.     BITMAPFILEHEADER bh;  
  45.     ZeroMemory(&bh, sizeof(BITMAPFILEHEADER));  
  46.     bh.bfType = 0x4d42;  //bitmap    
  47.     bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
  48.     bh.bfSize = bh.bfOffBits + ((nWidth*nHeight)*3);  
  49.       
  50.     CFile file;  
  51.     if(file.Open(szFileName, CFile::modeCreate | CFile::modeWrite))  
  52.     {    
  53.         file.Write(&bh, sizeof(BITMAPFILEHEADER));  
  54.         file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));  
  55.         file.Write(lpBitmapBits, 3 * nWidth * nHeight);  
  56.         file.Close();  
  57.     }  
  58.       
  59.     ::SelectObject(hMemDC, oldbmp);  
  60.     ::DeleteObject(bitmap);  
  61.     ::DeleteObject(hMemDC);  
  62.     ::ReleaseDC(NULL, hScrDC);  
  63. }  
微信小程序扫码登陆

文章评论

1249人参与,0条评论