/************************************************************************* 
 * 
 * CopyScreenToBitmap() 
 * 
 * Parameter: 
 * 
 * LPRECT lpRect    - specifies the window 
 * 
 * Return Value: 
 * 
 * HDIB             - identifies the device-dependent bitmap 
 * 
 * Description: 
 * 
 * This function copies the specified part of the screen to a device- 
 * dependent bitmap. 
 * 
 * 
 ************************************************************************/ 
void CDib::CopyFromScreen(const CRect& rectIn) 
{ 
    // check for an empty rectangle 
	if (rectIn.IsRectEmpty()) 
	{
		Empty();
		return;
	}

	CDC ScrDC;
	CDC MemDC;
	// create a DC for the screen and create 
    // a memory DC compatible to screen DC 
    ScrDC.CreateDCW(L"DISPLAY", NULL, NULL, NULL); 
    MemDC.CreateCompatibleDC(&ScrDC); 
 
	

    // get screen resolution 
    int xScrn = ScrDC.GetDeviceCaps(HORZRES); 
    int yScrn = ScrDC.GetDeviceCaps(VERTRES); 

	CRect rect(rectIn);
	rect.NormalizeRect();

	//make sure bitmap rectangle is visible 
	if (rect.left < 0) 
        rect.left = 0; 
    if (rect.top < 0) 
        rect.top = 0; 
    if (rect.right > xScrn) 
        rect.right = xScrn; 
    if (rect.bottom> yScrn) 
        rect.bottom = yScrn; 
 
    // create a bitmap compatible with the screen DC 

	
	CreateDib( CSize(rect.Width(), rect.Height()), 32);
	CBitmap* pBitmap = CBitmap::FromHandle( (HBITMAP)(*this));
	//pBitmap->GetBitmap(&bmp);

	//ASSERT(false);
	//HBITMAP hBitmap = NULL;//C reateCompatibleDIB( ScrDC, rect.Width(), rect.Height());
 
    // select new bitmap into memory DC 
	CBitmap* pOldBitmap = MemDC.SelectObject(pBitmap); 
	//HBITMAP hOldBitmap = (HBITMAP)MemDC.SelectObject((hBitmap); 
 
	
    // bitblt screen DC to memory DC 
	MemDC.BitBlt(0, 0, rect.Width(), rect.Height(), &ScrDC, rect.left, rect.top, SRCCOPY); 

	//CreatePaletteFromImage(m_palette);
 
    // select old bitmap back into memory DC and get handle to 
    // bitmap of the screen 
    //hBitmap = (HBITMAP)MemDC.SelectObject(hOldBitmap); 
	MemDC.SelectObject(pOldBitmap); 
	
 
    // clean up 
    ScrDC.DeleteDC(); 
    MemDC.DeleteDC(); 
}