{
HDC hscrdc, hmemdc;// 屏幕和内存设备描述表
HBITMAP hbitmap, holdbitmap;// 位图句柄
int nx, ny, nx2, ny2;// 选定区域坐标
int nwidth, nheight;// 位图宽度和高度
int xscrn, yscrn;// 屏幕分辨率
// 确保选定区域不为空矩形
if (IsRectEmpty(lprect))
return NULL;
//为屏幕创建设备描述表
hscrdc = CreateDC(L"display", NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hmemdc = CreateCompatibleDC(hscrdc);
// 获得选定区域坐标
nx = lprect->left;
ny = lprect->top;
nx2 = lprect->right;
ny2 = lprect->bottom;
// 获得屏幕分辨率
xscrn = GetDeviceCaps(hscrdc, HORZRES);
yscrn = GetDeviceCaps(hscrdc, VERTRES);
//确保选定区域是可见的
if (nx < 0)
nx = 0;
if (ny < 0)
ny = 0;
if (nx2 > xscrn)
nx2 = xscrn;
if (ny2 > yscrn)
ny2 = yscrn;
nheight = ny2 - ny;
hbitmap = CreateCompatibleBitmap(hscrdc, nwidth, nheight);
holdbitmap = (HBITMAP)SelectObject(hmemdc, hbitmap);
// 把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hmemdc, 0, 0, nwidth, nheight,hscrdc, nx, ny, SRCCOPY);
//得到屏幕位图的句柄
hbitmap = (HBITMAP)SelectObject(hmemdc, holdbitmap);
DeleteDC(hscrdc);
DeleteDC(hmemdc);
return hbitmap;
}