如何使用vc ++捕获桌面

时间:2010-11-10 12:05:19

标签: visual-c++ desktop screen capture

我正在尝试捕获桌面。我找到了一些捕获桌面的代码,但需要一些时间。

void CSDITESTView::OnFileTest()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);

HWND hDesktopWnd = GetDesktopWindow()->m_hWnd;

HDC hDesktopDC = ::GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap); 
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight, hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); 
::ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteDC(hCaptureDC);
}

但是我需要能够快速捕捉并将其保存为图像的代码。

先谢谢..

1 个答案:

答案 0 :(得分:0)

我使用性能计数器函数测试了您的代码,如下所示:

void CMainFrame::OnTestTest()
{
   LARGE_INTEGER lFreq, lStart, lEnd;
   ::QueryPerformanceFrequency(&lFreq);
   ::QueryPerformanceCounter(&lStart);

   /// BEGIN BENCHMARK ///
   int nScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
   int nScreenHeight = ::GetSystemMetrics(SM_CYSCREEN);

   HWND hDesktopWnd = ::GetDesktopWindow();

   HDC hDesktopDC = ::GetDC(hDesktopWnd);
   HDC hCaptureDC = ::CreateCompatibleDC(hDesktopDC);
   HBITMAP hCaptureBitmap = ::CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight);
   ::SelectObject(hCaptureDC,hCaptureBitmap); 
   ::BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight, hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); 
   ::ReleaseDC(hDesktopWnd,hDesktopDC);
   ::DeleteDC(hCaptureDC);
   /// END BENCHMARK ///

   ::QueryPerformanceCounter(&lEnd);
   double dTime = ((double)lEnd.QuadPart - lStart.QuadPart) / lFreq.QuadPart;
   // ..
}

结果时间约为 80微秒。 我不认为你的电脑速度令人难以置信。可能问题出在另一方面(例如线程同步问题)。