使用PrintWindow来取得背景DC的图片

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

HBITMAP GetWindowBitmap(HWND hWnd)
{
typedef BOOL ( __stdcall *pPrintWindow )(HWND hWnd,HDC hdcBlt,UINT nFlags);
    RECT rect; 
 HMODULE h;
 h = LoadLibrary( "user32.dll" );
 pPrintWindow p;
 if( h )
 {
  p = ( pPrintWindow )::GetProcAddress( h, "PrintWindow" );
 }
 
    ::GetWindowRect(hWnd,&rect);
 
    HDC hScrDC=::GetDC(hWnd);                            //创建屏幕DC
    HDC hMemDC=CreateCompatibleDC(hScrDC);                //创建内存DC
    HBITMAP bitmap=::CreateCompatibleBitmap(hScrDC,rect.right-rect.left,rect.bottom-rect.top); //创建兼容位图
    HBITMAP OldBitmap=(HBITMAP)::SelectObject(hMemDC,bitmap);    //把位图选进内存DC
 
    p(hWnd,hMemDC,0);  
 
 HDC d;
 HWND hw;
 hw = ::GetDesktopWindow();
 d = ::GetWindowDC( hw );
 ::BitBlt( d, 0, 0, 200, 200, hMemDC, 0, 0, SRCCOPY );
 
    ::SelectObject(hMemDC,OldBitmap); 
 
    ::DeleteDC(hMemDC) ;            //删除内存DC
    ::ReleaseDC(NULL,hScrDC) ;    //释放屏幕DC
 
    return bitmap;
}


 

你可能感兴趣的:(c)