code
#include <Windows.h>
#include <d3d11.h>
#include <dxgi1_2.h>
#include <wrl/client.h>
#include <iostream>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
using Microsoft::WRL::ComPtr;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Step 1: Register the window class
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Step 2: Create the window
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Screen Capture", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
// Step 3: Initialize desktop duplication
ComPtr<ID3D11Device> d3dDevice;
ComPtr<ID3D11DeviceContext> d3dDeviceContext;
ComPtr<IDXGIAdapter> dxgiAdapter;
ComPtr<IDXGIOutput> dxgiOutput;
ComPtr<IDXGIOutput1> dxgiOutput1;
ComPtr<IDXGIOutputDuplication> dxgiOutputDuplication;
DXGI_OUTDUPL_FRAME_INFO frameInfo = { };
DXGI_MODE_DESC modeDesc = { };
modeDesc.Width = 1920;
modeDesc.Height = 1080;
modeDesc.RefreshRate.Numerator = 60;
modeDesc.RefreshRate.Denominator = 1;
modeDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
modeDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
modeDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0 };
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
HRESULT hr = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, creationFlags, featureLevels, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, &d3dDevice, NULL, &d3dDeviceContext);
if (FAILED(hr))
{
return 0;
}
hr = dxgiAdapter.As(&dxgiOutput);
if (FAILED(hr))
{
return 0;
}
hr = dxgiOutput.As(&dxgiOutput1);
if (FAILED(hr))
{
return 0;
}
DXGI_OUTPUT_DESC outputDesc = { };
hr = dxgiOutput->GetDesc(&outputDesc);
if (FAILED(hr))
{
return 0;
}
hr = dxgiOutput1->DuplicateOutput(d3dDevice.Get(), &dxgiOutputDuplication);
if (FAILED(hr))
{
return 0;
}
// Step 4: Run message loop
ShowWindow(hwnd, nCmdShow);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
hr = dxgiOutputDuplication->AcquireNextFrame(0, &frameInfo, &dxgiResource);
if (hr == DXGI_ERROR_ACCESS_LOST)
{
dxgiOutputDuplication->ReleaseFrame();
dxgiOutputDuplication.Reset();
hr = dxgiOutput1->DuplicateOutput(d3dDevice.Get(), &dxgiOutputDuplication);
if (FAILED(hr))
{
break;
}
else
{
continue;
}
}
else if (FAILED(hr))
{
break;
}
DXGISurfaceScopedHandle dxgiScopedHandle(dxgiResource);
ComPtr<ID3D11Texture2D> d3dTexture;
hr = d3dDevice->OpenSharedResource(dxgiScopedHandle.Get(), IID_PPV_ARGS(&d3dTexture));
if (FAILED(hr))
{
dxgiOutputDuplication->ReleaseFrame();
break;
}
// Step 5: Add watermark to screen capture
ComPtr<ID3D11Texture2D> d3dTextureStaging;
D3D11_TEXTURE2D_DESC textureDesc;
d3dTexture->GetDesc(&textureDesc);
textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
textureDesc.Usage = D3D11_USAGE_STAGING;
textureDesc.BindFlags = 0;
textureDesc.MiscFlags = 0;
hr = d3dDevice->CreateTexture2D(&textureDesc, nullptr, &d3dTextureStaging);
if (FAILED(hr))
{
dxgiOutputDuplication->ReleaseFrame();
break;
}
d3dDeviceContext->CopyResource(d3dTextureStaging.Get(), d3dTexture.Get());
D3D11_MAPPED_SUBRESOURCE resource;
hr = d3dDeviceContext->Map(d3dTextureStaging.Get(), 0, D3D11_MAP_READ, 0, &resource);
if (FAILED(hr))
{
dxgiOutputDuplication->ReleaseFrame();
break;
}
BYTE* buffer = static_cast<BYTE*>(resource.pData);
int pitch = resource.RowPitch;
int width = textureDesc.Width;
int height = textureDesc.Height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
BYTE* pixelLocation = buffer + (y * pitch) + (x * 4);
// Add watermark (e.g. text, logo) to screen capture
if (x < 300 && y < 50)
{
pixelLocation[0] = 255; // Blue
pixelLocation[1] = 255; // Green
pixelLocation[2] = 255; // Red
}
}
}
d3dDeviceContext->Unmap(d3dTextureStaging.Get(), 0);
// Step 6: Display screen capture with watermark
const int screen_width = GetSystemMetrics(SM_CXSCREEN);
const int screen_height = GetSystemMetrics(SM_CYSCREEN);
HDC hdcScreen = GetDC(NULL);
HDC hdcWindow = GetDC(hwnd);
BitBlt(hdcWindow, 0, 0, screen_width, screen_height, hdcScreen, 0, 0, SRCCOPY);
RECT rcClient;
GetClientRect(hwnd, &rcClient);
DXGI_MAPPED_RECT mappedRect;
HRESULT hr = dxgiOutputDuplication->MapDesktopSurface(&mappedRect);
if (SUCCEEDED(hr))
{
BITMAPINFOHEADER bmiHeader = { sizeof(BITMAPINFOHEADER), width, -height, 1, 32, BI_RGB, 0, 0, 0, 0, };
SetDIBitsToDevice(hdcWindow, 0, 0, width, height, 0, 0, 0, height, &mappedRect.pBits, (BITMAPINFO*)&bmiHeader, DIB_RGB_COLORS);
dxgiOutputDuplication->UnMapDesktopSurface();
dxgiOutputDuplication->ReleaseFrame();
}
ReleaseDC(NULL, hdcScreen);
ReleaseDC(hwnd, hdcWindow);
}
// Step 7: Cleanup
dxgiOutputDuplication.Reset();
dxgiOutput1.Reset();
dxgiOutput.Reset();
DestroyWindow(hwnd);
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
{
PostQuitMessage(0);
return 0;
}
return 0;
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
此代码显示如何创建窗口并使用Desktop Duplication API从屏幕捕获屏幕截图。然后,它使用D3D11映射技术为屏幕截图添加水印,并在窗口中显示屏幕截图带有水印。请注意,此代码仅添加一个简单的水印,您可以根据需要扩展此代码以添加更复杂的水印。