64,650
社区成员
发帖
与我相关
我的任务
分享
//GDI与DX截屏API操作
LPDIRECTDRAW lpDD = NULL;
LPDIRECTDRAWSURFACE lpDDSPrime = NULL;
LPDIRECTDRAWSURFACE lpDDSBack = NULL;
LPDIRECTDRAWSURFACE lpDDSGdi = NULL;
LPDIRECTDRAWSURFACE lpSurf = NULL;
DDSURFACEDESC DDSdesc;
BOOL m_b24=TRUE;
//rfbServerInitMsg m_scrinfo;
RECT m_bmrect;
struct _BMInfo {
BITMAPINFO bmi ;
BOOL truecolour;
RGBQUAD cmap[256] ;
} m_bminfo; // 用来保存位图信息的结构
int DX_Init() {// DirectX初始化。返回当前表面获取一张屏幕位图的存储空间大小
HRESULT hr;
// 初始化directX
hr = DirectDrawCreate(0, &lpDD, 0);
if (FAILED(hr)) return FALSE;
hr = lpDD->SetCooperativeLevel(NULL, DDSCL_NORMAL);
if (FAILED(hr)) return FALSE;
ZeroMemory(&DDSdesc, sizeof(DDSdesc));
DDSdesc.dwSize = sizeof(DDSdesc);
DDSdesc.dwFlags = DDSD_CAPS;
DDSdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hr = lpDD->CreateSurface(&DDSdesc, &lpDDSPrime, 0);
if (FAILED(hr)) return FALSE;
hr = lpDD->GetGDISurface(&lpDDSGdi);
if (FAILED(hr)) return FALSE;
ZeroMemory(&DDSdesc, sizeof(DDSdesc));
DDSdesc.dwSize = sizeof(DDSdesc);
DDSdesc.dwFlags = DDSD_ALL;
hr = lpDDSPrime->GetSurfaceDesc(&DDSdesc);
if (FAILED(hr)) return FALSE;
// 初始化位图信息
if ((DDSdesc.dwFlags & DDSD_WIDTH) && (DDSdesc.dwFlags & DDSD_HEIGHT)) {
m_bmrect.left = m_bmrect.top = 0;
m_bmrect.right = DDSdesc.dwWidth;
m_bmrect.bottom = DDSdesc.dwHeight;
} else return FALSE;
m_bminfo.bmi.bmiHeader.biCompression = BI_RGB;//BI_BITFIELDS;
m_bminfo.bmi.bmiHeader.biBitCount = DDSdesc.ddpfPixelFormat.dwRGBBitCount;
// m_bminfo.truecolour = DDSdesc.ddpfPixelFormat.dwFlags & DDPF_RGB;
if (m_bminfo.bmi.bmiHeader.biBitCount > 8)
m_bminfo.truecolour = TRUE;
else
m_bminfo.truecolour = FALSE;
ZeroMemory(&DDSdesc, sizeof(DDSdesc));
DDSdesc.dwSize = sizeof(DDSdesc);
DDSdesc.dwFlags = DDSD_CAPS | DDSD_HEIGHT |DDSD_WIDTH;
DDSdesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
DDSdesc.dwHeight = m_bmrect.bottom - m_bmrect.top;
DDSdesc.dwWidth = m_bmrect.right - m_bmrect.left;
hr = lpDD->CreateSurface(&DDSdesc, &lpDDSBack, 0);
if (FAILED(hr)) return FALSE;
// hr = lpDDSPrime->QueryInterface( IID_IDirectDrawSurface3, (LPVOID *)&lpSurf);
// if (FAILED(hr)) return FALSE;
switch (m_bminfo.bmi.bmiHeader.biBitCount) {
case 32:
case 24:
// Update the bitmapinfo header
m_b24 = TRUE;
m_bminfo.bmi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
m_bminfo.bmi.bmiHeader.biWidth = 1024;
m_bminfo.bmi.bmiHeader.biHeight = 768;
m_bminfo.bmi.bmiHeader.biPlanes = 1;
// m_bminfo.bmi.bmiHeader.biBitCount = 24;
m_bminfo.bmi.bmiHeader.biCompression = BI_RGB;
m_bminfo.bmi.bmiHeader.biSizeImage = abs((m_bminfo.bmi.bmiHeader.biWidth * m_bminfo.bmi.bmiHeader.biHeight * m_bminfo.bmi.bmiHeader.biBitCount)/8);
m_bminfo.bmi.bmiHeader.biXPelsPerMeter = (1024*1000)/1024;
m_bminfo.bmi.bmiHeader.biYPelsPerMeter = (768*1000)/768;
m_bminfo.bmi.bmiHeader.biClrUsed = 0;
m_bminfo.bmi.bmiHeader.biClrImportant = 0;
break;
}
return m_bminfo.bmi.bmiHeader.biSizeImage;
}
BOOL CaptureScreen(RECT &rect, BYTE *scrBuff, UINT scrBuffSize) {// 捕捉屏幕。rect: 区域。scrBuff: 输出缓冲。scrBuffSize: 缓冲区大小
HRESULT hr=0;
hr = lpDDSBack->BltFast(rect.left,rect.top,lpDDSPrime,&rect,DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
if (FAILED(hr)) return FALSE;
DDSURFACEDESC surfdesc;
ZeroMemory(&surfdesc, sizeof(surfdesc));
surfdesc.dwSize = sizeof(surfdesc);
hr = lpDDSBack->Lock(&rect, &surfdesc, DDLOCK_READONLY | DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR /*|DDLOCK_NOSYSLOCK*/, NULL);
// hr = lpDDSPrime->Lock(&rect, &surfdesc, DDLOCK_READONLY | DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR /*|DDLOCK_NOSYSLOCK*/, NULL);
if (FAILED(hr)) return FALSE;
// copy the data into our buffer
BYTE * destbuffpos, * srcbuffpos;
// m_scrinfo.format.bitsPerPixel = 24;
srcbuffpos = (BYTE *) surfdesc.lpSurface;
destbuffpos = scrBuff;
memcpy( destbuffpos, srcbuffpos,m_bminfo.bmi.bmiHeader.biSizeImage);
// unlock the primary surface
// lpDDSPrime->Unlock(surfdesc.lpSurface);
lpDDSBack->Unlock(surfdesc.lpSurface);
return TRUE;
}