HBITMAP CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC;
HBITMAP hOldBitmap;
int nX, nY, nX2, nY2;
int nWidth, nHeight;
int xScrn, yScrn;
if (IsRectEmpty(lpRect))
return NULL;
hScrDC = CreateDC(_T('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;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
ShowWindow(SW_HIDE);
BitBlt(hMemDC, 0, 0, nWidth, nHeight,hScrDC, nX, nY, SRCCOPY);
hBitmap =(HBITMAP)SelectObject(hMemDC, hOldBitmap);
DeleteDC(hScrDC);
DeleteDC(hMemDC);
ShowWindow(SW_SHOW);
return hBitmap;
}
void OnBnClickedCopyscreen()
{
CRect rc;
GetDesktopWindow()->GetWindowRect(&rc);
hBitmap=CopyScreenToBitmap(&rc);
//复制到剪贴板
if (OpenClipboard())
{
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
}
}