Friday, 9 August 2013

C++ Direct2D blending colors with desktop (transparency)

C++ Direct2D blending colors with desktop (transparency)

I'm creating a window where the client area background is completely
transparent with WS_EX_LAYERED and SetLayeredWindowAttributes (m_hwnd, RGB
(0, 0, 0), 255, LWA_COLORKEY);
This leaves only the content visible (that isn't black), it's basically an
overlay like Rainmeter.
However I can't figure out how to get colors to blend with desktop colors,
if I have a color alpha 0.5, it just turns black as it gets closer to 0.
So my question is, how would I go about blending the content of my window
with the desktop, with Direct2D? Or rather what's a better way of
implementing transparency between my window and the desktop?
Here's my rendering method:
HRESULT CreateDeviceResources()
{
HRESULT hr = S_OK;
if (!m_pDCRT)
{
// Create a DC render target.
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED),
0,
0,
D2D1_RENDER_TARGET_USAGE_NONE,
D2D1_FEATURE_LEVEL_DEFAULT
);
hr = m_pD2DFactory->CreateDCRenderTarget(&props, &m_pDCRT);
}
}
HRESULT OnRender(const PAINTSTRUCT &ps)
{
HRESULT hr = S_OK;
RECT rc;
CreateDeviceResources();
// Get the dimensions of the client drawing area.
GetClientRect(m_hwnd, &rc);
if (SUCCEEDED(hr))
{
// Bind the DC to the DC render target.
hr = m_pDCRT->BindDC(ps.hdc, &rc);
m_pDCRT->BeginDraw();
m_pDCRT->SetTransform(D2D1::Matrix3x2F::Identity());
m_pDCRT->Clear(D2D1::ColorF(0,0.0f));
DrawRocket();
DrawHorizontalLine();
hr = m_pDCRT->EndDraw();
}
}

No comments:

Post a Comment