HDC hScrDC,hMemDC;//screen and mem dc
HBITMAP hBitmap, hOldBitmap;//handle of bitmap
int xScrn, yScrn;//the resolution of screen
int nX, nY, nX2, nY2;
int nWidth, nHeight;
nX = rect.left;
nY = rect.top;
nX2 = rect.right;
nY2 = rect.bottom;
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);//create dc for screen
hMemDC = CreateCompatibleDC(hScrDC);//create compatible mem dc for screen dc
xScrn = GetDeviceCaps(hScrDC, HORZRES);//get resolution of screen
yScrn = GetDeviceCaps(hScrDC, VERTRES);
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
//hBitmap = CreateCompatibleBitmap(hMemDC, nWidth, nHeight);
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);//create compatible bitmap for screen dc
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);//select bitmap into the mem dc
BitBlt(hMemDC, 0, 0, nWidth, nHeight,hScrDC,nX,nY, SRCCOPY);//copy screen dc to mem
hBitmap =(HBITMAP)SelectObject(hMemDC, hOldBitmap);//get the handle of screen bitmap
DeleteDC(hScrDC);//clear
DeleteDC(hMemDC);
DeleteObject(hOldBitmap);
return hBitmap;//return the handle of bitmap