使用PrintWindow来取得某窗口的DC图片本来是有定论的,不过因为直接编译不能通过,所以LoadLibrary一下了。

  1. HBITMAP GetWindowBitmap(HWND hWnd)  
  2. {  
  3. typedef BOOL ( __stdcall *pPrintWindow )(HWND hWnd,HDC hdcBlt,UINT nFlags);  
  4.     RECT rect;   
  5.  HMODULE h;  
  6.  h = LoadLibrary( "user32.dll" );  
  7.  pPrintWindow p;  
  8.  if( h )  
  9.  {  
  10.   p = ( pPrintWindow )::GetProcAddress( h, "PrintWindow" );  
  11.  }  
  12.    
  13.     ::GetWindowRect(hWnd,&rect);  
  14.    
  15.     HDC hScrDC=::GetDC(hWnd);                            //创建屏幕DC  
  16.     HDC hMemDC=CreateCompatibleDC(hScrDC);                //创建内存DC  
  17.     HBITMAP bitmap=::CreateCompatibleBitmap(hScrDC,rect.right-rect.left,rect.bottom-rect.top); //创建兼容位图  
  18.     HBITMAP OldBitmap=(HBITMAP)::SelectObject(hMemDC,bitmap);    //把位图选进内存DC  
  19.    
  20.     p(hWnd,hMemDC,0);    
  21.    
  22.  HDC d;  
  23.  HWND hw;  
  24.  hw = ::GetDesktopWindow();  
  25.  d = ::GetWindowDC( hw );  
  26.  ::BitBlt( d, 0, 0, 200, 200, hMemDC, 0, 0, SRCCOPY );  
  27.    
  28.     ::SelectObject(hMemDC,OldBitmap);   
  29.    
  30.     ::DeleteDC(hMemDC) ;            //删除内存DC  
  31.     ::ReleaseDC(NULL,hScrDC) ;    //释放屏幕DC  
  32.    
  33.     return bitmap;  
  34. }  
posted on 2014-10-15 17:14  归海一刀  阅读(1994)  评论(0编辑  收藏  举报