Search code examples
c++winapidirectxdirect2ddrawrectangle

Direct2d + winapi Rectangle not completly filled


I am currently learning D2D in C++ and I am trying to create a filled rectangle and it seems that my code is somewhat wrong because all I end up is only the Edge is getting filled here is my code(Initialize is called right after I created the Window with win32 winapi and then the Draw Function is called)

bool GraphicClass::Initialize()
{
    HRESULT result;
    //Initialize D2DFactory
    result = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &_d2dFactory);
    if (FAILED(result))
    {
        return false;
    }
    D2D1_SIZE_U size = D2D1::SizeU(
        _rc.right - _rc.left,
        _rc.bottom - _rc.top
        );
    //Initialize D2DRenderTarget
    HRESULT hr = _d2dFactory->CreateHwndRenderTarget(
        D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(
            _hwnd,size
        ),
        &_d2dRenderTarget
        );
    if (FAILED(result))
    {
        return false;
    }
    else
    {
        _d2dRenderTarget->CreateSolidColorBrush(
            D2D1::ColorF(D2D1::ColorF::Blue),
            &_blueBrush
            );
    }
    return true;
}

bool GraphicClass::DrawD2D()
{
    HRESULT result;
    _d2dRenderTarget->BeginDraw();
    _d2dRenderTarget->DrawRectangle(
        D2D1::RectF(
        _rc.left + 100.0f,
        _rc.top + 100.0f,
        _rc.right - 100.0f,
        _rc.bottom - 100.0f),
        _blueBrush);
    result = _d2dRenderTarget->EndDraw();
    if (FAILED(result))
    {
        return false;
    }
    return true;
}

probably a really stupid and small mistake, but I really would like to know how to fill the complete rectangle


Solution

  • Try calling ID2D1RenderTarget::FillRectangle, which "Paints the interior of the specified rectangle", instead of DrawRectangle, which "Draws the outline of a rectangle that has the specified dimensions and stroke style".