代码如下:
HDC hscrdc, hmemdc;
HBITMAP hbitmap, holdbitmap;
int x,y,w,h;
x = 200;
y = 200;
w = 300;
h = 300;
hscrdc = CreateDC("display", NULL, NULL, NULL);
hmemdc = CreateCompatibleDC(hscrdc);
hbitmap = CreateCompatibleBitmap(hscrdc, w, h);
holdbitmap = (HBITMAP)SelectObject(hmemdc, hbitmap);
BitBlt(hmemdc, 0, 0, w, h,hscrdc, x, y, SRCCOPY);
hbitmap = (HBITMAP)SelectObject(hmemdc, holdbitmap);
char* buffer = new char[500];
::GetCurrentDirectory(500,buffer);
CString fn;
fn = GetCurrentDateTimeYYYYMMDDHHMMSSMMM();
SaveBitmapToFile(hbitmap, (char*)(LPCTSTR)((CString)buffer + "\\jpg\\" + fn + ".bmp"));
DeleteDC(hscrdc);
DeleteDC(hmemdc);
运行结果文件: