can not fill rectangle using direct2d in winUI?

mc 5,426 Reputation points
2023-09-15T06:54:53.0433333+00:00

I am using winUI3.0 and direct2d. with .net 6.0.

here is my code:

D2D1_FACTORY_OPTIONS options = new D2D1_FACTORY_OPTIONS();

options.debugLevel = D2D1_DEBUG_LEVEL.D2D1_DEBUG_LEVEL_INFORMATION;

HRESULT hr = D2DTools.D2D1CreateFactory(D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED, ref D2DTools.CLSID_D2D1Factory, ref options, out factory);

D2D1_HWND_RENDER_TARGET_PROPERTIES hwndProperties = new D2D1_HWND_RENDER_TARGET_PROPERTIES();

var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

RECT rect1 = new RECT();

var r = GetClientRect(hwnd, out rect1);
            hwndProperties.hwnd = hwnd;
            hwndProperties.pixelSize = new D2D1_SIZE_U(1300, 720);
            hwndProperties.presentOptions = D2D1_PRESENT_OPTIONS.D2D1_PRESENT_OPTIONS_NONE;
ID2D1HwndRenderTarget prt;
hr = factory.CreateHwndRenderTarget(ref renderTargetProperties, ref hwndProperties, out prt);

if (hr != HRESULT.S_OK)
{
    return;
}

D2D1_COLOR_F color =  new D2D1_COLOR_F();
            color.a = 1;
            color.b = 0;
            color.g = 0;
            color.r = 0;
prt.CreateSolidColorBrush(color, new D2D1_BRUSH_PROPERTIES() { opacity = 1 }, out ID2D1SolidColorBrush brush);

prt.BeginDraw();

D2D1_RECT_F rect = new D2D1_RECT_F(15, 15, 1300, 700);

prt.Clear(color);

        prt.FillRectangle(ref rect, brush);
        hr = prt.EndDraw(out _, out _);

but there is no rectangle in there.

the func is in

Windows development Windows App SDK
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,521 Reputation points
    2023-09-15T11:25:16.9066667+00:00

    You used same colors for background and brush

    And don't use CreateHwndRenderTarget (old method) but Device Context

    If I replace Render in the sample I posted in the other thread :

    HRESULT Render()
    {
        HRESULT hr = HRESULT.S_OK;
        if (m_pD2DDeviceContext != null)
        {
            m_pD2DDeviceContext.BeginDraw();
    
            m_pD2DDeviceContext.Clear(new ColorF(ColorF.Enum.Blue, 1.0f));
    
            D2D1_RECT_F rectf = new D2D1_RECT_F();
            rectf.left = 50;
            rectf.top = 50;
            rectf.right = 500;
            rectf.bottom = 300;
            m_pD2DDeviceContext.FillRectangle(ref rectf, m_pMainBrush);
    
            hr = m_pD2DDeviceContext.EndDraw(out ulong tag1, out ulong tag2);
    
            if ((uint)hr == D2DTools.D2DERR_RECREATE_TARGET)
            {
                m_pD2DDeviceContext.SetTarget(null);
                SafeRelease(ref m_pD2DDeviceContext);
                hr = CreateDeviceContext();
                CleanDeviceResources();
                hr = CreateDeviceResources();
                hr = CreateSwapChain(IntPtr.Zero);
                hr = ConfigureSwapChain();
            }
            hr = m_pDXGISwapChain1.Present(1, 0);
        }
        return (hr);
    }
    

    with :

    ID2D1SolidColorBrush m_pMainBrush = null;
    
            HRESULT CreateDeviceResources()
            {
                HRESULT hr = HRESULT.S_OK;
                if (m_pD2DDeviceContext != null)
                {
                    if (m_pMainBrush == null)
                        hr = m_pD2DDeviceContext.CreateSolidColorBrush(new ColorF(ColorF.Enum.Red, 1.0f), null, out m_pMainBrush);           
                }
                return hr;
            }
    

    at end :

    SafeRelease(ref m_pMainBrush);
    

    I get :

    User's image

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.