把srcDC換成妳要保存的窗口的DC就行:
HBITMAP CopyScreenToBitmap(LPRECT lpRect)
{
int m_xScreen = GetSystemMetrics(SM_CXSCREEN);
//獲取y方向屏幕像素()
int m_yScreen = GetSystemMetrics(SM_CYSCREEN);
HDC hScrDC,hMemDC;
HBITMAP hBitmap,hOldBitmap;
int nX1,nX2,nY1,nY2;
int nWidth,nHeight;
if (IsRectEmpty(lpRect))
{
return FALSE;
}
//為屏幕創建設備描述表
hScrDC = CreateDC("DISPLAY",NULL,NULL,NULL);
//為屏幕設備描述表創建兼容的內存設備描述表
hMemDC = CreateCompatibleDC(hScrDC);
// 獲得選定區域坐標
nX1 = lpRect->left;
nY1 = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
//確保選定區域是可見的
if (nX1 < 0)
nX1 = 0;
if (nY1 < 0)
nY1 = 0;
if (nX2 > m_xScreen)
nX2 = m_xScreen;
if (nY2 > m_yScreen)
nY2 = m_yScreen;
nWidth = nX2 - nX1;
nHeight = nY2 - nY1;
// 創建壹個與屏幕設備描述表兼容的位圖
hBitmap = CreateCompatibleBitmap(hScrDC,nWidth,nHeight);
// 把新位圖選到內存設備描述表中
hOldBitmap = (HBITMAP)SelectObject(hMemDC,hBitmap);
// 把屏幕設備描述表拷貝到內存設備描述表中
BitBlt(hMemDC,0,0,nWidth,nHeight,hScrDC,nX1,nY1,SRCCOPY);
//得到屏幕位圖的句柄
hBitmap = (HBITMAP)SelectObject(hMemDC,hOldBitmap);
//清除
DeleteDC(hScrDC);
DeleteDC(hMemDC);
return hBitmap;
}