DirectX 截屏,抓图,截图

时间:2021-02-23 15:25:09

http://alaskaxinx.blog.163.com/blog/static/976173772008527105142242/

http://www.pin5i.com/showtopic-26129.html
http://blog.sina.com.cn/s/blog_6e04a8350100ng5u.html


void CComputerRobotTestDlg::OnBnClickedButton3()
{
 HWND hWnd = (HWND)0x000D013A;

 // init 3d  http://alaskaxinx.blog.163.com/blog/static/976173772008527105142242/
 LPDIRECT3D9    g_pD3D  =NULL;
 LPDIRECT3DDEVICE9   g_pd3dDevice =NULL;
 LPDIRECT3DVERTEXBUFFER9   g_pVB  =NULL;
 //创建Direct3D对象
 if (NULL == (g_pD3D =Direct3DCreate9(D3D_SDK_VERSION)))
 {
  return;
 }

 D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&d3dpp,sizeof(d3dpp));
  d3dpp.Windowed = TRUE;
  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;


 if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pd3dDevice)))
 {
  return;
 }


 

 BOOL hrRes = 0;
 LPDIRECT3DDEVICE9 lpDevice = g_pd3dDevice;
 TCHAR fileName[255] = _T("c:\\output4.bmp");
 // BOOL ScreenShot(LPDIRECT3DDEVICE9 lpDevice, HWND hWnd, TCHAR* fileName)  http://www.pin5i.com/showtopic-26129.html
 // 需要 http://blog.sina.com.cn/s/blog_6e04a8350100ng5u.html
 // #pragma comment(lib,"d3d9.lib")
 // #pragma comment(lib,"d3dx9.lib")
 // #项目(菜单上)->XX属性(也就是下拉菜单中最后一项)->配置属性->链接->常规,然后设置“附加库目录”
 //  "C:\Program Files\Microsoft DirectX SDK (August 2007)\Include"
 //  #include "C:\\Program Files\\Microsoft DirectX SDK (August 2007)\\Include\\D3dx9tex.h"


 {
   HRESULT hr;
  
   // Get adapter display mode
   D3DDISPLAYMODE mode;
   if (FAILED(hr = lpDevice->GetDisplayMode(0, &mode)))
    return ;

    // Create the surface to hold the screen image data
    LPDIRECT3DSURFACE9 surf;
    if (FAILED(hr = lpDevice->CreateOffscreenPlainSurface(mode.Width,
     mode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surf, NULL))) //注意第四个参数不能是D3DPOOL_DEFAULT
    {
     return ;
    }

    // Get the screen data
    if (FAILED(hr = lpDevice->GetFrontBufferData(0, surf)))
    {
     surf->Release() ;
     return ;
    }

    // area to capture
    RECT *rect = NULL ;

    WINDOWINFO windowInfo ;
    windowInfo.cbSize = sizeof(WINDOWINFO) ;

    if(hWnd) // capture window
    {
     ::GetWindowInfo(hWnd, &windowInfo) ;
     rect = &windowInfo.rcWindow ;
    }

    // Save the screen date to file
    hr = D3DXSaveSurfaceToFile(fileName, D3DXIFF_BMP, surf, NULL, rect);

    surf->Release() ;

    return ;
 }
}