C++ D3D9 Alpha Blending with Creators Update

2019-03-01 00:57发布

currently i'm working on an application which creates an overlay using dx9. Now i'm experiencing a problem that it won't make use of the alpha channel since i upgraded my Windows 10 to the latest build (Creators Update) E.g. if i'm trying to render a transparent rect it always stays 100% visible

Setup

WNDCLASSEX wc = {
    sizeof(WNDCLASSEX),
    0,
    WndProc,
    0,
    0,
    nullptr,
    LoadIcon(nullptr, IDI_APPLICATION),
    LoadCursor(nullptr, IDC_ARROW),
    nullptr,
    nullptr,
    OverlayName.c_str(),
    LoadIcon(nullptr, IDI_APPLICATION)
};

if (!RegisterClassEx(&wc))
    return false;

m_hWndOverlay = CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED,
    OverlayName.c_str(),
    OverlayName.c_str(),
    WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, nullptr, nullptr, nullptr, nullptr);

if (!m_hWndOverlay)
    return false;

if (!SetLayeredWindowAttributes(m_hWndOverlay, RGB(NULL, NULL, NULL), 255, ULW_COLORKEY | LWA_ALPHA))
    return false;

static MARGINS  DWMMargins = { -1, -1, -1, -1 };
if (!HR(DwmExtendFrameIntoClientArea(m_hWndOverlay, &DWMMargins), "DwmExtendFrameIntoClientArea()"))
    return false;

ShowWindow(m_hWndOverlay, SW_SHOWDEFAULT);
if (!UpdateWindow(m_hWndOverlay))
    return false;

if (!HR(Direct3DCreate9Ex(D3D_SDK_VERSION, &m_pDirect3D9Ex),        "Direct3DCreate9Ex()"))
    return false;

D3DPRESENT_PARAMETERS params;
ZeroMemory(&params, sizeof(D3DPRESENT_PARAMETERS));

params.Windowed = TRUE;
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
params.BackBufferFormat = D3DFMT_A8R8G8B8;
params.EnableAutoDepthStencil = TRUE;
params.AutoDepthStencilFormat = D3DFMT_D16;
params.MultiSampleType = D3DMULTISAMPLE_NONE;
params.PresentationInterval = 0x80000000L;
DWORD dwMSQAAQuality = 0;

if (HR(m_pDirect3D9Ex->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_A8R8G8B8, true, D3DMULTISAMPLE_NONMASKABLE, &dwMSQAAQuality), "CheckDeviceMultiSampleType()"))
{
    params.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE;
    params.MultiSampleQuality = dwMSQAAQuality - 1;
}

if (!HR(m_pDirect3D9Ex->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWndOverlay, D3DCREATE_HARDWARE_VERTEXPROCESSING, &params, nullptr, &m_pDirect3D9Device), "CreateDeviceEx()"))
    return false;

Render Method

void DrawFilledRect(float x, float y, float w, float h, DWORD color) const
{
VERTEX_2D_DIF V[4];

V[0].color = V[1].color = V[2].color = V[3].color = color;

V[0].z = V[1].z = V[2].z = V[3].z = 0;
V[0].rhw = V[1].rhw = V[2].rhw = V[3].rhw = 1;

V[0].x = x;
V[0].y = y;
V[1].x = x + w;
V[1].y = y;
V[2].x = x + w;
V[2].y = y + h;
V[3].x = x;
V[3].y = y + h;

m_pDirect3D9Device->SetFVF(VERTEX_2D_DIF::FVF);
m_pDirect3D9Device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &V, sizeof(VERTEX_2D_DIF));
}

Function Call

DrawFilledRect(25.f, 25.f, 25.f, 25.f, 0x10FFFFFF);

Any suggestions? It worked well before the update...

标签: c++ directx
1条回答
Lonely孤独者°
2楼-- · 2019-03-01 01:44

I've found the solution by myself now.

I had to remove the ULW_COLORKEY option which was meant to use the crkey as the transparency color. This might not work correctly with the newest creator update.

查看更多
登录 后发表回答