Guest User

Window to bitmap converter

a guest
Apr 3rd, 2010
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. HBITMAP WindowToBitmap(HWND hwnd,RECT* dims=NULL){
  2. HDC hdcWnd,hdcBitmap;
  3. int w=100,h=100;
  4. HBITMAP hbmBitmap;
  5. HBITMAP hbmOld;
  6. hdcWnd=GetDC(hwnd);
  7.  if(hdcWnd){
  8.  RECT wnd;
  9.     if(dims==NULL){
  10.     GetClientRect(hwnd,&wnd);
  11.     }
  12.     else{
  13.     CopyRect(&wnd,dims);
  14.     }
  15.  w=wnd.right;
  16.  h=wnd.bottom;
  17.  hbmBitmap=(HBITMAP)CreateCompatibleBitmap(hdcWnd,w,h);
  18.  }
  19.  if(!hbmBitmap){
  20.   if(hdcWnd){
  21.   RECT wnd;
  22.   GetWindowRect(hwnd,&wnd);
  23.   w=wnd.right;
  24.   h=wnd.bottom;
  25.   hbmBitmap=CreateCompatibleBitmap(hdcWnd,w,h);
  26.   }
  27.  }
  28.  if(!hbmBitmap){
  29.  std::cout<<"Bitmap missing! "<<std::endl;
  30.  }
  31. hdcBitmap=CreateCompatibleDC(hdcWnd);
  32. hbmOld=(HBITMAP)SelectObject((HDC)hdcBitmap,(HGDIOBJ)(HBITMAP)hbmBitmap);
  33. BitBlt(hdcBitmap,0,0,w,h,hdcWnd,0,0,SRCCOPY);
  34. SelectObject((HDC)hdcBitmap,(HGDIOBJ)(HBITMAP)hbmOld);
  35. DeleteDC(hdcBitmap);
  36. ReleaseDC(hwnd,hdcWnd);
  37. return hbmBitmap;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment