我想创建一个交换链,可以更改我的Alpha值,使其变为半透明。我的代码在这里:
#include <Windows.h>
#include <d3d11.h>
#include <d2d1_3.h>
#include <dxgi1_6.h>
#include <dwmapi.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "d2d1.lib")
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, msg, wparam, lparam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE, LPWSTR, int)
{
WNDCLASSEX wc{
sizeof WNDCLASSEX, CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hinstance, nullptr, LoadCursorW(nullptr, IDC_ARROW), nullptr, nullptr,
L"DxClass", nullptr
};
const auto hwnd{
CreateWindowExW(WS_EX_COMPOSITED, MAKEINTATOM(RegisterClassExW(&wc)), L"DirectX",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 650, HWND_DESKTOP, nullptr,
hinstance, nullptr)
};
const MARGINS marg{-1, -1, -1, -1};
DwmExtendFrameIntoClientArea(hwnd, &marg);
D3D_FEATURE_LEVEL levels[]{ D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_0 };
ID3D11Device* device;
IDXGIDevice* dxdevice;
IDXGIAdapter* adapter;
IDXGIFactory7* dxFactory;
ID2D1Device* d2dDevice;
ID2D1DeviceContext* d2dContext;
IDXGISwapChain1* swapChain;
D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_BGRA_SUPPORT, levels, 2u, D3D11_SDK_VERSION, &device, 0, 0);
device->QueryInterface(IID_PPV_ARGS(&dxdevice));
dxdevice->GetAdapter(&adapter);
adapter->GetParent(IID_PPV_ARGS(&dxFactory));
D2D1CreateDevice(dxdevice, { D2D1_THREADING_MODE_SINGLE_THREADED, D2D1_DEBUG_LEVEL_INFORMATION, D2D1_DEVICE_CONTEXT_OPTIONS_NONE }, &d2dDevice);
d2dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2dContext);
DXGI_SWAP_CHAIN_DESC1 sc{ 0 };
sc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
sc.SampleDesc.Count = 1;
sc.Scaling = DXGI_SCALING_NONE;
sc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sc.BufferCount = 2;
sc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
sc.AlphaMode = DXGI_ALPHA_MODE_PREMULTIPLIED;
sc.Flags = DXGI_SWAP_CHAIN_FLAG_FOREGROUND_LAYER;
HRESULT hr = dxFactory->CreateSwapChainForHwnd(dxdevice, hwnd, &sc, nullptr, 0, &swapChain);
if (FAILED(hr))
{
__debugbreak();
return -1;
}
MSG m;
while (GetMessageW(&m, nullptr, 0, 0))
{
TranslateMessage(&m);
DispatchMessageW(&m);
}
swapChain->Release();
d2dContext->Release();
device->Release();
d2dDevice->Release();
dxFactory->Release();
dxdevice->Release();
adapter->Release();
return 0;
}
我希望绘图表面是半透明的,因此背面将显示出来。我已经读过
WS_EX_COMPOSITED
和DwmExtendFrameIntoClientArea
将创建一个绘图表面,Direct3D可以直接将其绘制到其中,但是,由于这个问题,我还没有得到这么多。我的目标是在不使用分层窗口的情况下执行此操作,就像我看到的其他人那样:Vista D3D当我注释掉交换链结构的AlphaMode成员时,它可以工作,但是我需要使用它。我如何使它工作?谢谢。
最佳答案
如果启用调试层,错误消息将说明错误:
关于c++ - 设置DXGI_SWAP_CHAIN_DESC1的AlphaMode成员会使CreateSwapChainForHwnd失败,并出现0x887a0001,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59134319/