1:GDI
HDC g_hCaptureDC
void CaptureScreen(void) { RECT TaskBarRect; HWND hDesktopWnd = GetDesktopWindow(); HDC hDesktopDC = GetDC(hDesktopWnd); g_hCaptureDC = CreateCompatibleDC(hDesktopDC); HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight); SelectObject(g_hCaptureDC,hCaptureBitmap); BitBlt(g_hCaptureDC,0,0,nScreenWidth,nScreenHeight,hDesktopDC,0,0,SRCCOPY); ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteObject(hCaptureBitmap);
HWND HHTaskBar= FindWindow(L"HHTaskBar", NULL);//任务栏句柄 HDC hTaskDC =GetDC(HHTaskBar); g_hTaskBarDC=CreateCompatibleDC(hTaskDC); GetClientRect(HHTaskBar, &TaskBarRect); HBITMAP HHTaskBmp =CreateCompatibleBitmap(hTaskDC, nScreenWidth, 26); SelectObject(g_hTaskBarDC,HHTaskBmp); BitBlt(g_hTaskBarDC,0,0,nScreenWidth,26,hTaskDC,0,0,SRCCOPY); ReleaseDC(HHTaskBar,hTaskDC); DeleteObject(HHTaskBmp);
}
|
2:DirectDraw
HDC g_hCaptureDC BOOL CaptureScreen(void) { int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); int nScreenHeight = GetSystemMetrics(SM_CYSCREEN); LPDIRECTDRAW pDD = NULL; // DirectDraw object LPDIRECTDRAWSURFACE pDDSPrimary = NULL; // DirectDraw primary surface HDC hdc; BOOL ret = FALSE; HRESULT hRet;
hRet = DirectDrawCreate(NULL, &pDD, NULL); if (hRet != DD_OK) goto exit;
hRet = pDD->SetCooperativeLevel(NULL, DDSCL_NORMAL); if (hRet != DD_OK) goto exit;
DDSURFACEDESC ddsd; ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hRet = pDD->CreateSurface(&ddsd, &pDDSPrimary, NULL); if (hRet != DD_OK) goto exit;
if(pDDSPrimary->GetDC(&hdc) == DD_OK) { g_hCaptureDC = CreateCompatibleDC(hdc); HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hdc, nScreenWidth, nScreenHeight); SelectObject(g_hCaptureDC,hCaptureBitmap);
BitBlt(g_hCaptureDC,0,0,nScreenWidth,nScreenHeight,hdc,0,0,SRCCOPY);
DeleteObject(hCaptureBitmap); pDDSPrimary->ReleaseDC(hdc); }
ret = TRUE; exit: if(pDD != NULL) pDD->Release(); if(pDDSPrimary != NULL) pDDSPrimary->Release();
return ret; }
|