DirectDraw方式截取系统屏幕

#include <iostream>
#include <malloc.h>
#include <memory.h>
#include <Windows.h>
#include <tchar.h>
#include <WinGDI.h>
#include <d3d9.h>
#include <d3dx9.h>

using namespace std;

void CaptureScreenByDirectDraw()
{
        // 初始化阶段
        BITMAPINFO bmpInfo;
        ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));

        bmpInfo.bmiHeader.biSize = sizeof(BITMAPFILEHEADER);
        bmpInfo.bmiHeader.biBitCount = 32;
        bmpInfo.bmiHeader.biCompression = BI_RGB;
        bmpInfo.bmiHeader.biWidth = GetSystemMetrics(SM_CXSCREEN);
        bmpInfo.bmiHeader.biHeight = GetSystemMetrics(SM_CYSCREEN);
        bmpInfo.bmiHeader.biPlanes = 1;
        bmpInfo.bmiHeader.biSizeImage = abs(bmpInfo.bmiHeader.biHeight) * bmpInfo.bmiHeader.biWidth * bmpInfo.bmiHeader.biBitCount / 8;

        LPVOID pBits = NULL;
        HDC hDC = GetDC(GetDesktopWindow());
        HDC hBackDC = CreateCompatibleDC(hDC);
        HBITMAP hBackBitmap = CreateDIBSection(hDC, &bmpInfo, DIB_RGB_COLORS, &pBits, NULL, 0);

        if (hBackBitmap == NULL)
                cout<<"创建BackBuffer位图失败"<<endl;

        ReleaseDC(GetDesktopWindow(), hDC);

        // 开始初始化Direct3D
        D3DDISPLAYMODE d3dDisplayMode;
        D3DPRESENT_PARAMETERS d3dPresentParameters;
        
        IDirect3D9 *pD3D = Direct3DCreate9(D3D_SDK_VERSION);
        if (pD3D == NULL)
        {
                cout<<"Direct3DCreate9失败"<<endl;
                return ;
        }

        if (FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dDisplayMode)))
        {
                cout<<"GetAdapterDisplayMode失败"<<endl;
                return ;
        }

        ZeroMemory(&d3dPresentParameters, sizeof(D3DPRESENT_PARAMETERS));

        d3dPresentParameters.Windowed = TRUE;
        d3dPresentParameters.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
        d3dPresentParameters.BackBufferFormat = d3dDisplayMode.Format;
        d3dPresentParameters.BackBufferHeight = d3dDisplayMode.Height;
        d3dPresentParameters.BackBufferWidth = d3dDisplayMode.Width;
        d3dPresentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
        d3dPresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dPresentParameters.hDeviceWindow = NULL;
        d3dPresentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
        d3dPresentParameters.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;

        IDirect3DDevice9 *d3dDevice9 = NULL;
        if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dPresentParameters, &d3dDevice9)))
        {
                cout<<""<<endl;
                return ;
        }

        IDirect3DSurface9 *d3dSurface9 = NULL;
        if (FAILED(d3dDevice9->CreateOffscreenPlainSurface(d3dDisplayMode.Width, d3dDisplayMode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &d3dSurface9, NULL)))
        {
                cout<<""<<endl;
                return ;
        }

        d3dDevice9->GetFrontBufferData(0, d3dSurface9);
        D3DXSaveSurfaceToFile(_T("D:\\CaptureScreenByDirectDraw.bmp"), D3DXIFF_BMP, d3dSurface9, NULL, NULL);

        if(hBackDC)
        {
                DeleteDC(hBackDC);
                hBackDC=NULL;
        }

        if(hBackBitmap)
        {
                DeleteObject(hBackBitmap);
                hBackBitmap=NULL;
        }
}

 
 


注意:需要下载Microsoft DirectX SDK开发包

你可能感兴趣的:(DirectDraw方式截取系统屏幕)