How do I draw transparent Rectangles using DirectX in C++?

百般思念 提交于 2019-12-12 21:16:44

问题


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
    );


来源:https://stackoverflow.com/questions/18279272/how-do-i-draw-transparent-rectangles-using-directx-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!