目前我正在开发一个使用dx9创建叠加层的应用程序。 现在我遇到一个问题,它不会使用alpha通道,因为我将Windows 10升级到最新版本(Creators Update) 例如。如果我想渲染一个透明的矩形,它总是保持100%可见
设置
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(¶ms, 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, ¶ms, nullptr, &m_pDirect3D9Device), "CreateDeviceEx()"))
return false;
渲染方法
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));
}
函数调用
DrawFilledRect(25.f, 25.f, 25.f, 25.f, 0x10FFFFFF);
有什么建议吗?它在更新之前运作良好......
答案 0 :(得分:1)
我现在已经找到了解决方案。
我必须删除ULW_COLORKEY选项,该选项意味着使用crkey作为透明色。对于最新的创建者更新,这可能无法正常工作。