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开发包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值