How do I draw transparent Rectangles using DirectX

2019-07-11 18:23发布

问题:

I want to draw objects to be partially transparent, but I don't know how. I'm using MSDN and coding in C++.

The following code is how I draw a regular rectangle, but I want to draw a transparent rectangle.

VOID DrawingObject::Draw()
{
    ID2D1HwndRenderTarget *m_pRenderTarget;
    m_pRenderTarget->FillRectangle(RectF(10, 10, 20, 20),
        m_pD2DDriver->GetBrush(static_cast<DrawingColor>(m_uColorIndex))
        );
}

Any help or guidance is greatly appreciated.

回答1:

Have a look at the Brush Interface. You can create a brush and use SetOpacity to create a transparent brush to send to the rectangle.

You can also just create the color directly with the D2D1::ColorF(red,green,blue,alpha) function. The alpha argument is transparency. 0 is completely transparent while 1 is opaque.

If you are unaware on how to use it, this link contains fantastic examples with code on how to use the ID2D1 Brushes. Below is some sample code from that page.

ID2D1SolidColorBrush *pGridBrush = NULL;
hr = pCompatibleRenderTarget->CreateSolidColorBrush(
    D2D1::ColorF(D2D1::ColorF(0.93f, 0.94f, 0.96f, 1.0f)),
    &pGridBrush
    );