关闭

winapi 截屏函数(转)

227人阅读 评论(0) 收藏 举报
void screen(wchar_t* fileName)
  {
   HWND window = ::GetDesktopWindow();
   HDC _dc = ::GetWindowDC(window);//屏幕DC
   HDC dc = CreateCompatibleDC(0);//内存DC
   
   RECT re;
   ::GetWindowRect(window, &re);
   int w = re.right,
   h = re.bottom;
   void* buf = new char[w*h*4];
   
   HBITMAP bm = CreateCompatibleBitmap(_dc, w, h);//建立和屏幕兼容的bitmap
   SelectObject(dc, bm);//将memBitmap选入内存DC 
   StretchBlt(dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY);//复制屏幕图像到内存DC
   
   void* f =CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
   
   GetObject(bm, 84, buf);
   
   tagBITMAPINFO bi;
   bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
   bi.bmiHeader.biWidth = w;
   bi.bmiHeader.biHeight = h;
   bi.bmiHeader.biPlanes = 1;
   bi.bmiHeader.biBitCount = 32;
   bi.bmiHeader.biCompression = 0;
   bi.bmiHeader.biSizeImage = 0;
   
   CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &buf, 0, 0);
   GetDIBits(dc, bm, 0, h, buf, &bi, DIB_RGB_COLORS);
   
   BITMAPFILEHEADER bif;
   bif.bfType = MAKEWORD('B','M');
   bif.bfSize = w*h*4+54;
   bif.bfOffBits = 54;
  
   BITMAPINFOHEADER bii;
   bii.biSize = 40;
   bii.biWidth = w;
   bii.biHeight = h;
   bii.biPlanes = 1;
   bii.biBitCount = 32;
   bii.biCompression = 0;
   bii.biSizeImage = w*h*4;
   
   DWORD r;
   WriteFile(f, &bif, sizeof(bif), &r, NULL);
   WriteFile(f, &bii, sizeof(bii), &r, NULL);
   WriteFile(f, buf, w*h*4, &r, NULL);
  
   CloseHandle(f);
   DeleteDC(_dc);
   DeleteDC(dc);
  }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8202次
    • 积分:110
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:11篇
    • 译文:0篇
    • 评论:7条
    文章分类
    文章存档
    最新评论