19,469
社区成员
发帖
与我相关
我的任务
分享
int CDemoDlg::Capture()
{
LPDIRECT3D9 g_pD3D = NULL;
D3DDISPLAYMODE ddm;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DDevice9 *g_pd3dDevice;
IDirect3DSurface9 *pSurface;
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
ZeroMemory( &d3dpp, sizeof(d3dpp) );
if((g_pD3D=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
{
return 1;
}
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&ddm)))
{
return 1;
}
d3dpp.Windowed=TRUE;
d3dpp.Flags=D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.BackBufferFormat=ddm.Format;
d3dpp.BackBufferHeight=ddm.Height;
d3dpp.BackBufferWidth=ddm.Width;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow=::GetDesktopWindow();
d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT;
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,::GetDesktopWindow(),D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&d3dpp,&g_pd3dDevice)))
{
return 1;
}
if(FAILED(g_pd3dDevice->CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL)))
{
return 1;
}
g_pd3dDevice->CreateOffscreenPlainSurface(1024, 768,
D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);
g_pd3dDevice->GetFrontBufferData(0, pSurface);
char szFileName[512];
CTime tmNow = CTime::GetCurrentTime();
wsprintf(szFileName,"%s\\%04d-%02d-%02d %02d%02d%02d.jpg",GetExePath(),tmNow.GetYear(),
tmNow.GetMonth(),tmNow.GetDay(),tmNow.GetHour(),tmNow.GetMinute(),tmNow.GetSecond());
D3DXSaveSurfaceToFile(szFileName,D3DXIFF_JPG,pSurface,NULL,NULL);//保存为 jpg格式
pSurface->Release();
g_pd3dDevice->Release();
g_pD3D->Release ();
return 0;
}
void CDemoDlg::CaptureD3D()
{
char szFileName[512];
CTime tmNow = CTime::GetCurrentTime();
wsprintf(szFileName,"%s\\%04d-%02d-%02d %02d%02d%02d.jpg",GetExePath(),tmNow.GetYear(),
tmNow.GetMonth(),tmNow.GetDay(),tmNow.GetHour(),tmNow.GetMinute(),tmNow.GetSecond());
HWND hWnd = ::GetDesktopWindow();//m_hWnd;//
HWND hCS = ::FindWindow(_T("Valve001"),_T("Counter-Strike"));
::SetActiveWindow(hCS);
if(FAILED(InitD3D(::GetDesktopWindow())))
return;
g_pd3dDevice->GetFrontBufferData(0, g_pSurface);
D3DXSaveSurfaceToFile(szFileName,D3DXIFF_JPG,g_pSurface,NULL,NULL); //Save to File
D3DLOCKED_RECT lockedRect;
if(FAILED(g_pSurface->LockRect(&lockedRect,NULL,D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY)))
{
ErrorMessage("Unable to Lock Front Buffer Surface");
return;
}
for(int i=0;i<gScreenRect.bottom;i++)
{
memcpy((BYTE*)pBits+(gScreenRect.bottom-i-1)*gScreenRect.right*BITSPERPIXEL/8,(BYTE*)lockedRect.pBits+i*lockedRect.Pitch,gScreenRect.right*BITSPERPIXEL/8);//g_d3dpp.BackBufferHeight*g_d3dpp.BackBufferWidth*4);
}
g_pSurface->UnlockRect();
::InvalidateRect(hWnd,NULL,false);
//Capture();
}
//////////////////////////////////////////////////////////////////////////
// D3D截图函数
UINT_PTR nTimerId=0;
HMENU ghMenu=NULL;
RECT gScreenRect ={0,0,0,0};
bool gbCapturing=false;
HWND ghWnd;
HDC hBackDC=NULL;
LPVOID pBits=NULL;
HBITMAP hBackBitmap=NULL;
HBITMAP hOldBitmap=NULL;
int nDisplayWidth;
int nDisplayHeight;
IDirect3D9* g_pD3D=NULL;
IDirect3DDevice9* g_pd3dDevice=NULL;
IDirect3DSurface9* g_pSurface=NULL;
HRESULT Reset();
void Render();
void Cleanup();
HRESULT InitD3D(HWND hWnd);
void SaveBitmap(char *szFilename,HBITMAP hBitmap);
#define WINDOW_MODE true //Not in fullscreen mode
#define BITSPERPIXEL 32
#define ErrorMessage(x) ::MessageBox(NULL,x,"Error",MB_OK|MB_ICONERROR)
CString GetExePath();